Skip to content

Commit 6106afb

Browse files
author
Jonny Bekkum
committed
Updated style and removed ConfigureAwait
1 parent 70e49b0 commit 6106afb

File tree

4 files changed

+94
-84
lines changed

4 files changed

+94
-84
lines changed

ExampleCoreProject/Example.cs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,50 @@ static async Task Execute()
121121
urlPath: "asm/groups",
122122
requestBody: json.ToString());
123123
var ds_response = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(response.Body.ReadAsStringAsync().Result);
124-
string group_id = ds_response["id"].ToString();
125124
Console.WriteLine(response.StatusCode);
126125
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
127126
Console.WriteLine(response.Headers);
128127
Console.WriteLine("\n\nPress any key to continue to GET single.");
129128
Console.ReadLine();
130129

131-
// GET Single
132-
response = await client.RequestAsync(method: SendGridClient.Method.GET,
133-
urlPath: string.Format("asm/groups/{0}", group_id));
134-
Console.WriteLine(response.StatusCode);
135-
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
136-
Console.WriteLine(response.Headers);
137-
Console.WriteLine("\n\nPress any key to continue to PATCH.");
138-
Console.ReadLine();
139-
140-
// PATCH
141-
requestBody = @"{
142-
'name': 'Cool Magic Products'
143-
}";
144-
json = JsonConvert.DeserializeObject<object>(requestBody);
145-
146-
response = await client.RequestAsync(method: SendGridClient.Method.PATCH,
147-
urlPath: string.Format("asm/groups/{0}", group_id),
148-
requestBody: json.ToString());
149-
Console.WriteLine(response.StatusCode);
150-
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
151-
Console.WriteLine(response.Headers.ToString());
152-
153-
Console.WriteLine("\n\nPress any key to continue to PUT.");
154-
Console.ReadLine();
155-
156-
// DELETE
157-
response = await client.RequestAsync(method: SendGridClient.Method.DELETE,
158-
urlPath: string.Format("asm/groups/{0}", group_id));
159-
Console.WriteLine(response.StatusCode);
160-
Console.WriteLine(response.Headers.ToString());
161-
Console.WriteLine("\n\nPress any key to DELETE and exit.");
162-
Console.ReadLine();
130+
if (ds_response != null && ds_response.ContainsKey("id"))
131+
{
132+
string group_id = ds_response["id"].ToString();
133+
134+
135+
// GET Single
136+
response = await client.RequestAsync(method: SendGridClient.Method.GET,
137+
urlPath: string.Format("asm/groups/{0}", group_id));
138+
Console.WriteLine(response.StatusCode);
139+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
140+
Console.WriteLine(response.Headers);
141+
Console.WriteLine("\n\nPress any key to continue to PATCH.");
142+
Console.ReadLine();
143+
144+
// PATCH
145+
requestBody = @"{
146+
'name': 'Cool Magic Products'
147+
}";
148+
json = JsonConvert.DeserializeObject<object>(requestBody);
149+
150+
response = await client.RequestAsync(method: SendGridClient.Method.PATCH,
151+
urlPath: string.Format("asm/groups/{0}", group_id),
152+
requestBody: json.ToString());
153+
Console.WriteLine(response.StatusCode);
154+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
155+
Console.WriteLine(response.Headers.ToString());
156+
157+
Console.WriteLine("\n\nPress any key to continue to PUT.");
158+
Console.ReadLine();
159+
160+
// DELETE
161+
response = await client.RequestAsync(method: SendGridClient.Method.DELETE,
162+
urlPath: string.Format("asm/groups/{0}", group_id));
163+
Console.WriteLine(response.StatusCode);
164+
Console.WriteLine(response.Headers.ToString());
165+
Console.WriteLine("\n\nPress any key to DELETE and exit.");
166+
Console.ReadLine();
167+
}
163168
}
164169
}
165170
}

ExampleNet45Project/Example.cs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,50 @@ static async Task Execute()
121121
urlPath: "asm/groups",
122122
requestBody: json.ToString());
123123
var ds_response = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(response.Body.ReadAsStringAsync().Result);
124-
string group_id = ds_response["id"].ToString();
125124
Console.WriteLine(response.StatusCode);
126125
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
127126
Console.WriteLine(response.Headers);
128127
Console.WriteLine("\n\nPress any key to continue to GET single.");
129128
Console.ReadLine();
130129

131-
// GET Single
132-
response = await client.RequestAsync(method: SendGridClient.Method.GET,
133-
urlPath: string.Format("asm/groups/{0}", group_id));
134-
Console.WriteLine(response.StatusCode);
135-
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
136-
Console.WriteLine(response.Headers);
137-
Console.WriteLine("\n\nPress any key to continue to PATCH.");
138-
Console.ReadLine();
139-
140-
// PATCH
141-
requestBody = @"{
142-
'name': 'Cool Magic Products'
143-
}";
144-
json = JsonConvert.DeserializeObject<object>(requestBody);
145-
146-
response = await client.RequestAsync(method: SendGridClient.Method.PATCH,
147-
urlPath: string.Format("asm/groups/{0}", group_id),
148-
requestBody: json.ToString());
149-
Console.WriteLine(response.StatusCode);
150-
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
151-
Console.WriteLine(response.Headers.ToString());
152-
153-
Console.WriteLine("\n\nPress any key to continue to PUT.");
154-
Console.ReadLine();
155-
156-
// DELETE
157-
response = await client.RequestAsync(method: SendGridClient.Method.DELETE,
158-
urlPath: string.Format("asm/groups/{0}", group_id));
159-
Console.WriteLine(response.StatusCode);
160-
Console.WriteLine(response.Headers.ToString());
161-
Console.WriteLine("\n\nPress any key to DELETE and exit.");
162-
Console.ReadLine();
130+
if (ds_response != null && ds_response.ContainsKey("id"))
131+
{
132+
string group_id = ds_response["id"].ToString();
133+
134+
// GET Single
135+
response = await client.RequestAsync(method: SendGridClient.Method.GET,
136+
urlPath: string.Format("asm/groups/{0}", group_id));
137+
Console.WriteLine(response.StatusCode);
138+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
139+
Console.WriteLine(response.Headers);
140+
Console.WriteLine("\n\nPress any key to continue to PATCH.");
141+
Console.ReadLine();
142+
143+
144+
// PATCH
145+
requestBody = @"{
146+
'name': 'Cool Magic Products'
147+
}";
148+
json = JsonConvert.DeserializeObject<object>(requestBody);
149+
150+
response = await client.RequestAsync(method: SendGridClient.Method.PATCH,
151+
urlPath: string.Format("asm/groups/{0}", group_id),
152+
requestBody: json.ToString());
153+
Console.WriteLine(response.StatusCode);
154+
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
155+
Console.WriteLine(response.Headers.ToString());
156+
157+
Console.WriteLine("\n\nPress any key to continue to PUT.");
158+
Console.ReadLine();
159+
160+
// DELETE
161+
response = await client.RequestAsync(method: SendGridClient.Method.DELETE,
162+
urlPath: string.Format("asm/groups/{0}", group_id));
163+
Console.WriteLine(response.StatusCode);
164+
Console.WriteLine(response.Headers.ToString());
165+
Console.WriteLine("\n\nPress any key to DELETE and exit.");
166+
Console.ReadLine();
167+
}
163168
}
164169
}
165170
}

src/SendGrid/SendGridClient.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ namespace SendGrid
2424
public class SendGridClient
2525
{
2626
private readonly string version;
27-
//private readonly string urlPath;
27+
// private readonly string urlPath;
2828
private readonly string mediaType;
2929
private HttpClient client;
3030

31-
3231
/// <summary>
3332
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
3433
/// </summary>
@@ -45,8 +44,7 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
4544
var baseAddress = host ?? "https://api.sendgrid.com";
4645
var clientVersion = GetType().GetTypeInfo().Assembly.GetName().Version.ToString();
4746

48-
49-
//var servicePoint = ServicePointManager.FindServicePoint()
47+
// var servicePoint = ServicePointManager.FindServicePoint()
5048

5149
// Add the WebProxy if set
5250
if (webProxy != null)
@@ -61,20 +59,20 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
6159
}
6260
else
6361
{
64-
client = new HttpClient();
62+
client = new HttpClient();
6563
}
6664

6765
// standard headers
6866
client.BaseAddress = new Uri(baseAddress);
6967
Dictionary<string, string> headers = new Dictionary<string, string>
7068
{
71-
{"Authorization", "Bearer " + apiKey},
72-
{"Content-Type", "application/json"},
73-
{"User-Agent", "sendgrid/" + clientVersion + " csharp"},
74-
{"Accept", "application/json"}
69+
{ "Authorization", "Bearer " + apiKey },
70+
{ "Content-Type", "application/json" },
71+
{ "User-Agent", "sendgrid/" + clientVersion + " csharp" },
72+
{ "Accept", "application/json" }
7573
};
7674

77-
// set header overrides
75+
// set header overrides
7876
if (requestHeaders != null)
7977
{
8078
foreach (var header in requestHeaders)
@@ -111,7 +109,7 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
111109
/// <param name="requestHeaders">A dictionary of request headers</param>
112110
/// <param name="version">API version, override AddVersion to customize</param>
113111
/// <returns>Interface to the SendGrid REST API</returns>
114-
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3")
112+
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = "v3")
115113
: this(null, apiKey, host, requestHeaders, version)
116114
{
117115
}
@@ -164,7 +162,7 @@ public virtual AuthenticationHeaderValue AddAuthorization(KeyValuePair<string, s
164162
/// <param name="request">The parameters for the API call</param>
165163
/// <param name="cancellationToken">Cancel the asynchronous call</param>
166164
/// <returns>Response object</returns>
167-
private async Task<Response> MakeRequest(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken))
165+
public async Task<Response> MakeRequest(HttpRequestMessage request, CancellationToken cancellationToken = default(CancellationToken))
168166
{
169167
HttpResponseMessage response = await client.SendAsync(request, cancellationToken);
170168
return new Response(response.StatusCode, response.Content, response.Headers);
@@ -188,7 +186,7 @@ public async Task<Response> RequestAsync(
188186
{
189187
try
190188
{
191-
var endpoint = BuildUrl(urlPath, queryParams);
189+
var endpoint = client.BaseAddress + BuildUrl(urlPath, queryParams);
192190
// Build the request body
193191
StringContent content = null;
194192
if (requestBody != null)
@@ -222,10 +220,11 @@ public async Task<Response> RequestAsync(
222220
/// <returns>A Response object.</returns>
223221
public async Task<Response> SendEmailAsync(SendGridMessage msg, CancellationToken cancellationToken = default(CancellationToken))
224222
{
225-
return await this.RequestAsync(Method.POST,
223+
return await this.RequestAsync(
224+
Method.POST,
226225
msg.Serialize(),
227226
urlPath: "mail/send",
228-
cancellationToken: cancellationToken).ConfigureAwait(false);
227+
cancellationToken: cancellationToken);
229228
}
230229

231230
/// <summary>
@@ -242,11 +241,11 @@ private string BuildUrl(string urlPath, string queryParams = null)
242241

243242
if (version != null)
244243
{
245-
url = "/" + version + "/" + urlPath;
244+
url = version + "/" + urlPath;
246245
}
247246
else
248247
{
249-
url = "/" + urlPath;
248+
url = urlPath;
250249
}
251250

252251
if (queryParams != null)

src/SendGrid/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"additionalArguments": [
88
"/ruleset:./SendGrid.ruleset",
99
"/additionalfile:./stylecop.json"
10-
]
10+
],
11+
"define": [ "DEBUG" ]
1112
},
1213
"configurations": {
1314
"Release": {

0 commit comments

Comments
 (0)