From bb1e5666403474d2371d31edcd4a24602003dcb2 Mon Sep 17 00:00:00 2001 From: Thanetpon Kultontikorn Date: Thu, 2 Mar 2023 17:59:28 +0700 Subject: [PATCH 1/2] fix --- .../RandomUrlHttpClientTest.cs | 3 ++- Agoda.Frameworks.Http/RandomUrlHttpClient.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Agoda.Frameworks.Http.Tests/RandomUrlHttpClientTest.cs b/Agoda.Frameworks.Http.Tests/RandomUrlHttpClientTest.cs index 64e9821..28ba65f 100644 --- a/Agoda.Frameworks.Http.Tests/RandomUrlHttpClientTest.cs +++ b/Agoda.Frameworks.Http.Tests/RandomUrlHttpClientTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; @@ -105,7 +106,7 @@ public async Task TestPostJsonAsync() null, 3, null); - var res = await client.PostJsonAsync("api/55", "{\"foo\":\"bar\"}"); + var res = await client.PostJsonAsync("api/55", "{\"foo\":\"bar\"}", new Dictionary{ {"test", "test"}}); var content = await res.Content.ReadAsStringAsync(); Assert.AreEqual("ok", content); diff --git a/Agoda.Frameworks.Http/RandomUrlHttpClient.cs b/Agoda.Frameworks.Http/RandomUrlHttpClient.cs index 374e6ac..9754730 100644 --- a/Agoda.Frameworks.Http/RandomUrlHttpClient.cs +++ b/Agoda.Frameworks.Http/RandomUrlHttpClient.cs @@ -104,6 +104,17 @@ public void Dispose() #endif public Task PostJsonAsync(string url, string json) => SendAsync(url, (uri, cxlToken) => HttpClient.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), cxlToken)); + + public Task PostJsonAsync(string url, string json, Dictionary headers) => + SendAsync(url, (uri, cxlToken) => + { + var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri) + { + Content = new StringContent(json, Encoding.UTF8, "application/json") + }; + return HttpClient.SendAsync(AddHeaders(requestMessage, headers), cxlToken); + }); + public Task PutJsonAsync(string url, string json) => SendAsync(url, (uri, cxlToken) => HttpClient.PutAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), cxlToken)); From 867bf8bcb704268f6d3c34f69b917d8a8871ca86 Mon Sep 17 00:00:00 2001 From: Thanetpon Kultontikorn Date: Fri, 3 Mar 2023 10:19:15 +0700 Subject: [PATCH 2/2] add ability for the others --- Agoda.Frameworks.Http/RandomUrlHttpClient.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Agoda.Frameworks.Http/RandomUrlHttpClient.cs b/Agoda.Frameworks.Http/RandomUrlHttpClient.cs index 9754730..1409c06 100644 --- a/Agoda.Frameworks.Http/RandomUrlHttpClient.cs +++ b/Agoda.Frameworks.Http/RandomUrlHttpClient.cs @@ -94,6 +94,8 @@ public void Dispose() public Task GetAsync(string url) => SendAsync(url, (uri, cxlToken) => HttpClient.GetAsync(uri, cxlToken)); + public Task GetAsync(string url, Dictionary headers) => + SendAsync(url, (uri, cxlToken) => HttpClient.SendAsync(AddHeaders(new HttpRequestMessage(HttpMethod.Get, uri), headers), cxlToken)); #if !NET462 public Task PostAsync(string url, HttpContent content) => @@ -118,9 +120,22 @@ public void Dispose() public Task PutJsonAsync(string url, string json) => SendAsync(url, (uri, cxlToken) => HttpClient.PutAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), cxlToken)); + + public Task PutJsonAsync(string url, string json, Dictionary headers) => + SendAsync(url, (uri, cxlToken) => + { + var requestMessage = new HttpRequestMessage(HttpMethod.Put, uri) + { + Content = new StringContent(json, Encoding.UTF8, "application/json") + }; + return HttpClient.SendAsync(AddHeaders(requestMessage, headers), cxlToken); + }); public Task DeleteAsync(string url) => SendAsync(url, (uri, cxlToken) => HttpClient.DeleteAsync(uri, cxlToken)); + + public Task DeleteAsync(string url, Dictionary headers) => + SendAsync(url, (uri, cxlToken) => HttpClient.SendAsync(AddHeaders(new HttpRequestMessage(HttpMethod.Delete, uri), headers), cxlToken)); private HttpRequestMessage AddHeaders(HttpRequestMessage requestMessage, Dictionary headers = null) {