Skip to content

Commit e9e3456

Browse files
feat: Add IsTlsEnabled client option (#35)
Add `IsTlsEnabled` client option When `true`, https is used when generating index clients, when `false`, http is used. --------- Co-authored-by: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com>
1 parent fd63e1d commit e9e3456

File tree

9 files changed

+43
-25
lines changed

9 files changed

+43
-25
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ Control Plane endpoints are accessed via standard HTTP requests. You can configu
649649
- **Timeout**: The time limit for each request before it times out. Default is `30 seconds`.
650650
- **BaseUrl**: The base URL for all requests.
651651
- **HttpClient**: The HTTP client to be used for all requests.
652+
- **IsTlsEnabled**: The client will default to using HTTPS if `true`, and to HTTP if `false`. Default is `true`.
652653

653654
Example usage:
654655

@@ -657,8 +658,9 @@ var pinecone = new PineconeClient("PINECONE_API_KEY", new ClientOptions
657658
{
658659
MaxRetries = 3,
659660
Timeout = TimeSpan.FromSeconds(60),
660-
HttpClient = ... // Override the Http Client
661-
BaseUrl = ... // Override the Base URL
661+
HttpClient = ..., // Override the Http Client
662+
BaseUrl = ..., // Override the Base URL
663+
IsTlsEnabled = true
662664
});
663665
```
664666

src/Pinecone.Test/Integration/Control/Setup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public async Task GlobalSetup()
1919
Console.WriteLine("Initializing control plane integration tests...");
2020
Client = new PineconeClient(
2121
apiKey: Helpers.GetEnvironmentVar("PINECONE_API_KEY"),
22-
new ClientOptions { SourceTag = "test-tag" }
22+
new ClientOptions
23+
{
24+
SourceTag = "test-tag",
25+
BaseUrl = Helpers.GetEnvironmentVar("PINECONE_BASE_URL", BasePineconeEnvironment.Default)
26+
}
2327
);
2428
PineconeEnvironment = "us-west1-gcp";
2529
Dimension = 2;

src/Pinecone.Test/Integration/Data/Setup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public async Task GlobalSetup()
2020
Console.WriteLine("Initializing data plane integration tests...");
2121
Client = new PineconeClient(
2222
apiKey: Helpers.GetEnvironmentVar("PINECONE_API_KEY"),
23-
new ClientOptions { SourceTag = "test-tag" }
23+
new ClientOptions
24+
{
25+
SourceTag = "test-tag",
26+
BaseUrl = Helpers.GetEnvironmentVar("PINECONE_BASE_URL", BasePineconeEnvironment.Default)
27+
}
2428
);
2529
Metric = CreateIndexRequestMetric.Cosine;
2630
Spec = new ServerlessIndexSpec
@@ -84,4 +88,4 @@ private static async Task SeedData()
8488
Console.WriteLine("Seeding data in namespace \"\"");
8589
await Seed.SetupData(IndexClient, "", true);
8690
}
87-
}
91+
}

src/Pinecone.Test/Integration/Data/TestSetupUpsertErrors.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ public class TestSetupUpsertErrors : BaseTest
99
[Test]
1010
public void TestUpsertFailsWhenApiKeyInvalid()
1111
{
12-
var pinecone = new PineconeClient(Helpers.FakeApiKey());
12+
var pinecone = new PineconeClient(Helpers.FakeApiKey(), new ClientOptions
13+
{
14+
BaseUrl = Helpers.GetEnvironmentVar("PINECONE_BASE_URL", BasePineconeEnvironment.Default)
15+
});
1316
var e = Assert.ThrowsAsync<PineconeApiException>(async () =>
1417
{
1518
var index = pinecone.Index(null, IndexHost);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Pinecone;
2+
3+
public partial class ClientOptions
4+
{
5+
/// <summary>
6+
/// When TLS is enabled, the client will default to using HTTPS, and when disabled, it will default to using HTTP.
7+
/// Defaults to true.
8+
/// </summary>
9+
public bool IsTlsEnabled { get; init; } = true;
10+
}

src/Pinecone/Core/Public/Version.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ namespace Pinecone;
22

33
internal class Version
44
{
5-
public const string Current = "2.0.0";
5+
public const string Current = "2.1.0";
66
}

src/Pinecone/Core/RawGrpcClient.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using Grpc.Core;
32
using Grpc.Net.Client;
43

@@ -21,10 +20,7 @@ public RawGrpcClient(ClientOptions clientOptions)
2120
_clientOptions = clientOptions;
2221

2322
var grpcOptions = PrepareGrpcChannelOptions();
24-
Channel =
25-
grpcOptions != null
26-
? GrpcChannel.ForAddress(_clientOptions.BaseUrl, grpcOptions)
27-
: GrpcChannel.ForAddress(_clientOptions.BaseUrl);
23+
Channel = GrpcChannel.ForAddress(_clientOptions.BaseUrl, grpcOptions);
2824
}
2925

3026
/// <summary>
@@ -65,15 +61,11 @@ private void SetHeaders(global::Grpc.Core.Metadata metadata, Headers headers)
6561
}
6662
}
6763

68-
private GrpcChannelOptions? PrepareGrpcChannelOptions()
64+
private GrpcChannelOptions PrepareGrpcChannelOptions()
6965
{
70-
var grpcChannelOptions = _clientOptions.GrpcOptions;
71-
if (grpcChannelOptions == null)
72-
{
73-
return null;
74-
}
66+
var grpcChannelOptions = _clientOptions.GrpcOptions ?? new GrpcChannelOptions();
7567
grpcChannelOptions.HttpClient ??= _clientOptions.HttpClient;
7668
grpcChannelOptions.MaxRetryAttempts ??= _clientOptions.MaxRetries;
7769
return grpcChannelOptions;
7870
}
79-
}
71+
}

src/Pinecone/Pinecone.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<NuGetAudit>false</NuGetAudit>
99
<LangVersion>12</LangVersion>
1010
<Nullable>enable</Nullable>
11-
<Version>2.0.0</Version>
11+
<Version>2.1.0</Version>
1212
<AssemblyVersion>$(Version)</AssemblyVersion>
1313
<FileVersion>$(Version)</FileVersion>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>

src/Pinecone/PineconeClient.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public PineconeClient(string? apiKey = "", ClientOptions? clientOptions = null)
3838
var client = new RawClient(
3939
new ClientOptions
4040
{
41-
BaseUrl = NormalizeHost(host),
41+
BaseUrl = NormalizeHost(host, clientOptions.IsTlsEnabled),
4242
HttpClient = clientOptions.HttpClient,
4343
MaxRetries = clientOptions.MaxRetries,
4444
Timeout = clientOptions.Timeout,
@@ -76,25 +76,28 @@ private static ClientOptions PrepareClientOptions(
7676
clientOptions.Headers[header.Key] = header.Value;
7777
}
7878
}
79+
7980
if (clientOptions.SourceTag != null)
8081
{
8182
clientOptions.Headers["User-Agent"] =
8283
$"lang=C#; version={Version.Current}; source_tag={clientOptions.SourceTag}";
8384
}
85+
8486
return clientOptions;
8587
}
8688

87-
private string NormalizeHost(string host)
89+
private static string NormalizeHost(string host, bool isTlsEnabled)
8890
{
89-
if (host.StartsWith("https://") || host.StartsWith("http://"))
91+
if(host.StartsWith("http://") || host.StartsWith("https://"))
9092
{
9193
return host;
9294
}
93-
return "https://" + host;
95+
96+
return $"{(isTlsEnabled ? "https" : "http")}://{host}";
9497
}
9598

9699
private static string GetFromEnvironmentOrThrow(string env, string message)
97100
{
98101
return Environment.GetEnvironmentVariable(env) ?? throw new Exception(message);
99102
}
100-
}
103+
}

0 commit comments

Comments
 (0)