Skip to content

Commit c0989c4

Browse files
Improves using HttpClient
1 parent a05e85d commit c0989c4

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

dev-proxy-abstractions/LanguageModel/OllamaLanguageModelClient.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OllamaLanguageModelClient(LanguageModelConfiguration? configuration
1212
{
1313
private readonly LanguageModelConfiguration? _configuration = configuration;
1414
private readonly ILogger _logger = logger;
15+
private readonly HttpClient _httpClient = new();
1516
private bool? _lmAvailable;
1617
private readonly Dictionary<string, OllamaLanguageModelCompletionResponse> _cacheCompletion = [];
1718
private readonly Dictionary<ILanguageModelChatCompletionMessage[], OllamaLanguageModelChatCompletionResponse> _cacheChatCompletion = [];
@@ -119,11 +120,10 @@ private async Task<bool> IsEnabledInternalAsync()
119120

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

126-
var response = await client.PostAsJsonAsync(url,
126+
var response = await _httpClient.PostAsJsonAsync(url,
127127
new
128128
{
129129
prompt,
@@ -213,11 +213,10 @@ private async Task<bool> IsEnabledInternalAsync()
213213

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

220-
var response = await client.PostAsJsonAsync(url,
219+
var response = await _httpClient.PostAsJsonAsync(url,
221220
new
222221
{
223222
messages,

dev-proxy-abstractions/LanguageModel/OpenAILanguageModelClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OpenAILanguageModelClient(LanguageModelConfiguration? configuration
1212
{
1313
private readonly LanguageModelConfiguration? _configuration = configuration;
1414
private readonly ILogger _logger = logger;
15+
private readonly HttpClient _httpClient = new();
1516
private bool? _lmAvailable;
1617
private readonly Dictionary<ILanguageModelChatCompletionMessage[], OpenAIChatCompletionResponse> _cacheChatCompletion = [];
1718

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

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

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

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

176176
if (!response.IsSuccessStatusCode)

dev-proxy-plugins/GraphUtils.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace DevProxy.Plugins;
1111

1212
public class GraphUtils
1313
{
14+
private static readonly HttpClient _httpClient = new();
15+
1416
// throttle requests per workload
1517
public static string BuildThrottleKey(Request r) => BuildThrottleKey(r.RequestUri);
1618

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

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

dev-proxy-plugins/Mocks/MockRequestPlugin.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class MockRequestPlugin(IPluginEvents pluginEvents, IProxyContext context
2323
{
2424
protected MockRequestConfiguration _configuration = new();
2525
private MockRequestLoader? _loader = null;
26+
private readonly HttpClient _httpClient = new();
2627

2728
public override string Name => nameof(MockRequestPlugin);
2829

@@ -95,14 +96,13 @@ protected virtual async Task OnMockRequestAsync(object sender, EventArgs e)
9596
return;
9697
}
9798

98-
using var httpClient = new HttpClient();
9999
var requestMessage = GetRequestMessage();
100100

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

105-
await httpClient.SendAsync(requestMessage);
105+
await _httpClient.SendAsync(requestMessage);
106106
}
107107
catch (Exception ex)
108108
{

0 commit comments

Comments
 (0)