-
Notifications
You must be signed in to change notification settings - Fork 107
/
MandrillApi.cs
188 lines (161 loc) · 5.17 KB
/
MandrillApi.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
184
185
186
187
188
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MandrillApi.cs" company="">
//
// </copyright>
// <summary>
// Core class for using the MandrillApp Api
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Net;
using System.Threading.Tasks;
using Mandrill.Models;
using Mandrill.Requests;
using Mandrill.Utilities;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
namespace Mandrill
{
/// <summary>
/// Core class for using the MandrillApp Api
/// </summary>
public partial class MandrillApi
{
#region Fields
private readonly string baseUrl;
private HttpClient _httpClient;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="MandrillApi" /> class.
/// </summary>
/// <param name="apiKey">
/// The API Key recieved from MandrillApp
/// </param>
/// <param name="useHttps">
/// </param>
/// <param name="useStatic">
/// </param>
public MandrillApi(string apiKey, bool useHttps = true, bool useStatic = false)
{
ApiKey = apiKey;
if (useHttps && useStatic)
{
baseUrl = Configuration.BASE_STATIC_SECURE_URL;
}
else if (useHttps && !useStatic)
{
baseUrl = Configuration.BASE_SECURE_URL;
}
else if (!useHttps && useStatic)
{
baseUrl = Configuration.BASE_STATIC_URL;
}
else
{
baseUrl = Configuration.BASE_URL;
}
// Store URL value to be used in public BaseURL property
BaseUrl = baseUrl;
}
#endregion
#region Public Properties
/// <summary>
/// The Api Key for the project received from the MandrillApp website
/// </summary>
public string ApiKey { get; private set; }
///<Summary>
/// The base URL value being used for call, useful for client logging purposes
///</Summary>
public string BaseUrl { get; set; }
#endregion
#region Public Methods and Operators
/// <summary>
/// Allows overriding the HttpClient which is used in Post()
/// </summary>
/// <param name="httpClient">the httpClient to use</param>
public void SetHttpClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
/// <summary>
/// Execute post to path
/// </summary>
/// <param name="path">the path to post to</param>
/// <param name="data">the payload to send in request body as json</param>
/// <returns></returns>
public async Task<T> Post<T>(string path, RequestBase data)
{
data.Key = ApiKey;
try
{
using (var client = _httpClient ?? new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
string requestContent;
try
{
requestContent = JsonConvert.SerializeObject(data);
}
catch (JsonException ex)
{
throw new MandrillSerializeRequestException("Failed to serialize request data.", ex);
}
var response =
await
client.PostAsync(
path,
new StringContent(requestContent, Encoding.UTF8, "application/json"))
.ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return ParseResponseContent<T>(path, data, responseContent, response);
}
}
catch (TimeoutException ex)
{
throw new TimeoutException(string.Format("Post timed out to {0}", path), ex);
}
}
#endregion
#region Private Methods and Operators
private static T ParseResponseContent<T>(string path, RequestBase data, string content, HttpResponseMessage response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
try
{
return JsonConvert.DeserializeObject<T>(content);
}
catch (JsonException ex)
{
throw new MandrillDeserializeResponseException(string.Format("Failed to deserialize content received from path: {0}.", path), ex)
{
HttpResponseMessage = response,
MandrillRequest = data,
Content = content
};
}
}
try
{
var error = JsonConvert.DeserializeObject<ErrorResponse>(content);
throw new MandrillException(error, string.Format("Post failed: {0}", path))
{
HttpResponseMessage = response,
MandrillRequest = data
};
}
catch (JsonException ex)
{
throw new MandrillDeserializeResponseException(string.Format("Failed to deserialize error content received from path: {0}.", path), ex)
{
HttpResponseMessage = response,
MandrillRequest = data,
Content = content
};
}
}
#endregion
}
}