forked from blayvant/vert-magazine-api-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagazineClient.cs
59 lines (51 loc) · 1.95 KB
/
MagazineClient.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using MagazineAPI;
using Newtonsoft.Json;
namespace MagazineAPI
{
public class MagazineClient : IMagazineClient
{
private readonly HttpClient http;
public MagazineClient()
{
http = new HttpClient()
{
BaseAddress = new Uri("http://magazinestore.azurewebsites.net/api/")
};
}
public async Task<IEnumerable<string>> GetCategoriesAsync(string token)
{
return (await GetAsync<ApiResponse<string[]>>($"categories/{token}")).Data;
}
public async Task<IEnumerable<Magazine>> GetMagazinesAsync(string token, string category)
{
return (await GetAsync<ApiResponse<Magazine[]>>($"magazines/{token}/{category}")).Data;
}
public async Task<IEnumerable<Subscriber>> GetSubscribersAsync(string token)
{
return (await GetAsync<ApiResponse<Subscriber[]>>($"subscribers/{token}")).Data;
}
public async Task<string> GetTokenAsync()
{
return (await GetAsync<ApiResponse>("token")).Token;
}
public async Task<PostResult> SubmitAnswerAsync(string token, string[] ids)
{
var req = JsonConvert.SerializeObject(new { subscribers = ids });
var content = new StringContent(req, Encoding.UTF8, "application/json");
var response = await http.PostAsync($"answer/{token}", content);
string strResult = await response.Content.ReadAsStringAsync();
var apiResponse = JsonConvert.DeserializeObject<ApiResponse<PostResult>>(strResult);
return apiResponse.Data;
}
private async Task<T> GetAsync<T>(string path)
{
var strResponse = await http.GetStringAsync(path);
return JsonConvert.DeserializeObject<T>(strResponse);
}
}
}