-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApiClient.cs
63 lines (56 loc) · 2.47 KB
/
ApiClient.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Text;
using MagazineAPI;
namespace MagazineAPI {
class ApiClient {
private readonly HttpClient httpClient;
private readonly string token;
public ApiClient() {
httpClient = new HttpClient {
BaseAddress = new Uri("http://magazinestore.azurewebsites.net/api/")
};
token = GetTokenAsync().Result;
}
public async Task<List<string>> GetCategoriesAsync()
{
ApiResponseData<List<string>> data = await GetAsync<ApiResponseData<List<string>>>($"categories/{token}");
return data.data;
}
public async Task<List<Magazine>> GetMagazinesWithCategoryAsync(string category)
{
ApiResponseData<List<Magazine>> data = await GetAsync<ApiResponseData<List<Magazine>>>($"magazines/{token}/{category}");
return data.data;
}
public async Task<List<Subscriber>> GetSubscribersAsync()
{
ApiResponseData<List<Subscriber>> data = await GetAsync<ApiResponseData<List<Subscriber>>>($"subscribers/{token}");
return data.data;
}
public async Task<PostResult> SubmitAnswerAsync(List<string> subscriberIds)
{
Dictionary<string, List<string>> requestData = new Dictionary<string, List<string>>();
requestData["subscribers"] = subscriberIds;
string json = JsonConvert.SerializeObject(requestData, Formatting.Indented);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
PostResultData data = await PostAsync<PostResultData>($"answer/{token}", content);
return data.data;
}
private async Task<string> GetTokenAsync() {
ApiResponseData data = await GetAsync<ApiResponseData>("token");
return data.token;
}
private async Task<T> GetAsync<T>(string path) {
string response = await httpClient.GetStringAsync(path);
return JsonConvert.DeserializeObject<T>(response);
}
private async Task<T> PostAsync<T>(string path, HttpContent content) {
HttpResponseMessage response = await httpClient.PostAsync(path, content);
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
}
}