1
+ using System . IO ;
2
+ using System . Net . Http ;
3
+ using System . Net . Http . Headers ;
4
+ using System . Threading . Tasks ;
5
+ using Microsoft . AspNetCore . Authentication ;
6
+ using Microsoft . AspNetCore . Mvc ;
7
+ using Microsoft . Extensions . Configuration ;
8
+ using CSharpApp . Interfaces ;
9
+ using CSharpApp . Models ;
10
+ using Newtonsoft . Json ;
11
+ using Newtonsoft . Json . Linq ;
12
+ using Formatting = Newtonsoft . Json . Formatting ;
13
+
14
+ namespace CSharpApp . Controllers
15
+ {
16
+ public class APIController : Controller
17
+ {
18
+ protected JDeere MySettings { get ; set ; }
19
+
20
+ [ HttpPost ]
21
+ public async Task < IActionResult > EditAPIDetailsAsync ( [ FromServices ] IConfiguration configuration , string ClientId , string ClientSecret , string AuthorizationEndpoint , string TokenEndpoint , string APIEndPoint )
22
+ {
23
+ JDeere obj = new JDeere
24
+ {
25
+ ClientId = ClientId ,
26
+ ClientSecret = ClientSecret ,
27
+ AuthorizationEndpoint = AuthorizationEndpoint ,
28
+ TokenEndpoint = TokenEndpoint ,
29
+ APIEndPoint = APIEndPoint
30
+ } ;
31
+
32
+ _writableLocations . Update ( opt => {
33
+ opt . ClientId = obj . ClientId ;
34
+ opt . ClientSecret = obj . ClientSecret ;
35
+ opt . AuthorizationEndpoint = obj . AuthorizationEndpoint ;
36
+ opt . TokenEndpoint = obj . TokenEndpoint ;
37
+ opt . APIEndPoint = obj . APIEndPoint ;
38
+ } ) ;
39
+
40
+ ViewBag . ClientId = configuration [ "JDeere:ClientId" ] ;
41
+ ViewBag . ClientSecret = configuration [ "JDeere:ClientSecret" ] ;
42
+ ViewBag . AuthorizationEndpoint = configuration [ "JDeere:AuthorizationEndpoint" ] ;
43
+ ViewBag . TokenEndpoint = configuration [ "JDeere:TokenEndpoint" ] ;
44
+ ViewBag . APIEndPoint = configuration [ "JDeere:APIEndPoint" ] ;
45
+
46
+ TempData [ "ClientId" ] = ViewBag . ClientId ;
47
+ TempData [ "ClientSecret" ] = ViewBag . ClientSecret ;
48
+ TempData [ "AuthorizationEndpoint" ] = ViewBag . AuthorizationEndpoint ;
49
+ TempData [ "TokenEndpoint" ] = ViewBag . TokenEndpoint ;
50
+ TempData [ "APIEndPoint" ] = ViewBag . APIEndpoint ;
51
+
52
+ var APISave = ViewBag . APIEndPoint ;
53
+ var settings = new JDeere
54
+ {
55
+ Token = await HttpContext . GetTokenAsync ( "access_token" )
56
+ } ;
57
+ var token = await HttpContext . GetTokenAsync ( "access_token" ) ;
58
+ var Url = APISave . ToString ( ) ;
59
+ HttpClient client = new HttpClient ( ) ;
60
+
61
+ client . DefaultRequestHeaders . Accept . Clear ( ) ;
62
+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/vnd.deere.axiom.v3+json" ) ) ;
63
+ client . DefaultRequestHeaders . Add ( "Authorization" , $ "Bearer { token } ") ;
64
+
65
+ var response = await client . GetAsync ( Url ) ;
66
+ response . EnsureSuccessStatusCode ( ) ;
67
+
68
+ var stream = await response . Content . ReadAsStringAsync ( ) ;
69
+ var djson = JsonConvert . DeserializeObject < Stream > ( stream ) ;
70
+ var json = JsonConvert . SerializeObject ( stream , Formatting . Indented ) ;
71
+ var jsonFormatted = JValue . Parse ( json ) . ToString ( Formatting . Indented ) ;
72
+ ViewBag . APIPath = jsonFormatted . Value . ToString ( ) ;
73
+
74
+ return View ( obj ) ;
75
+ }
76
+
77
+ private readonly IWritableOptions < JDeere > _writableLocations ;
78
+ public APIController ( IWritableOptions < JDeere > writableLocations )
79
+ {
80
+ _writableLocations = writableLocations ;
81
+ }
82
+ public async Task < IActionResult > Index ( [ FromServices ] IConfiguration configuration , string ClientId , string ClientSecret , string AuthorizationEndpoint , string TokenEndpoint , string APIEndPoint )
83
+ {
84
+ JDeere obj = new JDeere
85
+ {
86
+ ClientId = ClientId ,
87
+ ClientSecret = ClientSecret ,
88
+ AuthorizationEndpoint = AuthorizationEndpoint ,
89
+ TokenEndpoint = TokenEndpoint ,
90
+ APIEndPoint = APIEndPoint
91
+ } ;
92
+
93
+ _writableLocations . Update ( opt => {
94
+ opt . ClientId = obj . ClientId ;
95
+ opt . ClientSecret = obj . ClientSecret ;
96
+ opt . AuthorizationEndpoint = obj . AuthorizationEndpoint ;
97
+ opt . TokenEndpoint = obj . TokenEndpoint ;
98
+ opt . APIEndPoint = obj . APIEndPoint ;
99
+ } ) ;
100
+
101
+ ViewBag . ClientId = configuration [ "JDeere:ClientId" ] ;
102
+ ViewBag . ClientSecret = configuration [ "JDeere:ClientSecret" ] ;
103
+ ViewBag . AuthorizationEndpoint = configuration [ "JDeere:AuthorizationEndpoint" ] ;
104
+ ViewBag . TokenEndpoint = configuration [ "JDeere:TokenEndpoint" ] ;
105
+ ViewBag . APIEndPoint = configuration [ "JDeere:APIEndPoint" ] ;
106
+
107
+ TempData [ "ClientId" ] = ViewBag . ClientId ;
108
+ TempData [ "ClientSecret" ] = ViewBag . ClientSecret ;
109
+ TempData [ "AuthorizationEndpoint" ] = ViewBag . AuthorizationEndpoint ;
110
+ TempData [ "TokenEndpoint" ] = ViewBag . TokenEndpoint ;
111
+ TempData [ "APIEndPoint" ] = ViewBag . APIEndpoint ;
112
+
113
+ var APISave = TempData [ "APIEndPoint" ] ;
114
+ ViewBag . APIEndPoint = APISave ;
115
+
116
+ var settings = new JDeere
117
+ {
118
+ Token = await HttpContext . GetTokenAsync ( "access_token" )
119
+ } ;
120
+
121
+ var token = await HttpContext . GetTokenAsync ( "access_token" ) ;
122
+ var Url = APISave . ToString ( ) ;
123
+ HttpClient client = new HttpClient ( ) ;
124
+
125
+ client . DefaultRequestHeaders . Accept . Clear ( ) ;
126
+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/vnd.deere.axiom.v3+json" ) ) ;
127
+ client . DefaultRequestHeaders . Add ( "Authorization" , $ "Bearer { token } ") ;
128
+
129
+ var response = await client . GetAsync ( Url ) ;
130
+ response . EnsureSuccessStatusCode ( ) ;
131
+
132
+ var stream = await response . Content . ReadAsStringAsync ( ) ;
133
+ var json = JsonConvert . SerializeObject ( stream , Formatting . Indented ) ;
134
+ var json2 = new JsonResult ( json ) { SerializerSettings = new JsonSerializerSettings ( ) { Formatting = Formatting . Indented } } ;
135
+ ViewBag . APIPath = json2 . Value . ToString ( ) ;
136
+
137
+ return View ( settings ) ;
138
+ }
139
+ }
140
+ }
0 commit comments