Skip to content

Commit 46edc75

Browse files
author
Raul Hidalgo Caballero
authored
Fix Issue 19 (#21)
* deleted using * parsing JSON as stream * protected * GraphQLHttpException * catching errors * fix style * disposing requests
1 parent 432b644 commit 46edc75

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
namespace GraphQL.Client.Exceptions {
5+
6+
/// <summary>
7+
/// An exception thrown on unexpected <see cref="System.Net.Http.HttpResponseMessage"/>
8+
/// </summary>
9+
public class GraphQLHttpException : Exception {
10+
11+
/// <summary>
12+
/// The <see cref="System.Net.Http.HttpResponseMessage"/>
13+
/// </summary>
14+
public HttpResponseMessage HttpResponseMessage { get; }
15+
16+
/// <summary>
17+
/// Creates a new instance of <see cref="GraphQLHttpException"/>
18+
/// </summary>
19+
/// <param name="httpResponseMessage">The unexpected <see cref="System.Net.Http.HttpResponseMessage"/></param>
20+
public GraphQLHttpException(HttpResponseMessage httpResponseMessage) : base($"Unexpected {nameof(System.Net.Http.HttpResponseMessage)} with code: {httpResponseMessage.StatusCode}") {
21+
this.HttpResponseMessage = httpResponseMessage ?? throw new ArgumentNullException(nameof(httpResponseMessage));
22+
}
23+
24+
}
25+
26+
}

src/GraphQL.Client/GraphQLClient.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.IO;
23
using System.Net.Http;
34
using System.Net.Http.Headers;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using GraphQL.Client.Exceptions;
68
using GraphQL.Common.Request;
79
using GraphQL.Common.Response;
810
using Newtonsoft.Json;
@@ -116,8 +118,9 @@ public async Task<GraphQLResponse> GetAsync(GraphQLRequest request) {
116118
var queryParamsBuilder = new StringBuilder($"query={request.Query}", 3);
117119
if (request.OperationName != null) { queryParamsBuilder.Append($"&operationName={request.OperationName}"); }
118120
if (request.Variables != null) { queryParamsBuilder.Append($"&variables={JsonConvert.SerializeObject(request.Variables)}"); }
119-
var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false);
120-
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
121+
using (var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false)) {
122+
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
123+
}
121124
}
122125

123126
/// <summary>
@@ -141,9 +144,10 @@ public async Task<GraphQLResponse> PostAsync(GraphQLRequest request) {
141144
if (request.Query == null) { throw new ArgumentNullException(nameof(request.Query)); }
142145

143146
var graphQLString = JsonConvert.SerializeObject(request, this.Options.JsonSerializerSettings);
144-
var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType);
145-
var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false);
146-
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
147+
using (var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType))
148+
using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false)) {
149+
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
150+
}
147151
}
148152

149153
/// <summary>
@@ -158,8 +162,22 @@ public void Dispose() =>
158162
/// <param name="httpResponseMessage">The Response</param>
159163
/// <returns>The GrahQLResponse</returns>
160164
private async Task<GraphQLResponse> ReadHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage) {
161-
var resultString = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
162-
return JsonConvert.DeserializeObject<GraphQLResponse>(resultString, this.Options.JsonSerializerSettings);
165+
using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
166+
using (var streamReader = new StreamReader(stream))
167+
using (var jsonTextReader = new JsonTextReader(streamReader)) {
168+
var jsonSerializer = new JsonSerializer {
169+
ContractResolver = this.Options.JsonSerializerSettings.ContractResolver
170+
};
171+
try {
172+
return jsonSerializer.Deserialize<GraphQLResponse>(jsonTextReader);
173+
}
174+
catch (JsonReaderException exception) {
175+
if (httpResponseMessage.IsSuccessStatusCode) {
176+
throw exception;
177+
}
178+
throw new GraphQLHttpException(httpResponseMessage);
179+
}
180+
}
163181
}
164182

165183
}

src/GraphQL.Client/GraphQLClientExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.IO;
21
using System.Threading.Tasks;
32
using GraphQL.Common.Request;
43
using GraphQL.Common.Response;

src/GraphQL.Common/Exceptions/GraphQLException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GraphQLException : Exception {
1717
/// Constructor for a GraphQLException
1818
/// </summary>
1919
/// <param name="graphQLError">The GraphQL Error</param>
20-
public GraphQLException(GraphQLError graphQLError):base(graphQLError.Message) {
20+
public GraphQLException(GraphQLError graphQLError) : base(graphQLError.Message) {
2121
this.GraphQLError = graphQLError;
2222
}
2323

tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace GraphQL.Client.Tests {
44

5-
public abstract class BaseGraphQLClientTest : IDisposable{
5+
public abstract class BaseGraphQLClientTest : IDisposable {
66

7-
public GraphQLClient GraphQLClient { get; set; } = new GraphQLClient("https://swapi.apis.guru/");
7+
protected GraphQLClient GraphQLClient { get; } = new GraphQLClient("https://swapi.apis.guru/");
88

99
public void Dispose() =>
1010
this.GraphQLClient.Dispose();

0 commit comments

Comments
 (0)