Skip to content

Commit 7ea44d6

Browse files
Merge pull request #417 from jonnybee/#405_Reuse_HttpClient
#405 reuse http client
2 parents 6e715fd + 698740c commit 7ea44d6

File tree

6 files changed

+1782
-2273
lines changed

6 files changed

+1782
-2273
lines changed

ExampleCoreProject/Example.cs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static async Task Execute()
3131
Console.WriteLine(msg.Serialize());
3232
Console.WriteLine(response.StatusCode);
3333
Console.WriteLine(response.Headers);
34-
Console.WriteLine("\n\nPress any key to continue.");
34+
Console.WriteLine("\n\nPress <Enter> to continue.");
3535
Console.ReadLine();
3636

3737
// Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object
@@ -48,7 +48,7 @@ static async Task Execute()
4848
Console.WriteLine(msg.Serialize());
4949
Console.WriteLine(response.StatusCode);
5050
Console.WriteLine(response.Headers);
51-
Console.WriteLine("\n\nPress any key to continue.");
51+
Console.WriteLine("\n\nPress <Enter> to continue.");
5252
Console.ReadLine();
5353

5454
// Send a Single Email using the Mail Helper, entirely with convenience methods
@@ -63,7 +63,7 @@ static async Task Execute()
6363
Console.WriteLine(msg.Serialize());
6464
Console.WriteLine(response.StatusCode);
6565
Console.WriteLine(response.Headers);
66-
Console.WriteLine("\n\nPress any key to continue.");
66+
Console.WriteLine("\n\nPress <Enter> to continue.");
6767
Console.ReadLine();
6868

6969
// Send a Single Email Without the Mail Helper
@@ -94,7 +94,7 @@ static async Task Execute()
9494
urlPath: "mail/send");
9595
Console.WriteLine(response.StatusCode);
9696
Console.WriteLine(response.Headers);
97-
Console.WriteLine("\n\nPress any key to continue.");
97+
Console.WriteLine("\n\nPress <Enter> to continue.");
9898
Console.ReadLine();
9999

100100
// GET Collection
@@ -107,7 +107,7 @@ static async Task Execute()
107107
Console.WriteLine(response.StatusCode);
108108
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
109109
Console.WriteLine(response.Headers);
110-
Console.WriteLine("\n\nPress any key to continue to POST.");
110+
Console.WriteLine("\n\nPress <Enter> to continue to POST.");
111111
Console.ReadLine();
112112

113113
// POST
@@ -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);
128-
Console.WriteLine("\n\nPress any key to continue to GET single.");
127+
Console.WriteLine("\n\nPress <Enter> 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 <Enter> 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 <Enter> 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 <Enter> to DELETE and exit.");
166+
Console.ReadLine();
167+
}
163168
}
164169
}
165170
}

ExampleNet45Project/Example.cs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static async Task Execute()
3131
Console.WriteLine(msg.Serialize());
3232
Console.WriteLine(response.StatusCode);
3333
Console.WriteLine(response.Headers);
34-
Console.WriteLine("\n\nPress any key to continue.");
34+
Console.WriteLine("\n\nPress <Enter> to continue.");
3535
Console.ReadLine();
3636

3737
// Send a Single Email using the Mail Helper with convenience methods and initialized SendGridMessage object
@@ -48,7 +48,7 @@ static async Task Execute()
4848
Console.WriteLine(msg.Serialize());
4949
Console.WriteLine(response.StatusCode);
5050
Console.WriteLine(response.Headers);
51-
Console.WriteLine("\n\nPress any key to continue.");
51+
Console.WriteLine("\n\nPress <Enter> to continue.");
5252
Console.ReadLine();
5353

5454
// Send a Single Email using the Mail Helper, entirely with convenience methods
@@ -63,7 +63,7 @@ static async Task Execute()
6363
Console.WriteLine(msg.Serialize());
6464
Console.WriteLine(response.StatusCode);
6565
Console.WriteLine(response.Headers);
66-
Console.WriteLine("\n\nPress any key to continue.");
66+
Console.WriteLine("\n\nPress <Enter> to continue.");
6767
Console.ReadLine();
6868

6969
// Send a Single Email Without the Mail Helper
@@ -94,7 +94,7 @@ static async Task Execute()
9494
urlPath: "mail/send");
9595
Console.WriteLine(response.StatusCode);
9696
Console.WriteLine(response.Headers);
97-
Console.WriteLine("\n\nPress any key to continue.");
97+
Console.WriteLine("\n\nPress <Enter> to continue.");
9898
Console.ReadLine();
9999

100100
// GET Collection
@@ -107,7 +107,7 @@ static async Task Execute()
107107
Console.WriteLine(response.StatusCode);
108108
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
109109
Console.WriteLine(response.Headers);
110-
Console.WriteLine("\n\nPress any key to continue to POST.");
110+
Console.WriteLine("\n\nPress <Enter> to continue to POST.");
111111
Console.ReadLine();
112112

113113
// POST
@@ -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);
128-
Console.WriteLine("\n\nPress any key to continue to GET single.");
127+
Console.WriteLine("\n\nPress <Enter> 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 <Enter> 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 <Enter> 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 <Enter> to DELETE and exit.");
166+
Console.ReadLine();
167+
}
163168
}
164169
}
165170
}

SendGrid.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Global
2828
Release|Any CPU = Release|Any CPU
2929
EndGlobalSection
3030
GlobalSection(ProjectConfigurationPlatforms) = postSolution
31-
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.ActiveCfg = Release|Any CPU
32-
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.Build.0 = Release|Any CPU
31+
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{377C20E4-2297-488F-933B-FB635C56D8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
3333
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
3434
{377C20E4-2297-488F-933B-FB635C56D8FC}.Release|Any CPU.Build.0 = Release|Any CPU
3535
{D89ADAEA-2BE8-49AC-B5BC-6EABBB2AE4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU

0 commit comments

Comments
 (0)