Skip to content

Improves using HttpClient #1168

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 3 commits into from
May 13, 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 @@ -12,6 +12,7 @@ public class OllamaLanguageModelClient(LanguageModelConfiguration? configuration
{
private readonly LanguageModelConfiguration? _configuration = configuration;
private readonly ILogger _logger = logger;
private readonly HttpClient _httpClient = new();
private bool? _lmAvailable;
private readonly Dictionary<string, OllamaLanguageModelCompletionResponse> _cacheCompletion = [];
private readonly Dictionary<ILanguageModelChatCompletionMessage[], OllamaLanguageModelChatCompletionResponse> _cacheChatCompletion = [];
Expand Down Expand Up @@ -119,11 +120,10 @@ private async Task<bool> IsEnabledInternalAsync()

try
{
using var client = new HttpClient();
var url = $"{_configuration.Url}/api/generate";
_logger.LogDebug("Requesting completion. Prompt: {prompt}", prompt);

var response = await client.PostAsJsonAsync(url,
var response = await _httpClient.PostAsJsonAsync(url,
new
{
prompt,
Expand Down Expand Up @@ -213,11 +213,10 @@ private async Task<bool> IsEnabledInternalAsync()

try
{
using var client = new HttpClient();
var url = $"{_configuration.Url}/api/chat";
_logger.LogDebug("Requesting chat completion. Message: {lastMessage}", messages.Last().Content);

var response = await client.PostAsJsonAsync(url,
var response = await _httpClient.PostAsJsonAsync(url,
new
{
messages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class OpenAILanguageModelClient(LanguageModelConfiguration? configuration
{
private readonly LanguageModelConfiguration? _configuration = configuration;
private readonly ILogger _logger = logger;
private readonly HttpClient _httpClient = new();
private bool? _lmAvailable;
private readonly Dictionary<ILanguageModelChatCompletionMessage[], OpenAIChatCompletionResponse> _cacheChatCompletion = [];

Expand Down Expand Up @@ -158,7 +159,6 @@ private async Task<bool> IsEnabledInternalAsync()

try
{
using var client = new HttpClient();
var url = $"{_configuration.Url}/chat/completions";
_logger.LogDebug("Requesting chat completion. Message: {lastMessage}", messages.Last().Content);

Expand All @@ -170,7 +170,7 @@ private async Task<bool> IsEnabledInternalAsync()
Temperature = options?.Temperature
};

var response = await client.PostAsJsonAsync(url, payload);
var response = await _httpClient.PostAsJsonAsync(url, payload);
_logger.LogDebug("Response: {response}", response.StatusCode);

if (!response.IsSuccessStatusCode)
Expand Down
5 changes: 3 additions & 2 deletions dev-proxy-plugins/GraphUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace DevProxy.Plugins;

public class GraphUtils
{
private static readonly HttpClient _httpClient = new();

// throttle requests per workload
public static string BuildThrottleKey(Request r) => BuildThrottleKey(r.RequestUri);

Expand Down Expand Up @@ -53,14 +55,13 @@ internal static async Task<IEnumerable<string>> UpdateUserScopesAsync(IEnumerabl
var newMinimalScopes = new HashSet<string>(minimalScopes);

var url = $"https://devxapi-func-prod-eastus.azurewebsites.net/permissions?scopeType={GetScopeTypeString(permissionsType)}";
using var httpClient = new HttpClient();
var urls = userEndpoints.Select(e => {
logger.LogDebug("Getting permissions for {method} {url}", e.method, e.url);
return $"{url}&requesturl={e.url}&method={e.method}";
});
var tasks = urls.Select(u => {
logger.LogTrace("Calling {url}...", u);
return httpClient.GetFromJsonAsync<GraphPermissionInfo[]>(u);
return _httpClient.GetFromJsonAsync<GraphPermissionInfo[]>(u);
});
await Task.WhenAll(tasks);

Expand Down
4 changes: 2 additions & 2 deletions dev-proxy-plugins/Mocks/MockRequestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class MockRequestPlugin(IPluginEvents pluginEvents, IProxyContext context
{
protected MockRequestConfiguration _configuration = new();
private MockRequestLoader? _loader = null;
private readonly HttpClient _httpClient = new();

public override string Name => nameof(MockRequestPlugin);

Expand Down Expand Up @@ -95,14 +96,13 @@ protected virtual async Task OnMockRequestAsync(object sender, EventArgs e)
return;
}

using var httpClient = new HttpClient();
var requestMessage = GetRequestMessage();

try
{
Logger.LogRequest("Sending mock request", MessageType.Mocked, _configuration.Request.Method, _configuration.Request.Url);

await httpClient.SendAsync(requestMessage);
await _httpClient.SendAsync(requestMessage);
}
catch (Exception ex)
{
Expand Down