Skip to content

Commit 36cbd51

Browse files
refactor: reorganize codebase structure for better maintainability
1 parent 3bf9f8d commit 36cbd51

33 files changed

Lines changed: 159 additions & 193 deletions
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated;
7-
8-
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaConnectionException;
9-
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaException;
10-
use Codewithkyrian\ChromaDB\Generated\Models\Collection;
11-
use Codewithkyrian\ChromaDB\Generated\Models\Database;
12-
use Codewithkyrian\ChromaDB\Generated\Models\Tenant;
13-
use Codewithkyrian\ChromaDB\Generated\Requests\AddEmbeddingRequest;
14-
use Codewithkyrian\ChromaDB\Generated\Requests\CreateCollectionRequest;
15-
use Codewithkyrian\ChromaDB\Generated\Requests\CreateDatabaseRequest;
16-
use Codewithkyrian\ChromaDB\Generated\Requests\CreateTenantRequest;
17-
use Codewithkyrian\ChromaDB\Generated\Requests\DeleteEmbeddingRequest;
18-
use Codewithkyrian\ChromaDB\Generated\Requests\GetEmbeddingRequest;
19-
use Codewithkyrian\ChromaDB\Generated\Requests\QueryEmbeddingRequest;
20-
use Codewithkyrian\ChromaDB\Generated\Requests\UpdateCollectionRequest;
21-
use Codewithkyrian\ChromaDB\Generated\Requests\UpdateEmbeddingRequest;
22-
use Codewithkyrian\ChromaDB\Generated\Responses\GetItemsResponse;
23-
use Codewithkyrian\ChromaDB\Generated\Responses\QueryItemsResponse;
6+
namespace Codewithkyrian\ChromaDB;
7+
8+
use Codewithkyrian\ChromaDB\Exceptions\ChromaConnectionException;
9+
use Codewithkyrian\ChromaDB\Exceptions\ChromaException;
10+
use Codewithkyrian\ChromaDB\Models\Collection;
11+
use Codewithkyrian\ChromaDB\Models\Database;
12+
use Codewithkyrian\ChromaDB\Models\Tenant;
13+
use Codewithkyrian\ChromaDB\Requests\AddEmbeddingRequest;
14+
use Codewithkyrian\ChromaDB\Requests\CreateCollectionRequest;
15+
use Codewithkyrian\ChromaDB\Requests\CreateDatabaseRequest;
16+
use Codewithkyrian\ChromaDB\Requests\CreateTenantRequest;
17+
use Codewithkyrian\ChromaDB\Requests\DeleteEmbeddingRequest;
18+
use Codewithkyrian\ChromaDB\Requests\GetEmbeddingRequest;
19+
use Codewithkyrian\ChromaDB\Requests\QueryEmbeddingRequest;
20+
use Codewithkyrian\ChromaDB\Requests\UpdateCollectionRequest;
21+
use Codewithkyrian\ChromaDB\Requests\UpdateEmbeddingRequest;
22+
use Codewithkyrian\ChromaDB\Responses\GetItemsResponse;
23+
use Codewithkyrian\ChromaDB\Responses\QueryItemsResponse;
2424
use GuzzleHttp\Client;
2525
use GuzzleHttp\Exception\ConnectException;
2626
use GuzzleHttp\Exception\RequestException;
@@ -29,7 +29,7 @@
2929
/**
3030
* Client for ChromaDB API (v.0.1.0)
3131
*/
32-
class ChromaApiClient
32+
class Api
3333
{
3434

3535
public function __construct(
@@ -43,6 +43,7 @@ public function root(): array
4343
} catch (ClientExceptionInterface $e) {
4444
$this->handleChromaApiException($e);
4545
}
46+
4647
return json_decode($response->getBody()->getContents(), true);
4748
}
4849

@@ -51,12 +52,11 @@ public function version(): string
5152
{
5253
try {
5354
$response = $this->httpClient->get('/api/v2/version');
54-
55-
// remove the quo
56-
return trim($response->getBody()->getContents(), '"');
5755
} catch (ClientExceptionInterface $e) {
5856
$this->handleChromaApiException($e);
5957
}
58+
59+
return json_decode($response->getBody()->getContents(), true);
6060
}
6161

6262
public function heartbeat(): array
@@ -66,6 +66,7 @@ public function heartbeat(): array
6666
} catch (ClientExceptionInterface $e) {
6767
$this->handleChromaApiException($e);
6868
}
69+
6970
return json_decode($response->getBody()->getContents(), true);
7071
}
7172

@@ -76,6 +77,7 @@ public function preFlightChecks(): mixed
7677
} catch (ClientExceptionInterface $e) {
7778
$this->handleChromaApiException($e);
7879
}
80+
7981
return json_decode($response->getBody()->getContents(), true);
8082
}
8183

@@ -119,13 +121,13 @@ public function getTenant(string $tenant): ?Tenant
119121
{
120122
try {
121123
$response = $this->httpClient->get("/api/v2/tenants/$tenant");
122-
123-
$result = json_decode($response->getBody()->getContents(), true);
124-
125-
return Tenant::make($result);
126124
} catch (ClientExceptionInterface $e) {
127125
$this->handleChromaApiException($e);
128126
}
127+
128+
$result = json_decode($response->getBody()->getContents(), true);
129+
130+
return Tenant::make($result);
129131
}
130132

131133

@@ -139,9 +141,7 @@ public function listCollections(string $database, string $tenant): array
139141

140142
$result = json_decode($response->getBody()->getContents(), true);
141143

142-
return array_map(function (array $item) {
143-
return Collection::make($item);
144-
}, $result);
144+
return array_map(fn(array $item) => Collection::make($item), $result);
145145
}
146146

147147
public function createCollection(string $database, string $tenant, CreateCollectionRequest $request): Collection
@@ -175,7 +175,7 @@ public function getCollection(string $collectionId, string $database, string $te
175175
public function updateCollection(string $collectionId, string $database, string $tenant, UpdateCollectionRequest $request): void
176176
{
177177
try {
178-
$response = $this->httpClient->put("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId", [
178+
$this->httpClient->put("/api/v2/tenants/$tenant/databases/$database/collections/$collectionId", [
179179
'json' => $request->toArray(),
180180
]);
181181
} catch (ClientExceptionInterface $e) {

src/ChromaDB.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static function client(): Client
1313
}
1414

1515
/**
16-
* Creates a new factory instance to configure a custom Alchemy Client
16+
* Creates a new factory instance to configure a custom ChromaDB Client
1717
*/
1818
public static function factory(): Factory
1919
{
@@ -24,8 +24,8 @@ public static function factory(): Factory
2424
* Resets the database. This will delete all collections and entries and
2525
* return true if the database was reset successfully.
2626
*/
27-
public static function reset() : bool
27+
public static function reset(): bool
2828
{
29-
return (new Factory())->createApiClient()->reset();
29+
return (new Factory())->createApi()->reset();
3030
}
31-
}
31+
}

src/Client.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,39 @@
55
namespace Codewithkyrian\ChromaDB;
66

77
use Codewithkyrian\ChromaDB\Embeddings\EmbeddingFunction;
8-
use Codewithkyrian\ChromaDB\Generated\ChromaApiClient;
9-
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaNotFoundException;
10-
use Codewithkyrian\ChromaDB\Generated\Models\Collection;
8+
use Codewithkyrian\ChromaDB\Api;
9+
use Codewithkyrian\ChromaDB\Exceptions\ChromaNotFoundException;
10+
use Codewithkyrian\ChromaDB\Models\Collection;
11+
use Codewithkyrian\ChromaDB\Requests\CreateDatabaseRequest;
12+
use Codewithkyrian\ChromaDB\Requests\CreateTenantRequest;
13+
use Codewithkyrian\ChromaDB\Requests\CreateCollectionRequest;
1114
use Codewithkyrian\ChromaDB\Resources\CollectionResource;
1215

1316
class Client
1417
{
1518
public function __construct(
16-
public readonly ChromaApiClient $apiClient,
17-
public readonly string $database,
18-
public readonly string $tenant,
19-
)
20-
{
19+
public readonly Api $api,
20+
public readonly string $database,
21+
public readonly string $tenant,
22+
) {
2123
$this->initDatabaseAndTenant();
2224
}
2325

2426

2527
public function initDatabaseAndTenant(): void
2628
{
2729
try {
28-
$this->apiClient->getTenant($this->tenant);
30+
$this->api->getTenant($this->tenant);
2931
} catch (ChromaNotFoundException) {
30-
$createTenantRequest = new Generated\Requests\CreateTenantRequest($this->tenant);
31-
$this->apiClient->createTenant($createTenantRequest);
32+
$createTenantRequest = new CreateTenantRequest($this->tenant);
33+
$this->api->createTenant($createTenantRequest);
3234
}
3335

3436
try {
35-
$this->apiClient->getDatabase($this->database, $this->tenant);
37+
$this->api->getDatabase($this->database, $this->tenant);
3638
} catch (ChromaNotFoundException) {
37-
$createDatabaseRequest = new Generated\Requests\CreateDatabaseRequest($this->database);
38-
$this->apiClient->createDatabase($this->tenant, $createDatabaseRequest);
39+
$createDatabaseRequest = new CreateDatabaseRequest($this->database);
40+
$this->api->createDatabase($this->tenant, $createDatabaseRequest);
3941
}
4042
}
4143

@@ -44,7 +46,7 @@ public function initDatabaseAndTenant(): void
4446
*/
4547
public function version(): string
4648
{
47-
return $this->apiClient->version();
49+
return $this->api->version();
4850
}
4951

5052
/**
@@ -53,7 +55,7 @@ public function version(): string
5355
*/
5456
public function heartbeat(): int
5557
{
56-
$res = $this->apiClient->heartbeat();
58+
$res = $this->api->heartbeat();
5759

5860
return $res['nanosecond heartbeat'] ?? 0;
5961
}
@@ -65,7 +67,7 @@ public function heartbeat(): int
6567
*/
6668
public function listCollections(): array
6769
{
68-
return $this->apiClient->listCollections($this->database, $this->tenant);
70+
return $this->api->listCollections($this->database, $this->tenant);
6971
}
7072

7173

@@ -80,17 +82,16 @@ public function listCollections(): array
8082
*/
8183
public function createCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
8284
{
83-
$request = new Generated\Requests\CreateCollectionRequest($name, $metadata);
84-
85-
$collection = $this->apiClient->createCollection($this->database, $this->tenant, $request);
85+
$request = new CreateCollectionRequest($name, $metadata);
8686

87+
$collection = $this->api->createCollection($this->database, $this->tenant, $request);
8788

8889
return CollectionResource::make(
8990
$collection,
9091
$this->database,
9192
$this->tenant,
9293
$embeddingFunction,
93-
$this->apiClient
94+
$this->api
9495
);
9596
}
9697

@@ -105,16 +106,16 @@ public function createCollection(string $name, ?array $metadata = null, ?Embeddi
105106
*/
106107
public function getOrCreateCollection(string $name, ?array $metadata = null, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
107108
{
108-
$request = new Generated\Requests\CreateCollectionRequest($name, $metadata, true);
109+
$request = new CreateCollectionRequest($name, $metadata, true);
109110

110-
$collection = $this->apiClient->createCollection($this->database, $this->tenant, $request);
111+
$collection = $this->api->createCollection($this->database, $this->tenant, $request);
111112

112113
return CollectionResource::make(
113114
$collection,
114115
$this->database,
115116
$this->tenant,
116117
$embeddingFunction,
117-
$this->apiClient
118+
$this->api
118119
);
119120
}
120121

@@ -129,14 +130,14 @@ public function getOrCreateCollection(string $name, ?array $metadata = null, ?Em
129130
*/
130131
public function getCollection(string $name, ?EmbeddingFunction $embeddingFunction = null): CollectionResource
131132
{
132-
$collection = $this->apiClient->getCollection($name, $this->database, $this->tenant);
133+
$collection = $this->api->getCollection($name, $this->database, $this->tenant);
133134

134135
return CollectionResource::make(
135136
$collection,
136137
$this->database,
137138
$this->tenant,
138139
$embeddingFunction,
139-
$this->apiClient
140+
$this->api
140141
);
141142
}
142143

@@ -147,7 +148,7 @@ public function getCollection(string $name, ?EmbeddingFunction $embeddingFunctio
147148
*/
148149
public function deleteCollection(string $name): void
149150
{
150-
$this->apiClient->deleteCollection($name, $this->database, $this->tenant);
151+
$this->api->deleteCollection($name, $this->database, $this->tenant);
151152
}
152153

153154
/**
@@ -161,7 +162,4 @@ public function deleteAllCollections(): void
161162
$this->deleteCollection($collection->name);
162163
}
163164
}
164-
165-
166-
167-
}
165+
}

src/Generated/Exceptions/ChromaAuthorizationException.php renamed to src/Exceptions/ChromaAuthorizationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaAuthorizationException extends ChromaException
99
{

src/Generated/Exceptions/ChromaConnectionException.php renamed to src/Exceptions/ChromaConnectionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
5+
namespace Codewithkyrian\ChromaDB\Exceptions;
66

77

88
class ChromaConnectionException extends ChromaException

src/Generated/Exceptions/ChromaDimensionalityException.php renamed to src/Exceptions/ChromaDimensionalityException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaDimensionalityException extends ChromaException
99
{

src/Generated/Exceptions/ChromaException.php renamed to src/Exceptions/ChromaException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaException extends \Exception
99
{

src/Generated/Exceptions/ChromaInvalidArgumentException.php renamed to src/Exceptions/ChromaInvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaInvalidArgumentException extends ChromaException {}

src/Generated/Exceptions/ChromaInvalidCollectionException.php renamed to src/Exceptions/ChromaInvalidCollectionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaInvalidCollectionException extends ChromaException
99
{

src/Generated/Exceptions/ChromaNotFoundException.php renamed to src/Exceptions/ChromaNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
6+
namespace Codewithkyrian\ChromaDB\Exceptions;
77

88
class ChromaNotFoundException extends ChromaException
99
{

0 commit comments

Comments
 (0)