Skip to content

Commit 4dc36b9

Browse files
authored
Adding feature to append additional path in TypesenseClient (#295)
Adding option to able to append additional path in TypesenseClient
1 parent f876856 commit 4dc36b9

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Typesense/Setup/Node.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public record Node
1818
/// Protocol for the Typesense service - defaults to http.
1919
/// </summary>
2020
public string Protocol { get; set; } = "http";
21+
/// <summary>
22+
/// Protocol for the Typesense service - defaults to http.
23+
/// </summary>
24+
public string AdditionalPath { get; set; } = "";
2125

2226
[Obsolete("Use multi-arity constructor instead.")]
2327
public Node()
@@ -52,4 +56,32 @@ public Node(string host, string port, string protocol = "http")
5256
Port = port;
5357
Protocol = protocol;
5458
}
59+
60+
/// <param name="host">Hostname for the Typesense service.</param>
61+
/// <param name="port">Port for the typesense service.</param>
62+
/// <param name="protocol">Protocol for the Typesense service - defaults to http.</param>
63+
/// <param name="additionalPath">additionalPath for the Typesense service - defaults to empty string.</param>
64+
/// <exception cref="ArgumentException"></exception>
65+
public Node(string host, string port, string protocol = "http", string additionalPath = "")
66+
{
67+
if (string.IsNullOrEmpty(host))
68+
{
69+
throw new ArgumentException("Cannot be NULL or empty.", nameof(host));
70+
}
71+
72+
if (string.IsNullOrEmpty(protocol))
73+
{
74+
throw new ArgumentException("Cannot be NULL or empty.", nameof(protocol));
75+
}
76+
77+
if (string.IsNullOrEmpty(port))
78+
{
79+
throw new ArgumentException("Cannot be NULL or empty.", nameof(port));
80+
}
81+
82+
Host = host;
83+
Port = port;
84+
Protocol = protocol;
85+
AdditionalPath = additionalPath;
86+
}
5587
}

src/Typesense/TypesenseClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public TypesenseClient(IOptions<Config> config, HttpClient httpClient)
4242
ArgumentNullException.ThrowIfNull(httpClient);
4343

4444
var node = config.Value.Nodes.First();
45-
httpClient.BaseAddress = new Uri($"{node.Protocol}://{node.Host}:{node.Port}");
45+
UriBuilder typeSenseUriBuilder = new UriBuilder(node.Protocol, node.Host, int.Parse(node.Port), node.AdditionalPath);
46+
httpClient.BaseAddress = typeSenseUriBuilder.Uri;
4647
httpClient.DefaultRequestHeaders.Add("X-TYPESENSE-API-KEY", config.Value.ApiKey);
4748
_httpClient = httpClient;
4849
if (config.Value.JsonSerializerOptions is not null)
@@ -416,7 +417,7 @@ public Task<CollectionResponse> DeleteCollection(string name, bool compactStore
416417
{
417418
if (string.IsNullOrWhiteSpace(name))
418419
throw new ArgumentException("cannot be null empty or whitespace", nameof(name));
419-
420+
420421
// add compact_store query parameter only on false since it is true by default
421422
var compactStoreQuery = compactStore ? string.Empty : "?compact_store=false";
422423

0 commit comments

Comments
 (0)