Skip to content

Commit

Permalink
Version 1.6.2 (#87)
Browse files Browse the repository at this point in the history
* Add response headers in HttpException
* Add possibility to encapsulate HttpException automatically.
* Update RELEASE-NOTES.md
* Update stylecop
* remove useless affectation
* fix identation
* typo
* fix comments
* remove using
  • Loading branch information
jgiacomini authored May 4, 2019
1 parent 8e16d65 commit 3967167
Show file tree
Hide file tree
Showing 38 changed files with 385 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<!-- Add reference to StyleCop analyzers to all projects -->
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />

<!-- Common StyleCop configuration -->
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,28 @@ catch (HttpException ex) when (ex.StatusCode == System.Net.HttpStatusCode.Intern
throw new ServerErrorException($"{ex.Message} {ex.ReasonPhrase}");
}
```

### Enclapsulate HttpExceptions
We can setup a global handler to provide a logic to encapsulate HttpException automatically.

For example I can choose to translate all HttpException with StatusCode NotFound in a NotFoundCustomException.
```cs
client.Settings.EncapsulateHttpExceptionHandler = (ex) =>
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundCustomException();
}

return ex;
};
```

Now if I call an API wich respond with status code NotFound it will throw automaticaly my custom exception.
```cs
// Call an API wich throw NotFound error
await client.GetRequest("APIWhichNotExists").ExecuteAsync();
```
## ETag
The lib supports the Entity tag but it's not enabled by default.

Expand Down
25 changes: 25 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
# Release notes
## 1.6.2
* Now HttpException expose the headers of the response
* Constructor of HttpException is now internal
* Add possibility to encapsulate HttpException automatically.

We can setup a global handler to provide a logic to encapsulate HttpException automatically.
For example I can choose to translate all HttpException with StatusCode NotFound in a NotFoundCustomException.
```cs
client.Settings.EncapsulateHttpExceptionHandler = (ex) =>
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundCustomException();
}

return ex;
};
```

Now if I call an API wich respond with status code NotFound it will throw automaticaly my custom exception.
```cs
// Call an API wich throw NotFound error
await client.GetRequest("APIWhichNotExists").ExecuteAsync();
```

## 1.6.1
* Fix patch request which sent patch verb in lowercase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<string> One()
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var result = await reader.ReadToEndAsync();
var result = await reader.ReadToEndAsync();

return result.TrimEnd();
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tiny.RestClient.ForTest.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddResponseCompression(o =>
{
o.Providers.Add(new BrotliCompressionProvider());
o.Providers.Add(new CompressionProvider.BrotliCompressionProvider());
o.Providers.Add(new DeflateCompressionProvider());
o.EnableForHttps = true;
});
Expand Down
53 changes: 53 additions & 0 deletions Tests/Tiny.RestClient.Tests/HttpExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;

namespace Tiny.RestClient.Tests
{
[TestClass]
public class HttpExceptionTests : BaseTest
{
[TestMethod]
public async Task CheckIfHttpExceptionReadHeaders()
{
bool exceptionThrowed = false;
var client = GetClient();
try
{
// Call an api not found (the ETag header is present in all responses on this server)
await client.GetRequest("APIWhichNotExists").ExecuteAsync();
}
catch (HttpException ex)
{
exceptionThrowed = true;

Assert.IsTrue(ex.ResponseHeaders.Contains("ETag"), "An header name 'ETag' must be present in response.");
}

Assert.IsTrue(exceptionThrowed, $"An {nameof(HttpException)} must be throwed");
}

[ExpectedException(typeof(NotFoundCustomException))]
[TestMethod]
public async Task CheckIfEnclapsulationWorks()
{
var client = GetNewClient();
client.Settings.EncapsulateHttpExceptionHandler = (ex) =>
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundCustomException();
}

return ex;
};

// Call an API wich throw NotFound error
await client.GetRequest("APIWhichNotExists").ExecuteAsync();
}

internal class NotFoundCustomException : Exception
{
}
}
}
24 changes: 12 additions & 12 deletions Tiny.RestClient/Compression/Compressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Tiny.RestClient
{
/// <summary>
/// Represent headers of requests
/// Represent headers of requests.
/// </summary>
public class Compressions : IEnumerable<KeyValuePair<string, ICompression>>
{
Expand All @@ -16,9 +16,9 @@ internal Compressions()
}

/// <summary>
/// Add header
/// Add header.
/// </summary>
/// <param name="compression">header name</param>
/// <param name="compression">header name.</param>
public void Add(ICompression compression)
{
if (!_compressions.ContainsKey(compression.ContentEncoding))
Expand All @@ -32,30 +32,30 @@ public void Add(ICompression compression)
}

/// <summary>
/// Removes the compression
/// Removes the compression.
/// </summary>
/// <param name="compression">item to remove</param>
/// <param name="compression">item to remove.</param>
/// <returns></returns>
public bool Remove(ICompression compression)
{
return _compressions.Remove(compression.ContentEncoding);
}

/// <summary>
/// Determines whether the <see cref="Compressions"/> contains the specified compression system
/// Determines whether the <see cref="Compressions"/> contains the specified compression system.
/// </summary>
/// <param name="contentEncoding">content encoding</param>
/// <returns>returns true if contains an element with this contentEncoding otherwise false</returns>
/// <param name="contentEncoding">content encoding.</param>
/// <returns>returns true if contains an element with this contentEncoding otherwise false.</returns>
public bool Contains(string contentEncoding)
{
return _compressions.ContainsKey(contentEncoding);
}

/// <summary>
/// Gets or sets Compression system
/// Gets or sets Compression system.
/// </summary>
/// <param name="contentEncoding">content encoding key</param>
/// <returns>return compression system</returns>
/// <param name="contentEncoding">content encoding key.</param>
/// <returns>return compression system.</returns>
public ICompression this[string contentEncoding]
{
get
Expand All @@ -81,7 +81,7 @@ IEnumerator IEnumerable.GetEnumerator()

/// <summary>
/// Removes all <see cref="ICompression"/> system
/// /// </summary>
/// ///. </summary>
public void Clear()
{
_compressions.Clear();
Expand Down
2 changes: 1 addition & 1 deletion Tiny.RestClient/Compression/DeflateCompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Tiny.RestClient
{
/// <summary>
/// Gzip compression
/// Gzip compression.
/// </summary>
public class DeflateCompression : ICompression
{
Expand Down
2 changes: 1 addition & 1 deletion Tiny.RestClient/Compression/GzipCompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Tiny.RestClient
{
/// <summary>
/// Gzip compression
/// Gzip compression.
/// </summary>
public class GzipCompression : ICompression
{
Expand Down
22 changes: 11 additions & 11 deletions Tiny.RestClient/Compression/ICompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
namespace Tiny.RestClient
{
/// <summary>
/// Represent a way to manage compression / decompression
/// Represent a way to manage compression / decompression.
/// </summary>
public interface ICompression
{
/// <summary>
/// Gets the content encoding of compression
/// Gets the content encoding of compression.
/// </summary>
string ContentEncoding { get; }

/// <summary>
/// Gets or sets if the compression system add accept headers when the request is sended
/// Gets or sets if the compression system add accept headers when the request is sended.
/// </summary>
bool AddAcceptEncodingHeader { get; set; }

/// <summary>
/// Compresses the stream.
/// </summary>
/// <param name="stream">the stream to compress</param>
/// <param name="bufferSize">the buffer size to use</param>
/// <param name="cancellationToken">the cancellation token</param>
/// <returns>returns stream compressed</returns>
/// <param name="stream">the stream to compress.</param>
/// <param name="bufferSize">the buffer size to use.</param>
/// <param name="cancellationToken">the cancellation token.</param>
/// <returns>returns stream compressed.</returns>
Task<Stream> CompressAsync(Stream stream, int bufferSize, CancellationToken cancellationToken);

/// <summary>
/// Decompresses the stream.
/// </summary>
/// <param name="stream">the stream to decompress</param>
/// <param name="bufferSize">the buffer size to use</param>
/// <param name="cancellationToken">the cancellation token</param>
/// <returns>returns stream compressed</returns>
/// <param name="stream">the stream to decompress.</param>
/// <param name="bufferSize">the buffer size to use.</param>
/// <param name="cancellationToken">the cancellation token.</param>
/// <returns>returns stream compressed.</returns>
Task<Stream> DecompressAsync(Stream stream, int bufferSize, CancellationToken cancellationToken);
}
}
4 changes: 2 additions & 2 deletions Tiny.RestClient/ETag/ETagFileContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Tiny.RestClient
{
/// <summary>
/// Class <see cref="ETagFileContainer"/> which store data of entity in a directory
/// Class <see cref="ETagFileContainer"/> which store data of entity in a directory.
/// </summary>
public class ETagFileContainer : IETagContainer
{
Expand All @@ -19,7 +19,7 @@ public class ETagFileContainer : IETagContainer
/// <summary>
/// Initializes a new instance of the <see cref="ETagFileContainer"/> class.
/// </summary>
/// <param name="pathOfDirectoryContainer">the path of the directory which will store the data</param>
/// <param name="pathOfDirectoryContainer">the path of the directory which will store the data.</param>
public ETagFileContainer(string pathOfDirectoryContainer)
{
_pathOfDirectoryContainer = pathOfDirectoryContainer ?? throw new ArgumentNullException(nameof(pathOfDirectoryContainer));
Expand Down
18 changes: 9 additions & 9 deletions Tiny.RestClient/ETag/IETagContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
namespace Tiny.RestClient
{
/// <summary>
/// Entity Tag container
/// Entity Tag container.
/// </summary>
public interface IETagContainer
{
/// <summary>
/// Get the existing ETag.
/// </summary>
/// <param name="uri">the uri</param>
/// <param name="uri">the uri.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>return the etag if found. If not return null.</returns>
Task<string> GetExistingETagAsync(Uri uri, CancellationToken cancellationToken);

/// <summary>
/// Get data of specific uri
/// Get data of specific uri.
/// </summary>
/// <param name="uri">the uri</param>
/// <param name="uri">the uri.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>return the <see cref="Stream"/> of data</returns>
/// <returns>return the <see cref="Stream"/> of data.</returns>
Task<Stream> GetDataAsync(Uri uri, CancellationToken cancellationToken);

/// <summary>
/// S
/// S.
/// </summary>
/// <param name="uri">the uri</param>
/// <param name="etag">the etag of data</param>
/// <param name="stream"><see cref="Stream"/> of data to store</param>
/// <param name="uri">the uri.</param>
/// <param name="etag">the etag of data.</param>
/// <param name="stream"><see cref="Stream"/> of data to store.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task SaveDataAsync(Uri uri, string etag, Stream stream, CancellationToken cancellationToken);
Expand Down
19 changes: 15 additions & 4 deletions Tiny.RestClient/Exceptions/HttpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tiny.RestClient
{
/// <summary>
/// A <see cref="HttpException"/>
/// A <see cref="HttpException"/>.
/// </summary>
/// <seealso cref="TinyRestClientException" />
public class HttpException : TinyRestClientException
Expand All @@ -16,17 +16,19 @@ public class HttpException : TinyRestClientException
/// <param name="uri">The URL.</param>
/// <param name="verb">The verb.</param>
/// <param name="reasonPhrase">The reason phrase.</param>
/// <param name="headers">The headers of the request</param>
/// <param name="headers">The headers of the request.</param>
/// <param name="content">The content.</param>
/// <param name="statusCode">The status code.</param>
/// <param name="responseHeaders">The headers of response.</param>
/// <param name="ex">The ex.</param>
public HttpException(
internal HttpException(
Uri uri,
string verb,
string reasonPhrase,
HttpRequestHeaders headers,
string content,
HttpStatusCode statusCode,
HttpResponseHeaders responseHeaders,
Exception ex)
: base($"Response status code does not indicate success. Url : {uri.ToString()}, Verb : {verb}, StatusCode : {statusCode}, ReasonPhrase : {reasonPhrase}", ex)
{
Expand All @@ -36,6 +38,7 @@ public HttpException(
StatusCode = statusCode;
ReasonPhrase = reasonPhrase;
Headers = headers;
ResponseHeaders = responseHeaders;
}

/// <summary>
Expand All @@ -47,7 +50,7 @@ public HttpException(
public string Verb { get; private set; }

/// <summary>
/// Gets the headers of sended request
/// Gets the headers of sended request.
/// </summary>
/// <value>
/// The verb.
Expand Down Expand Up @@ -78,6 +81,14 @@ public HttpException(
/// </value>
public string Content { get; private set; }

/// <summary>
/// Gets the response headers of sended request.
/// </summary>
/// <value>
/// The verb.
/// </value>
public HttpResponseHeaders ResponseHeaders { get; private set; }

/// <summary>
/// Gets the status code.
/// </summary>
Expand Down
Loading

0 comments on commit 3967167

Please sign in to comment.