Skip to content

Commit

Permalink
Merge pull request #103 from babrekel/httpclientfactory
Browse files Browse the repository at this point in the history
Allow an HttpClientFactory to be assigned to OpenAIAPI
  • Loading branch information
OkGoDoIt committed Apr 2, 2023
2 parents 6661723 + 3633e68 commit e585eb2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
16 changes: 9 additions & 7 deletions OpenAI_API/EndpointBase.cs
Expand Up @@ -59,16 +59,18 @@ protected HttpClient GetClient()
{
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details.");
}

/*
if (_Api.SharedHttpClient==null)

HttpClient client;
var clientFactory = _Api.HttpClientFactory;
if (clientFactory != null)
{
client = clientFactory.CreateClient();
}
else
{
_Api.SharedHttpClient = new HttpClient();
_Api.SharedHttpClient.
client = new HttpClient();
}
*/

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Api.Auth.ApiKey);
// Further authentication-header used for Azure openAI service
client.DefaultRequestHeaders.Add("api-key", _Api.Auth.ApiKey);
Expand Down
8 changes: 6 additions & 2 deletions OpenAI_API/OpenAIAPI.cs
Expand Up @@ -5,7 +5,7 @@
using OpenAI_API.Images;
using OpenAI_API.Models;
using OpenAI_API.Moderation;
using System.Xml.Linq;
using System.Net.Http;

namespace OpenAI_API
{
Expand All @@ -31,6 +31,11 @@ public class OpenAIAPI : IOpenAIAPI
/// </summary>
public APIAuthentication Auth { get; set; }

/// <summary>
/// Optionally provide an IHttpClientFactory to create the client to send requests.
/// </summary>
public IHttpClientFactory HttpClientFactory { get; set; }

/// <summary>
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
/// </summary>
Expand Down Expand Up @@ -96,6 +101,5 @@ public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, A
/// 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.
/// </summary>
public ImageGenerationEndpoint ImageGenerations { get; }

}
}
5 changes: 5 additions & 0 deletions OpenAI_API/OpenAI_API.csproj
Expand Up @@ -42,10 +42,15 @@
</None>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="OpenAI_Tests" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Http" Version="1.1.1" />
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions OpenAI_Tests/HttpClientResolutionTests.cs
@@ -0,0 +1,66 @@
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using OpenAI_API;
using System;
using System.Linq;
using System.Net.Http;

namespace OpenAI_Tests
{
public class HttpClientResolutionTests
{
[Test]
public void GetHttpClient_NoFactory()
{
var api = new OpenAIAPI(new APIAuthentication("fake-key"));
var endpoint = new TestEndpoint(api);

var client = endpoint.GetHttpClient();
Assert.IsNotNull(client);
}

[Test]
public void GetHttpClient_WithFactory()
{
var expectedClient1 = new HttpClient();
var mockedFactory1 = Mock.Of<IHttpClientFactory>(f => f.CreateClient(Options.DefaultName) == expectedClient1);

var expectedClient2 = new HttpClient();
var mockedFactory2 = Mock.Of<IHttpClientFactory>(f => f.CreateClient(Options.DefaultName) == expectedClient2);

var api = new OpenAIAPI(new APIAuthentication("fake-key"));
var endpoint = new TestEndpoint(api);

api.HttpClientFactory = mockedFactory1;
var actualClient1 = endpoint.GetHttpClient();

api.HttpClientFactory = mockedFactory2;
var actualClient2 = endpoint.GetHttpClient();

Assert.AreSame(expectedClient1, actualClient1);
Assert.AreSame(expectedClient2, actualClient2);

api.HttpClientFactory = null;
var actualClient3 = endpoint.GetHttpClient();

Assert.NotNull(actualClient3);
Assert.AreNotSame(expectedClient1, actualClient3);
Assert.AreNotSame(expectedClient2, actualClient3);
}

private class TestEndpoint : EndpointBase
{
public TestEndpoint(OpenAIAPI api) : base(api)
{
}

protected override string Endpoint => throw new System.NotSupportedException();

public HttpClient GetHttpClient()
{
return base.GetClient();
}
}
}
}
1 change: 1 addition & 0 deletions OpenAI_Tests/OpenAI_Tests.csproj
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
Expand Down

0 comments on commit e585eb2

Please sign in to comment.