-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceProvider.cs
46 lines (40 loc) · 1.29 KB
/
ServiceProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace FoodService
{
public interface IServiceProvider
{
Task<List<FoodTruck>> LoadData();
}
public class ServiceProvider : IServiceProvider
{
public ServiceProvider()
{
}
public async Task<List<FoodTruck>> LoadData()
{
try
{
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(@"https://data.sfgov.org/");
HttpResponseMessage response = await httpClient.GetAsync(@"resource/rqzj-sfat.json?$limit=25").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var resultList = new List<FoodTruck>();
string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
resultList = JsonConvert.DeserializeObject<List<FoodTruck>>(result);
return resultList;
}
throw new Exception("No data found");
}
catch (Exception ex)
{
// telemetry logging exception here
throw ex;
}
}
}
}