Skip to content

Commit 61d84f2

Browse files
authored
fix: Use the private body and header datamembers for DeserializeResponseBodyAsync and DeserializeResponseHeaders (#1151)
1 parent 43e680e commit 61d84f2

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

src/SendGrid/Permissions/SendGridClientExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class SendGridClientExtensions
1919
public static async Task<SendGridPermissionsBuilder> CreateMaskedPermissionsBuilderForClient(this ISendGridClient client)
2020
{
2121
var response = await client.RequestAsync(method: SendGridClient.Method.GET, urlPath: "scopes");
22-
var body = await response.DeserializeResponseBodyAsync(response.Body);
22+
var body = await response.DeserializeResponseBodyAsync();
2323
var userScopesJArray = (body["scopes"] as JArray);
2424
var includedScopes = userScopesJArray.Values<string>().ToArray();
2525
var builder = new SendGridPermissionsBuilder();

src/SendGrid/Response.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ public class Response
2121
/// <summary>
2222
/// The status code returned from Twilio SendGrid.
2323
/// </summary>
24-
private HttpStatusCode statusCode;
24+
private HttpStatusCode _statusCode;
2525

2626
/// <summary>
2727
/// The response body returned from Twilio SendGrid.
2828
/// </summary>
29-
private HttpContent body;
29+
private HttpContent _body;
3030

3131
/// <summary>
3232
/// The response headers returned from Twilio SendGrid.
3333
/// </summary>
34-
private HttpResponseHeaders headers;
34+
private HttpResponseHeaders _headers;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="Response"/> class.
@@ -53,12 +53,12 @@ public HttpStatusCode StatusCode
5353
{
5454
get
5555
{
56-
return this.statusCode;
56+
return this._statusCode;
5757
}
5858

5959
set
6060
{
61-
this.statusCode = value;
61+
this._statusCode = value;
6262
}
6363
}
6464

@@ -72,57 +72,67 @@ public bool IsSuccessStatusCode
7272

7373
/// <summary>
7474
/// Gets or sets the response body returned from Twilio SendGrid.
75+
/// <see href="https://docs.microsoft.com/dotnet/api/system.net.http.httpcontent"></see>
7576
/// </summary>
7677
public HttpContent Body
7778
{
7879
get
7980
{
80-
return this.body;
81+
return this._body;
8182
}
8283

8384
set
8485
{
85-
this.body = value;
86+
this._body = value;
8687
}
8788
}
8889

8990
/// <summary>
9091
/// Gets or sets the response headers returned from Twilio SendGrid.
92+
/// <see href="https://docs.microsoft.com/dotnet/api/system.net.http.headers.httpresponseheaders"></see>
9193
/// </summary>
9294
public HttpResponseHeaders Headers
9395
{
9496
get
9597
{
96-
return this.headers;
98+
return this._headers;
9799
}
98100

99101
set
100102
{
101-
this.headers = value;
103+
this._headers = value;
102104
}
103105
}
104106

105107
/// <summary>
106108
/// Converts string formatted response body to a Dictionary.
107109
/// </summary>
108-
/// <param name="content">https://docs.microsoft.com/dotnet/api/system.net.http.httpcontent.</param>
109110
/// <returns>Dictionary object representation of HttpContent.</returns>
110-
public virtual async Task<Dictionary<string, dynamic>> DeserializeResponseBodyAsync(HttpContent content)
111+
public virtual async Task<Dictionary<string, dynamic>> DeserializeResponseBodyAsync()
111112
{
112-
var stringContent = await content.ReadAsStringAsync().ConfigureAwait(false);
113+
if (this._body is null)
114+
{
115+
return new Dictionary<string, dynamic>();
116+
}
117+
118+
var stringContent = await this._body.ReadAsStringAsync().ConfigureAwait(false);
113119
var dsContent = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(stringContent);
114120
return dsContent;
115121
}
116122

117123
/// <summary>
118124
/// Converts string formatted response headers to a Dictionary.
119125
/// </summary>
120-
/// <param name="content">https://docs.microsoft.com/dotnet/api/system.net.http.headers.httpresponseheaders.</param>
121126
/// <returns>Dictionary object representation of HttpResponseHeaders.</returns>
122-
public virtual Dictionary<string, string> DeserializeResponseHeaders(HttpResponseHeaders content)
127+
public virtual Dictionary<string, string> DeserializeResponseHeaders()
123128
{
124129
var dsContent = new Dictionary<string, string>();
125-
foreach (var pair in content)
130+
if (this._headers == null)
131+
{
132+
return dsContent;
133+
}
134+
135+
foreach (var pair in this._headers)
126136
{
127137
dsContent.Add(pair.Key, pair.Value.First());
128138
}

tests/SendGrid.Tests/ResponseTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
using System.Collections.Generic;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
8+
using Xunit;
9+
10+
namespace SendGrid.Tests
11+
{
12+
public class ResponseTests
13+
{
14+
[Fact]
15+
public async Task DeserializeResponseBodyAsync_NullHttpContent_ReturnsEmptyDictionary()
16+
{
17+
var response = new Response(HttpStatusCode.OK, null, null);
18+
Dictionary<string, dynamic> responseBody = await response.DeserializeResponseBodyAsync();
19+
Assert.Empty(responseBody);
20+
}
21+
22+
[Fact]
23+
public async Task DeserializeResponseBodyAsync_JsonHttpContent_ReturnsBodyAsDictionary()
24+
{
25+
var content = "{\"scopes\": [\"alerts.read\"]}";
26+
var response = new Response(HttpStatusCode.OK, new StringContent(content), null);
27+
Dictionary<string, dynamic> responseBody = await response.DeserializeResponseBodyAsync();
28+
Assert.Equal(new JArray() { "alerts.read" }, responseBody["scopes"]);
29+
}
30+
31+
[Fact]
32+
public void DeserializeResponseHeaders_NullHttpResponseHeaders_ReturnsEmptyDictionary()
33+
{
34+
var response = new Response(HttpStatusCode.OK, null, null);
35+
Dictionary<string, string> responseHeadersDeserialized = response.DeserializeResponseHeaders();
36+
Assert.Empty(responseHeadersDeserialized);
37+
}
38+
39+
[Fact]
40+
public void DeserializeResponseHeaders_NullHttpResponseHeaders_ReturnsHeadersAsDictionary()
41+
{
42+
var message = new HttpResponseMessage();
43+
message.Headers.Add("HeaderKey", "HeaderValue");
44+
var response = new Response(HttpStatusCode.OK, null, message.Headers);
45+
Dictionary<string, string> responseHeadersDeserialized = response.DeserializeResponseHeaders();
46+
Assert.Equal("HeaderValue", responseHeadersDeserialized["HeaderKey"]);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)