Skip to content

Commit e585eb2

Browse files
authored
Merge pull request #103 from babrekel/httpclientfactory
Allow an HttpClientFactory to be assigned to OpenAIAPI
2 parents 6661723 + 3633e68 commit e585eb2

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

OpenAI_API/EndpointBase.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ protected HttpClient GetClient()
5959
{
6060
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details.");
6161
}
62-
63-
/*
64-
if (_Api.SharedHttpClient==null)
62+
63+
HttpClient client;
64+
var clientFactory = _Api.HttpClientFactory;
65+
if (clientFactory != null)
66+
{
67+
client = clientFactory.CreateClient();
68+
}
69+
else
6570
{
66-
_Api.SharedHttpClient = new HttpClient();
67-
_Api.SharedHttpClient.
71+
client = new HttpClient();
6872
}
69-
*/
7073

71-
HttpClient client = new HttpClient();
7274
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Api.Auth.ApiKey);
7375
// Further authentication-header used for Azure openAI service
7476
client.DefaultRequestHeaders.Add("api-key", _Api.Auth.ApiKey);

OpenAI_API/OpenAIAPI.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using OpenAI_API.Images;
66
using OpenAI_API.Models;
77
using OpenAI_API.Moderation;
8-
using System.Xml.Linq;
8+
using System.Net.Http;
99

1010
namespace OpenAI_API
1111
{
@@ -31,6 +31,11 @@ public class OpenAIAPI : IOpenAIAPI
3131
/// </summary>
3232
public APIAuthentication Auth { get; set; }
3333

34+
/// <summary>
35+
/// Optionally provide an IHttpClientFactory to create the client to send requests.
36+
/// </summary>
37+
public IHttpClientFactory HttpClientFactory { get; set; }
38+
3439
/// <summary>
3540
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
3641
/// </summary>
@@ -96,6 +101,5 @@ public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, A
96101
/// The API lets you do operations with images. You can Given a prompt and/or an input image, the model will generate a new image.
97102
/// </summary>
98103
public ImageGenerationEndpoint ImageGenerations { get; }
99-
100104
}
101105
}

OpenAI_API/OpenAI_API.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@
4242
</None>
4343
</ItemGroup>
4444

45+
<ItemGroup>
46+
<InternalsVisibleTo Include="OpenAI_Tests" />
47+
</ItemGroup>
48+
4549
<ItemGroup>
4650
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
4751
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
4852
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
53+
<PackageReference Include="Microsoft.Extensions.Http" Version="1.1.1" />
4954
</ItemGroup>
5055

5156
</Project>
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Microsoft.Extensions.Options;
2+
using Moq;
3+
using NUnit.Framework;
4+
using OpenAI_API;
5+
using System;
6+
using System.Linq;
7+
using System.Net.Http;
8+
9+
namespace OpenAI_Tests
10+
{
11+
public class HttpClientResolutionTests
12+
{
13+
[Test]
14+
public void GetHttpClient_NoFactory()
15+
{
16+
var api = new OpenAIAPI(new APIAuthentication("fake-key"));
17+
var endpoint = new TestEndpoint(api);
18+
19+
var client = endpoint.GetHttpClient();
20+
Assert.IsNotNull(client);
21+
}
22+
23+
[Test]
24+
public void GetHttpClient_WithFactory()
25+
{
26+
var expectedClient1 = new HttpClient();
27+
var mockedFactory1 = Mock.Of<IHttpClientFactory>(f => f.CreateClient(Options.DefaultName) == expectedClient1);
28+
29+
var expectedClient2 = new HttpClient();
30+
var mockedFactory2 = Mock.Of<IHttpClientFactory>(f => f.CreateClient(Options.DefaultName) == expectedClient2);
31+
32+
var api = new OpenAIAPI(new APIAuthentication("fake-key"));
33+
var endpoint = new TestEndpoint(api);
34+
35+
api.HttpClientFactory = mockedFactory1;
36+
var actualClient1 = endpoint.GetHttpClient();
37+
38+
api.HttpClientFactory = mockedFactory2;
39+
var actualClient2 = endpoint.GetHttpClient();
40+
41+
Assert.AreSame(expectedClient1, actualClient1);
42+
Assert.AreSame(expectedClient2, actualClient2);
43+
44+
api.HttpClientFactory = null;
45+
var actualClient3 = endpoint.GetHttpClient();
46+
47+
Assert.NotNull(actualClient3);
48+
Assert.AreNotSame(expectedClient1, actualClient3);
49+
Assert.AreNotSame(expectedClient2, actualClient3);
50+
}
51+
52+
private class TestEndpoint : EndpointBase
53+
{
54+
public TestEndpoint(OpenAIAPI api) : base(api)
55+
{
56+
}
57+
58+
protected override string Endpoint => throw new System.NotSupportedException();
59+
60+
public HttpClient GetHttpClient()
61+
{
62+
return base.GetClient();
63+
}
64+
}
65+
}
66+
}

OpenAI_Tests/OpenAI_Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="FluentAssertions" Version="6.9.0" />
11+
<PackageReference Include="Moq" Version="4.18.4" />
1112
<PackageReference Include="nunit" Version="3.13.3" />
1213
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />

0 commit comments

Comments
 (0)