Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.6.3 #91

Merged
merged 61 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
79093d5
Add response headers in HttpException
May 4, 2019
e5252f8
Update RELEASE-NOTES.md
jgiacomini May 4, 2019
c3444d5
Update stylecop
May 4, 2019
020660f
Merge branch 'features/AddResponseHeaderInHttpException' of https://g…
May 4, 2019
9946546
Update nuget packages
May 4, 2019
46a5666
small fixes
May 4, 2019
6b6cf16
remove useless affectation
May 4, 2019
dfe5d36
fix identation
May 4, 2019
0265c33
fix warning
May 4, 2019
9a5d461
Add new tests
May 4, 2019
eec36b8
downgrad nuget package for unit test work
May 4, 2019
9b00f04
downgrad version of .netcore in tests
May 4, 2019
60b6e70
change version of nugets on test project
May 4, 2019
eae3a12
Fix tests
May 4, 2019
19cab9d
small change
May 4, 2019
551f784
typo
jgiacomini May 4, 2019
bc8396f
Merge pull request #83 from jgiacomini/features/AddResponseHeaderInHt…
jgiacomini May 4, 2019
2bb6ea8
fixComment
jgiacomini May 4, 2019
7e3cd27
fix comment
jgiacomini May 4, 2019
bbc5c58
fixComment
jgiacomini May 4, 2019
6475366
fix comment
jgiacomini May 4, 2019
e1f7723
fix comment
jgiacomini May 4, 2019
358e6e2
fix comment
jgiacomini May 4, 2019
716ea14
fix comment
jgiacomini May 4, 2019
6c436b5
fix comment
jgiacomini May 4, 2019
b56a31a
fix comment
jgiacomini May 4, 2019
f80117d
fix comment
jgiacomini May 4, 2019
20ff5ad
fix comment
jgiacomini May 4, 2019
290226c
fix comment
jgiacomini May 4, 2019
10975b4
fix comment
jgiacomini May 4, 2019
63a1d1f
fix comment
jgiacomini May 4, 2019
d895104
fix comment
jgiacomini May 4, 2019
9970b7d
fix comment
jgiacomini May 4, 2019
941c32a
fix comment
jgiacomini May 4, 2019
d02fc5f
fix comment
jgiacomini May 4, 2019
404c806
fix comment
jgiacomini May 4, 2019
4884dea
fix comment
jgiacomini May 4, 2019
e63930f
fix comment
jgiacomini May 4, 2019
76e3d84
fix comment
jgiacomini May 4, 2019
c41eaef
fix comment
jgiacomini May 4, 2019
63a5c07
fix comment
jgiacomini May 4, 2019
e5d17c6
fix comment
jgiacomini May 4, 2019
a17503c
fix comment
jgiacomini May 4, 2019
2eae907
fix comment
jgiacomini May 4, 2019
ea89c79
fix comment
jgiacomini May 4, 2019
29b6e9f
fix comment
jgiacomini May 4, 2019
da904d2
fix comment
jgiacomini May 4, 2019
8d74c64
fix comment
jgiacomini May 4, 2019
a933f9b
Merge pull request #84 from jgiacomini/features/fixComments
jgiacomini May 4, 2019
4ab6e02
Encapsulate exception if needed
jgiacomini May 4, 2019
4585c48
Add test
jgiacomini May 4, 2019
a978216
fix comment
jgiacomini May 4, 2019
b682714
Update README.md
jgiacomini May 4, 2019
a73ce3b
Update RELEASE-NOTES.md
jgiacomini May 4, 2019
b05b550
Merge pull request #85 from jgiacomini/features/addNewWayToHandleExce…
jgiacomini May 4, 2019
bd448ea
remove using
jgiacomini May 4, 2019
989e70a
Merge pull request #86 from jgiacomini/features/addNewWayToHandleExce…
jgiacomini May 4, 2019
b43bd89
Update Tiny.RestClient.csproj
jgiacomini May 4, 2019
a166f91
Update README.md
jgiacomini May 21, 2019
8e9276a
Calculate headers before send request (#90)
jgiacomini May 21, 2019
94c4919
merge
jgiacomini May 21, 2019
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
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.