This repository has been archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EzBtcClient.cs
111 lines (90 loc) · 3.9 KB
/
EzBtcClient.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace EzBtc.Api
{
public partial class EzBtcClient : IDisposable
{
private string _url;
private readonly HttpClient _httpClient = new HttpClient();
internal static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }
};
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="EzBtcClient"/> class.
/// </summary>
public EzBtcClient()
{
_url = "https://www.ezbtc.ca/api/";
_httpClient.BaseAddress = new Uri(_url);
}
#endregion
/// <summary>
/// Sends a public GET request to the ezBtc API as an asynchronous operation.
/// </summary>
/// <typeparam name="T">Type of data contained in the response.</typeparam>
/// <param name="requestUrl">The relative URL the request is sent to.</param>
/// <param name="args">Optional arguments passed as querystring parameters.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
/// <remarks>The <paramref name="requestUrl"/> is relative to https://www.ezbtc.ca/api/</remarks>
/// <exception cref="ArgumentNullException"><paramref name="requestUrl"/> is <c>null</c>.</exception>
/// <exception cref="HttpRequestException">There was a problem with the HTTP request.</exception>
public async Task<T> QueryPublicAsync<T>(string requestUrl, Dictionary<string, string> args = null)
{
if (requestUrl == null)
throw new ArgumentNullException(nameof(requestUrl));
args = args ?? new Dictionary<string, string>(0);
// Setup request.
var urlEncodedArgs = UrlEncode(args);
var req = new HttpRequestMessage()
{
Method = HttpMethod.Get,
RequestUri = new Uri(_httpClient.BaseAddress, string.Format("{0}?{1}", requestUrl, urlEncodedArgs))
};
// Send request and deserialize response.
return await SendRequestAsync<T>(req).ConfigureAwait(false);
}
/// <summary>
/// Releases the unmanaged resources and disposes of the managed resources used by the
/// underlying <see cref="HttpClient"/>.
/// </summary>
public void Dispose() => _httpClient.Dispose();
#region Private methods
private async Task<T> SendRequestAsync<T>(HttpRequestMessage req)
{
var reqCtx = new RequestContext
{
HttpRequest = req
};
// Perform the HTTP request.
var res = await _httpClient.SendAsync(reqCtx.HttpRequest).ConfigureAwait(false);
var resCtx = new ResponseContext
{
HttpResponse = res
};
// Throw for HTTP-level error.
resCtx.HttpResponse.EnsureSuccessStatusCode();
// Get the response.
var jsonContent = await resCtx.HttpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Debug.WriteLine(req);
Debug.WriteLine(res);
Debug.WriteLine(jsonContent);
// Deserialize the response.
var response = JsonConvert.DeserializeObject<T>(jsonContent, JsonSettings);
return response;
}
private static string UrlEncode(Dictionary<string, string> args) => string.Join(
"&",
args.Where(x => x.Value != null).Select(x => x.Key + "=" + WebUtility.UrlEncode(x.Value))
);
#endregion
}
}