Skip to content

refine http headers #1062

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

Merged
merged 1 commit into from
May 23, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Http;

public interface IHttpRequestHook
{
void OnAddHttpHeaders(HttpHeaders headers);
void OnAddHttpHeaders(HttpHeaders headers, Uri uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public async Task<bool> Execute(RoleDialogModel message)
if (string.IsNullOrEmpty(url)) return null;

using var client = _httpClientFactory.CreateClient();
PrepareRequestHeaders(client);


var (uri, request) = BuildHttpRequest(url, method, content);
PrepareRequestHeaders(client, uri);

var response = await client.SendAsync(request);
if (response == null || !response.IsSuccessStatusCode)
{
Expand All @@ -68,12 +69,12 @@ public async Task<bool> Execute(RoleDialogModel message)
return response;
}

private void PrepareRequestHeaders(HttpClient client)
private void PrepareRequestHeaders(HttpClient client, Uri uri)
{
var hooks = _services.GetServices<IHttpRequestHook>();
foreach (var hook in hooks)
{
hook.OnAddHttpHeaders(client.DefaultRequestHeaders);
hook.OnAddHttpHeaders(client.DefaultRequestHeaders, uri);
}
}

Expand Down Expand Up @@ -115,20 +116,20 @@ private HttpMethod GetHttpMethod(string? method)

switch (localMethod)
{
case "GET":
matchMethod = HttpMethod.Get;
case "POST":
matchMethod = HttpMethod.Post;
break;
case "DELETE":
matchMethod = HttpMethod.Delete;
break;
case "PUT":
matchMethod = HttpMethod.Put;
break;
case "Patch":
case "PATCH":
matchMethod = HttpMethod.Patch;
break;
default:
matchMethod = HttpMethod.Post;
matchMethod = HttpMethod.Get;
break;
}
return matchMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public BasicHttpRequestHook(
_context = context;
}

public void OnAddHttpHeaders(HttpHeaders headers)
public void OnAddHttpHeaders(HttpHeaders headers, Uri uri)
{
var settings = _services.GetRequiredService<HttpHandlerSettings>();

Expand Down
Loading