-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMcfClient.cs
183 lines (151 loc) · 6.68 KB
/
McfClient.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using MyCashFlow.Client.Exceptions;
using MyCashFlow.Client.Serialization.Responses;
using Newtonsoft.Json;
namespace MyCashFlow.Client
{
public class McfClient
{
public string BaseUrl { get; }
private readonly HttpClient _client;
private readonly string _baseUrl = "https://{0}.mycashflow.fi/api/v1";
private readonly string _userName;
private readonly string _apiKey;
public McfClient(string storename, string username, string apiKey)
{
_baseUrl = string.Format(_baseUrl, storename);
_client = new HttpClient();
_userName = username;
_apiKey = apiKey;
}
public async Task<StockItemResponse> GetStockItem(string productCode)
{
var uriBuilder = new StringBuilder();
uriBuilder.Append(_baseUrl);
uriBuilder.Append("/stock/");
if (string.IsNullOrWhiteSpace(productCode))
{
throw new Exception("No valid ProductCode provided");
}
uriBuilder.Append(productCode.Trim());
var uriString = uriBuilder.ToString();
return await GetPayloadUsingGet<StockItemResponse>(uriString);
}
public async Task<ProductItemResponse> GetProductItemById(int productId, bool expandByVariations = false)
{
var uriBuilder = new StringBuilder();
uriBuilder.Append(_baseUrl);
uriBuilder.Append("/products/");
uriBuilder.Append(productId);
if (expandByVariations)
{
uriBuilder.Append("?expand=variations");
}
var uriString = uriBuilder.ToString();
return await GetPayloadUsingGet<ProductItemResponse>(uriString);
}
public async Task<ProductItemResponse> GetProductItemByProductCode(string productCode, bool expandByVariations = false)
{
var uriBuilder = new StringBuilder();
uriBuilder.Append(_baseUrl);
uriBuilder.Append("/products/product_code=");
if (string.IsNullOrWhiteSpace(productCode))
{
throw new Exception("No valid ProductCode provided");
}
uriBuilder.Append(productCode.Trim());
if (expandByVariations)
{
uriBuilder.Append("?expand=variations");
}
var uriString = uriBuilder.ToString();
return await GetPayloadUsingGet<ProductItemResponse>(uriString);
}
public async Task<StockChangeResponse> GetStockChanges(int? stockItemId, DateTimeOffset? changedAtFrom, DateTimeOffset? changedAtTo, int pageSize = 100, int page = 1)
{
var uriBuilder = new StringBuilder();
uriBuilder.Append(_baseUrl);
uriBuilder.Append("/stock-changes?");
if (stockItemId != null)
{
uriBuilder.Append($"stock_item_id={stockItemId.Value}&");
}
if (changedAtFrom != null)
{
uriBuilder.Append($"changed_at-from={changedAtFrom.Value:s}&");
}
if (changedAtTo != null)
{
uriBuilder.Append($"changed_at-to={changedAtTo.Value:s}&");
}
uriBuilder.Append($"page_size={pageSize}&");
uriBuilder.Append($"page={page}&");
var uriString = uriBuilder.ToString();
uriString = uriString.Remove(uriString.Length - 1);
return await GetPayloadUsingGet<StockChangeResponse>(uriString);
}
private async Task<TResponse> GetPayloadUsingGet<TResponse>(string uri)
{
var client = GetAuthorizedClient();
using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(uri)))
{
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = await client.SendAsync(request);
var headers = response.Headers.ToDictionary(h => h.Key, h => h.Value);
if (response.Content != null && response.Content.Headers != null)
{
foreach (var httpContentHeader in response.Content.Headers)
{
headers[httpContentHeader.Key] = httpContentHeader.Value;
}
}
var status = $"{(int) response.StatusCode}";
string errorMsg;
switch (status)
{
case "200":
var jsonContent = await response.Content.ReadAsStringAsync();
var deserialized = JsonConvert.DeserializeObject<TResponse>(jsonContent);
return deserialized;
case "400":
errorMsg = "Bad request. Make sure that the request is using the encrypted HTTPS protocol.";
break;
case "401":
errorMsg = "Unauthorized. Check the user name and API key used to authenticate the request.";
break;
case "404":
errorMsg = "Not found.";
break;
case "406":
errorMsg =
"Not acceptable. The request did not include the Accept: application/json HTTP header.";
break;
case "409":
errorMsg = "The action cannot be processed, because the item is in an incorrect state.";
break;
case "500":
errorMsg =
"Processing the action was interrupted by an error in MyCashflow or an external service.";
break;
default:
errorMsg = $"The HTTP status code of the response was not expected ({status}).";
break;
}
var responseData = response.Content == null ? null : await response.Content.ReadAsStringAsync();
throw new McfClientException(errorMsg, status, responseData, headers);
}
}
private HttpClient GetAuthorizedClient()
{
var client = _client;
var byteArray = Encoding.ASCII.GetBytes($"{_userName}:{_apiKey}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
return client;
}
}
}