Skip to content

Commit ec8d225

Browse files
authored
Add static api authentication with bearer token
1 parent 8d8691f commit ec8d225

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

src/Factory.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ class Factory
3333
*/
3434
protected string $tenant = 'default_tenant';
3535

36+
/**
37+
* The bearer token used for authentication.
38+
*/
39+
protected string $bearerToken;
40+
3641
/**
3742
* The http client to use for the requests.
3843
*/
3944
protected \GuzzleHttp\Client $httpClient;
4045

41-
4246
/**
4347
* The ChromaDB api provider for the instance.
4448
*/
@@ -80,6 +84,15 @@ public function withTenant(string $tenant): self
8084
return $this;
8185
}
8286

87+
/**
88+
* The bearer token used to authenticate requests.
89+
*/
90+
public function withBearerToken(string $bearerToken): self
91+
{
92+
$this->bearerToken = $bearerToken;
93+
return $this;
94+
}
95+
8396
/**
8497
* The http client to use for the requests.
8598
*/
@@ -100,14 +113,20 @@ public function createApiClient() : ChromaApiClient
100113
{
101114
$this->baseUrl = $this->host . ':' . $this->port;
102115

103-
$this->httpClient ??= new \GuzzleHttp\Client([
116+
$headers = [
117+
'Content-Type' => 'application/json',
118+
'Accept' => 'application/json',
119+
];
120+
121+
if (!empty($this->bearerToken)) {
122+
$headers['Authorization'] = 'Bearer ' . $this->bearerToken;
123+
}
124+
125+
$this->httpClient ??= new \GuzzleHttp\Client([
104126
'base_uri' => $this->baseUrl,
105-
'headers' => [
106-
'Content-Type' => 'application/json',
107-
'Accept' => 'application/json'
108-
],
127+
'headers' => $headers,
109128
]);
110129

111130
return new ChromaApiClient($this->httpClient);
112131
}
113-
}
132+
}

0 commit comments

Comments
 (0)