Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/GraphQL.Client/Exceptions/GraphQLHttpException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Net.Http;

namespace GraphQL.Client.Exceptions {

/// <summary>
/// An exception thrown on unexpected <see cref="System.Net.Http.HttpResponseMessage"/>
/// </summary>
public class GraphQLHttpException : Exception {

/// <summary>
/// The <see cref="System.Net.Http.HttpResponseMessage"/>
/// </summary>
public HttpResponseMessage HttpResponseMessage { get; }

/// <summary>
/// Creates a new instance of <see cref="GraphQLHttpException"/>
/// </summary>
/// <param name="httpResponseMessage">The unexpected <see cref="System.Net.Http.HttpResponseMessage"/></param>
public GraphQLHttpException(HttpResponseMessage httpResponseMessage) : base($"Unexpected {nameof(System.Net.Http.HttpResponseMessage)} with code: {httpResponseMessage.StatusCode}") {
this.HttpResponseMessage = httpResponseMessage ?? throw new ArgumentNullException(nameof(httpResponseMessage));
}

}

}
32 changes: 25 additions & 7 deletions src/GraphQL.Client/GraphQLClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using GraphQL.Client.Exceptions;
using GraphQL.Common.Request;
using GraphQL.Common.Response;
using Newtonsoft.Json;
Expand Down Expand Up @@ -116,8 +118,9 @@ public async Task<GraphQLResponse> GetAsync(GraphQLRequest request) {
var queryParamsBuilder = new StringBuilder($"query={request.Query}", 3);
if (request.OperationName != null) { queryParamsBuilder.Append($"&operationName={request.OperationName}"); }
if (request.Variables != null) { queryParamsBuilder.Append($"&variables={JsonConvert.SerializeObject(request.Variables)}"); }
var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false);
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
using (var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false)) {
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
}
}

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

var graphQLString = JsonConvert.SerializeObject(request, this.Options.JsonSerializerSettings);
var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType);
var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false);
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
using (var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType))
using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false)) {
return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false);
}
}

/// <summary>
Expand All @@ -158,8 +162,22 @@ public void Dispose() =>
/// <param name="httpResponseMessage">The Response</param>
/// <returns>The GrahQLResponse</returns>
private async Task<GraphQLResponse> ReadHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage) {
var resultString = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<GraphQLResponse>(resultString, this.Options.JsonSerializerSettings);
using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
using (var streamReader = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(streamReader)) {
var jsonSerializer = new JsonSerializer {
ContractResolver = this.Options.JsonSerializerSettings.ContractResolver
};
try {
return jsonSerializer.Deserialize<GraphQLResponse>(jsonTextReader);
}
catch (JsonReaderException exception) {
if (httpResponseMessage.IsSuccessStatusCode) {
throw exception;
}
throw new GraphQLHttpException(httpResponseMessage);
}
}
}

}
Expand Down
1 change: 0 additions & 1 deletion src/GraphQL.Client/GraphQLClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Threading.Tasks;
using GraphQL.Common.Request;
using GraphQL.Common.Response;
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Common/Exceptions/GraphQLException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GraphQLException : Exception {
/// Constructor for a GraphQLException
/// </summary>
/// <param name="graphQLError">The GraphQL Error</param>
public GraphQLException(GraphQLError graphQLError):base(graphQLError.Message) {
public GraphQLException(GraphQLError graphQLError) : base(graphQLError.Message) {
this.GraphQLError = graphQLError;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace GraphQL.Client.Tests {

public abstract class BaseGraphQLClientTest : IDisposable{
public abstract class BaseGraphQLClientTest : IDisposable {

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

public void Dispose() =>
this.GraphQLClient.Dispose();
Expand Down