Skip to content

Commit

Permalink
Version 1.6.3 (#91)
Browse files Browse the repository at this point in the history
* Add response headers in HttpException

* Calculate headers before send request (#90)
  • Loading branch information
jgiacomini authored May 21, 2019
1 parent 3967167 commit f2a3d10
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ client.GetRequest("City/All").
ExecuteAsync();
```


#### Calculate headers before send the requests

Before send requests to server we can add calculate dynamically the headers to add to resquest like below :
```cs
client.Settings.CalculateHeadersHandler = async () =>
{
var token = await GetACustomTokenAsync();

var headers = new Headers
{
{ "CustomToken", token },
};
return headers;
};
```

#### Read headers of response

```cs
Expand All @@ -108,6 +125,7 @@ foreach(var header in headersOfResponse)
}
```


### Basic GET http requests

```cs
Expand Down
17 changes: 17 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# Release notes
# 1.6.3
* Now calculate dynamically the headers to add to resquest.

In this sample we get a custom token and add it to all our requests =>
```cs
client.Settings.CalculateHeadersHandler = async () =>
{
var token = await GetACustomTokenAsync();

var headers = new Headers
{
{ "CustomToken", token },
};
return headers;
};
```

## 1.6.2
* Now HttpException expose the headers of the response
* Constructor of HttpException is now internal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Tiny.RestClient.ForTest.Api.Controllers
{
[Route("api/HeadersTest")]
[ApiController]
public class HeadersTestController : ControllerBase
{
[HttpGet("NoResponse")]
public Task NoResponse()
{
foreach (var header in Request.Headers)
{
Response.Headers.Add("FROM_CLIENT" + header.Key, header.Value);
}

return Task.Delay(1);
}
}
}
43 changes: 43 additions & 0 deletions Tests/Tiny.RestClient.Tests/HeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tiny.RestClient.Tests
{
[TestClass]
public class HeaderTests : BaseTest
{
[TestMethod]
public async Task CalculateHeadersTest()
{
var client = GetNewClient();

var headers = new Headers
{
{ "Header1", "Value1" },
{ "Header2", "Value2" },
{ "Header3", "Value3" }
};

client.Settings.CalculateHeadersHandler = () =>
{
return Task.FromResult(headers);
};

await client.
GetRequest("HeadersTest/NoResponse").
FillResponseHeaders(out Headers responseHeaders).
ExecuteAsync();

foreach (var sendedHeader in headers)
{
Assert.IsTrue(responseHeaders.Any(h => h.Key == "FROM_CLIENT" + sendedHeader.Key), $"{sendedHeader.Key} seem not passed to server side");
var responseHeader = responseHeaders.FirstOrDefault(h => h.Key == "FROM_CLIENT" + sendedHeader.Key);
Assert.IsTrue(sendedHeader.Value.First() == responseHeader.Value.First(), $"Values for header {sendedHeader.Key} seem not match with values resend by server");
}
}
}
}
5 changes: 4 additions & 1 deletion Tiny.RestClient/Request/Headers/Headers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class Headers : IEnumerable<KeyValuePair<string, IEnumerable<string>>>
{
private Dictionary<string, IEnumerable<string>> _headers;

internal Headers()
/// <summary>
/// Initializes a new instance of the <see cref="Headers"/> class.
/// </summary>
public Headers()
{
_headers = new Dictionary<string, IEnumerable<string>>();
}
Expand Down
6 changes: 6 additions & 0 deletions Tiny.RestClient/RestClientSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Threading.Tasks;

namespace Tiny.RestClient
{
Expand Down Expand Up @@ -65,6 +66,11 @@ public Headers DefaultHeaders
get; private set;
}

/// <summary>
/// Gets or set the handler used to calculate headers before send request.
/// </summary>
public Func<Task<Headers>> CalculateHeadersHandler { get; set; }

/// <summary>
/// Log all requests.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Tiny.RestClient/Tiny.RestClient.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.1;netstandard1.2;netstandard1.3;netstandard2.0;net45;net46;net47</TargetFrameworks>
<Version>1.6.2</Version>
<Version>1.6.3</Version>
<Copyright>Copyright © Jérôme Giacomini 2019</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<Title>Tiny.RestClient</Title>
Expand Down Expand Up @@ -36,7 +36,7 @@
<RepositoryUrl>https://github.com/jgiacomini/Tiny.RestClient.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<MinClientVersion>3.0.3</MinClientVersion>
<PackageReleaseNotes>See release notes at https://github.com/jgiacomini/Tiny.RestClient/blob/1.6.2/RELEASE-NOTES.md</PackageReleaseNotes>
<PackageReleaseNotes>See release notes at https://github.com/jgiacomini/Tiny.RestClient/blob/1.6.3/RELEASE-NOTES.md</PackageReleaseNotes>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand Down
15 changes: 15 additions & 0 deletions Tiny.RestClient/TinyRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@ private async Task<HttpResponseMessage> SendRequestAsync(
stopwatch = new Stopwatch();
}

Headers calculatedHeaders = null;
var calculateHeadersHandler = Settings.CalculateHeadersHandler;
if (calculateHeadersHandler != null)
{
calculatedHeaders = await calculateHeadersHandler.Invoke();
}

using (var request = new HttpRequestMessage(httpMethod, uri))
{
if (deserializer == null)
Expand Down Expand Up @@ -625,6 +632,14 @@ private async Task<HttpResponseMessage> SendRequestAsync(
}
}

if (calculatedHeaders != null)
{
foreach (var item in calculatedHeaders)
{
request.Headers.Add(item.Key, item.Value);
}
}

if (eTagContainer != null)
{
if (!request.Headers.IfNoneMatch.Any())
Expand Down
Binary file not shown.

0 comments on commit f2a3d10

Please sign in to comment.