Skip to content

Commit

Permalink
Merge pull request #105 from ShipEngine/allow-no-content-response
Browse files Browse the repository at this point in the history
Allow SDK to handle No Content (204) responses
  • Loading branch information
joshuaflanagan authored Jul 22, 2024
2 parents 3b89b48 + bab2b44 commit 827664c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Updated references to use proper converter to resolve enum string values

### Changed

Impoved handling of unexpected HTTP responses
Improved handling of unexpected HTTP responses

## 2.0.4

Expand All @@ -157,4 +157,10 @@ Updated nuget package dependencies
## Changed

- Move the enums back to the root ShipEngineSDK namespace
- Remove the autogenerated models and methods
- Remove the autogenerated models and methods

## 2.2.1

### Changed

Fixed handling of No Content responses
2 changes: 1 addition & 1 deletion ShipEngineSDK.Test/Helpers/MockShipEngineFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public string StubRequest(HttpMethod method, string path, HttpStatusCode status,
{
var requestId = Guid.NewGuid().ToString();
var responseMessage = new HttpResponseMessage(status);
responseMessage.Content = new StringContent(response);
responseMessage.Content = new StringContent(response ?? "");
responseMessage.Headers.Add("x-shipengine-requestid", requestId);
responseMessage.Headers.Add("request-id", requestId);

Expand Down
36 changes: 35 additions & 1 deletion ShipEngineSDK.Test/ShipEngineClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public async Task SuccessResponseThatCannotBeParsedThrowsExceptionWithUnparsedRe
}

[Fact]
public async Task SuccessResponseWithNullContentThrowsShipEngineExceptionWithUnparsedResponse()
public async Task SuccessResponseWithNullStringContentThrowsShipEngineExceptionWithUnparsedResponse()
{
var config = new Config(apiKey: "test", timeout: TimeSpan.FromSeconds(0.5));
var mockShipEngineFixture = new MockShipEngineFixture(config);
Expand All @@ -189,5 +189,39 @@ public async Task SuccessResponseWithNullContentThrowsShipEngineExceptionWithUnp
Assert.Equal(responseBody, await ex.ResponseMessage.Content.ReadAsStringAsync());
Assert.Equal(requestId, ex.RequestId);
}

[Fact]
public async Task SuccessResponseWhenStringRequestedReturnsUnparsedString()
{
var config = new Config(apiKey: "test", timeout: TimeSpan.FromSeconds(0.5));
var mockShipEngineFixture = new MockShipEngineFixture(config);
var shipengine = mockShipEngineFixture.ShipEngine;

// this scenario is similar to unparseable JSON - except that it is valid JSON
var responseBody = @"The Response";
mockShipEngineFixture.StubRequest(HttpMethod.Delete, "/v1/something", System.Net.HttpStatusCode.OK,
responseBody);
var result = await shipengine.SendHttpRequestAsync<string>(HttpMethod.Delete, "/v1/something", "",
mockShipEngineFixture.HttpClient, config);

Assert.Equal(responseBody, result);
}

[Fact]
public async Task SuccessResponseWithNoContentCanBeReturnedIfStringRequested()
{
var config = new Config(apiKey: "test", timeout: TimeSpan.FromSeconds(0.5));
var mockShipEngineFixture = new MockShipEngineFixture(config);
var shipengine = mockShipEngineFixture.ShipEngine;

// this scenario is similar to unparseable JSON - except that it is valid JSON
string responseBody = null;
mockShipEngineFixture.StubRequest(HttpMethod.Delete, "/v1/something", System.Net.HttpStatusCode.OK,
responseBody);
var result = await shipengine.SendHttpRequestAsync<string>(HttpMethod.Delete, "/v1/something", "",
mockShipEngineFixture.HttpClient, config);

Assert.Null(responseBody);
}
}
}
5 changes: 5 additions & 0 deletions ShipEngineSDK/ShipEngineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ private async Task<T> DeserializedResultOrThrow<T>(HttpResponseMessage response)
}

T? result;
// If the caller asked for a string, return the response as-is. This can be useful for no content responses.
if (typeof(T).IsAssignableFrom(typeof(string)))
{
return (T)(object)(contentString ?? "");
}
try
{
result = JsonSerializer.Deserialize<T>(contentString, JsonSerializerOptions);
Expand Down
2 changes: 1 addition & 1 deletion ShipEngineSDK/ShipEngineSDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>ShipEngine</PackageId>
<PackageTags>sdk;rest;api;shipping;rates;label;tracking;cost;address;validation;normalization;fedex;ups;usps;</PackageTags>

<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Authors>ShipEngine</Authors>
<Company>ShipEngine</Company>
<Summary>The official ShipEngine C# SDK for .NET</Summary>
Expand Down

0 comments on commit 827664c

Please sign in to comment.