Skip to content

Commit 9f1d3f2

Browse files
committed
Call the JohnDeere api and display results.
1 parent bb886aa commit 9f1d3f2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

CSharpApp/Controllers/HomeController.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using System;
1212
using System.Text;
1313

14-
namespace CSharpApp.Controllers
14+
namespace CSharpApp.Controllers
1515
{
1616
public class HomeController : Controller
1717
{
@@ -98,6 +98,16 @@ public async Task<IActionResult> Callback(string code, string state)
9898
[Route("/call-api")]
9999
public async Task<IActionResult> CallAPI(string url)
100100
{
101+
var response = await SecuredApiGetRequest(url);
102+
103+
response.EnsureSuccessStatusCode();
104+
105+
var responseBody = await response.Content.ReadAsStringAsync();
106+
var jsonResponse = JsonConvert.DeserializeObject(responseBody);
107+
108+
ViewBag.APIResponse = JsonConvert.SerializeObject(jsonResponse, Formatting.Indented);
109+
ViewBag.Settings = _settings;
110+
101111
return View("Index");
102112
}
103113

@@ -119,6 +129,16 @@ private static async Task<Dictionary<string, object>> GetOAuthMetadata(string We
119129
return oAuthMetadata;
120130
}
121131

132+
private async Task<HttpResponseMessage> SecuredApiGetRequest(string url)
133+
{
134+
var client = new HttpClient();
135+
var token = _settings.AccessToken.access_token;
136+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.deere.axiom.v3+json"));
137+
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
138+
139+
return await client.GetAsync(url);
140+
}
141+
122142
public IActionResult Privacy()
123143
{
124144
return View();

CSharpApp/Views/Home/Index.cshtml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
<input size="100" type="url" id="url" name="url" value="@ViewBag.Settings.APIURL/organizations"><br/><br/>
3737
<button type="submit" class="btn-primary">Go!</button>
3838
</form>
39+
40+
@if(ViewBag.APIResponse != null) {
41+
<div>
42+
<pre class="token">
43+
<code class="language-json" >
44+
@ViewBag.APIResponse
45+
</code>
46+
</pre>
47+
</div>
48+
}
3949
}
4050
</body>
4151
</html>

0 commit comments

Comments
 (0)