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

able to pass header per request from SMAPI graphql client #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion Agoda.Frameworks.Http.Tests/RandomUrlHttpClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -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<string, string>{ {"test", "test"}});
var content = await res.Content.ReadAsStringAsync();

Assert.AreEqual("ok", content);
Expand Down
26 changes: 26 additions & 0 deletions Agoda.Frameworks.Http/RandomUrlHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public void Dispose()

public Task<HttpResponseMessage> GetAsync(string url) =>
SendAsync(url, (uri, cxlToken) => HttpClient.GetAsync(uri, cxlToken));
public Task<HttpResponseMessage> GetAsync(string url, Dictionary<string, string> headers) =>
SendAsync(url, (uri, cxlToken) => HttpClient.SendAsync(AddHeaders(new HttpRequestMessage(HttpMethod.Get, uri), headers), cxlToken));

#if !NET462
public Task<HttpResponseMessage> PostAsync(string url, HttpContent content) =>
Expand All @@ -104,12 +106,36 @@ public Task<HttpResponseMessage> PutAsync(string url, HttpContent content) =>
#endif
public Task<HttpResponseMessage> PostJsonAsync(string url, string json) =>
SendAsync(url, (uri, cxlToken) => HttpClient.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), cxlToken));

public Task<HttpResponseMessage> PostJsonAsync(string url, string json, Dictionary<string, string> 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<HttpResponseMessage> PutJsonAsync(string url, string json) =>
SendAsync(url, (uri, cxlToken) => HttpClient.PutAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), cxlToken));

public Task<HttpResponseMessage> PutJsonAsync(string url, string json, Dictionary<string, string> 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<HttpResponseMessage> DeleteAsync(string url) =>
SendAsync(url, (uri, cxlToken) => HttpClient.DeleteAsync(uri, cxlToken));

public Task<HttpResponseMessage> DeleteAsync(string url, Dictionary<string, string> headers) =>
SendAsync(url, (uri, cxlToken) => HttpClient.SendAsync(AddHeaders(new HttpRequestMessage(HttpMethod.Delete, uri), headers), cxlToken));

private HttpRequestMessage AddHeaders(HttpRequestMessage requestMessage, Dictionary<string, string> headers = null)
{
Expand Down