Skip to content

Commit 698740c

Browse files
author
Jonny Bekkum
committed
Added urlPath to ctor
Added summary comments
1 parent 21cdbfc commit 698740c

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/SendGrid/SendGridClient.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,24 @@ namespace SendGrid
2222
/// </summary>
2323
public class SendGridClient
2424
{
25-
private readonly string version;
26-
private readonly string mediaType;
25+
/// <summary>
26+
/// Gets or sets the path to the API resource.
27+
/// </summary>
28+
public string UrlPath { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the API version.
32+
/// </summary>
33+
public string Version { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the request media type.
37+
/// </summary>
38+
public string MediaType { get; set; }
39+
40+
/// <summary>
41+
/// The HttpClient instance to use for all calls from this SendGridClient instance.
42+
/// </summary>
2743
private HttpClient client;
2844

2945
/// <summary>
@@ -34,17 +50,17 @@ public class SendGridClient
3450
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
3551
/// <param name="requestHeaders">A dictionary of request headers</param>
3652
/// <param name="version">API version, override AddVersion to customize</param>
53+
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
3754
/// <returns>Interface to the SendGrid REST API</returns>
38-
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3")
55+
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3", string urlPath = null)
3956
{
40-
this.version = version;
57+
UrlPath = urlPath;
58+
Version = version;
4159

4260
var baseAddress = host ?? "https://api.sendgrid.com";
4361
var clientVersion = GetType().GetTypeInfo().Assembly.GetName().Version.ToString();
4462

45-
// var servicePoint = ServicePointManager.FindServicePoint()
46-
47-
// Add the WebProxy if set
63+
// Create client with WebProxy if set
4864
if (webProxy != null)
4965
{
5066
var httpClientHandler = new HttpClientHandler()
@@ -90,7 +106,7 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
90106
else if (header.Key == "Content-Type")
91107
{
92108
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value));
93-
mediaType = header.Value;
109+
MediaType = header.Value;
94110
}
95111
else
96112
{
@@ -106,9 +122,10 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
106122
/// <param name="host">Base url (e.g. https://api.sendgrid.com)</param>
107123
/// <param name="requestHeaders">A dictionary of request headers</param>
108124
/// <param name="version">API version, override AddVersion to customize</param>
125+
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint)</param>
109126
/// <returns>Interface to the SendGrid REST API</returns>
110-
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3")
111-
: this(null, apiKey, host, requestHeaders, version)
127+
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3", string urlPath = null)
128+
: this(null, apiKey, host, requestHeaders, version, urlPath)
112129
{
113130
}
114131

@@ -190,7 +207,7 @@ public async Task<Response> RequestAsync(
190207
StringContent content = null;
191208
if (requestBody != null)
192209
{
193-
content = new StringContent(requestBody, Encoding.UTF8, this.mediaType);
210+
content = new StringContent(requestBody, Encoding.UTF8, this.MediaType);
194211
}
195212

196213
// Build the final request
@@ -238,13 +255,16 @@ private string BuildUrl(string urlPath, string queryParams = null)
238255
{
239256
string url = null;
240257

241-
if (version != null)
258+
// create urlPAth - from parameter if overriden on call or from ctor parameter
259+
var urlpath = urlPath ?? UrlPath;
260+
261+
if (Version != null)
242262
{
243-
url = version + "/" + urlPath;
263+
url = Version + "/" + urlpath;
244264
}
245265
else
246266
{
247-
url = urlPath;
267+
url = urlpath;
248268
}
249269

250270
if (queryParams != null)

0 commit comments

Comments
 (0)