Skip to content

Commit f458617

Browse files
authored
Merge pull request hkulekci#47 from hkulekci/decoupling-guzzle
decouple guzzle
2 parents 4f906ea + 4acdc31 commit f458617

16 files changed

Lines changed: 313 additions & 181 deletions

composer.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
"psr/http-client": "^1.0",
99
"psr/http-message": "^1.0|^2.0",
1010
"psr/log": "^1.0|^2.0|^3.0",
11-
"guzzlehttp/guzzle": "^7.5",
12-
"guzzlehttp/psr7": "^2.0",
13-
"webmozart/assert": "^1.11"
11+
"webmozart/assert": "^1.11",
12+
"psr/http-factory": "^1.0",
13+
"php-http/discovery": "^1.19",
14+
"psr/http-factory-implementation": "^1.0",
15+
"psr/http-client-implementation": "^1.0"
1416
},
1517
"require-dev": {
1618
"phpunit/phpunit": "^10.0",
1719
"mockery/mockery": "^1.5",
18-
"openai-php/client": "^0.3.5",
19-
"hkulekci/cohere": "dev-main",
20-
"phpunit/php-code-coverage": "^10.1@dev"
20+
"phpunit/php-code-coverage": "^10.1@dev",
21+
"symfony/http-client": "^6.4.3|^7.1.0",
22+
"nyholm/psr7": "^1.8@dev",
23+
"php-http/mock-client": "1.x-dev"
2124
},
2225
"autoload": {
2326
"psr-4": {
@@ -38,5 +41,10 @@
3841
"scripts": {
3942
"test": "vendor/bin/phpunit --display-warnings --display-deprecations",
4043
"coverage": "vendor/bin/phpunit --display-warnings --display-deprecations --coverage-html coverage/"
44+
},
45+
"config": {
46+
"allow-plugins": {
47+
"php-http/discovery": false
48+
}
4149
}
4250
}

src/ClientInterface.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,9 @@
55
*/
66
namespace Qdrant;
77

8-
use Qdrant\Endpoints\Cluster;
9-
use Qdrant\Endpoints\Collections;
10-
use Qdrant\Endpoints\Service;
11-
use Qdrant\Endpoints\Snapshots;
12-
use Qdrant\Http\HttpClientInterface;
8+
use Psr\Http\Message\RequestInterface;
139

14-
interface ClientInterface extends HttpClientInterface
10+
interface ClientInterface
1511
{
16-
public function collections(string $collectionName = null): Collections;
17-
18-
public function snapshots(): Snapshots;
19-
20-
public function cluster(): Cluster;
21-
22-
public function service(): Service;
12+
public function execute(RequestInterface $request): Response;
2313
}

src/Config.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010

1111
class Config
1212
{
13-
1413
protected ?string $apiKey = null;
1514

1615
public function __construct(protected string $host, protected int $port = 6333)
1716
{
1817
}
1918

19+
public function getHost(): string
20+
{
21+
return parse_url($this->host, PHP_URL_HOST) ?: $this->host;
22+
}
23+
24+
public function getPort(): int
25+
{
26+
return $this->port;
27+
}
28+
29+
public function getScheme(): string
30+
{
31+
return parse_url($this->host, PHP_URL_SCHEME) ?: 'http';
32+
}
33+
2034
public function getDomain(): string
2135
{
2236
return $this->host . ':' . $this->port;

src/Endpoints/AbstractEndpoint.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
*/
88
namespace Qdrant\Endpoints;
99

10-
use GuzzleHttp\Psr7\HttpFactory;
11-
use GuzzleHttp\Psr7\Query;
12-
use Psr\Http\Message\RequestInterface;
10+
use Qdrant\ClientInterface;
1311
use Qdrant\Exception\InvalidArgumentException;
14-
use Qdrant\Http\HttpClientInterface;
1512

1613
abstract class AbstractEndpoint
1714
{
1815
protected ?string $collectionName = null;
1916

20-
public function __construct(protected HttpClientInterface $client)
17+
use HttpFactoryTrait;
18+
19+
public function __construct(protected ClientInterface $client)
2120
{
2221
}
2322

@@ -39,32 +38,4 @@ public function getCollectionName(): string
3938
}
4039
return $this->collectionName;
4140
}
42-
43-
protected function queryBuild(array $params): string
44-
{
45-
if ($params) {
46-
return '?' . Query::build($params);
47-
}
48-
return '';
49-
}
50-
51-
/**
52-
* @throws InvalidArgumentException
53-
*/
54-
protected function createRequest(string $method, string $uri, array $body = []): RequestInterface
55-
{
56-
$httpFactory = new HttpFactory();
57-
$request = $httpFactory->createRequest($method, $uri);
58-
if ($body) {
59-
try {
60-
$request = $request->withBody(
61-
$httpFactory->createStream(json_encode($body, JSON_THROW_ON_ERROR))
62-
);
63-
} catch (\JsonException $e) {
64-
throw new InvalidArgumentException('Json parse error!');
65-
}
66-
}
67-
68-
return $request;
69-
}
7041
}

src/Endpoints/HttpFactoryTrait.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* HttpFactoryTrait
4+
*
5+
* @since Jan 2024
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Endpoints;
10+
11+
use Http\Discovery\Psr17FactoryDiscovery;
12+
use Psr\Http\Message\RequestFactoryInterface;
13+
use Psr\Http\Message\RequestInterface;
14+
use Qdrant\Exception\InvalidArgumentException;
15+
16+
trait HttpFactoryTrait
17+
{
18+
protected ?RequestFactoryInterface $httpFactory = null;
19+
20+
public function getHttpFactory(): RequestFactoryInterface
21+
{
22+
if ($this->httpFactory === null) {
23+
$this->httpFactory = Psr17FactoryDiscovery::findRequestFactory();
24+
}
25+
return $this->httpFactory;
26+
}
27+
28+
public function setHttpFactory(RequestFactoryInterface $httpFactory): void
29+
{
30+
$this->httpFactory = $httpFactory;
31+
}
32+
33+
/**
34+
* @throws InvalidArgumentException
35+
*/
36+
protected function createRequest(string $method, string $uri, array $body = []): RequestInterface
37+
{
38+
39+
$request = $this->getHttpFactory()->createRequest($method, $uri);
40+
if ($body) {
41+
try {
42+
$request = $request->withBody(
43+
$this->getHttpFactory()->createStream(json_encode($body, JSON_THROW_ON_ERROR))
44+
);
45+
} catch (\JsonException $e) {
46+
throw new InvalidArgumentException('Json parse error!', $e->getCode(), $e);
47+
}
48+
}
49+
50+
return $request;
51+
}
52+
53+
protected function queryBuild(array $params): string
54+
{
55+
return '?' . http_build_query($params);
56+
}
57+
}

src/Http/Builder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Builder
4+
*
5+
* @since Jan 2024
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Http;
10+
11+
use Http\Discovery\Psr18ClientDiscovery;
12+
use Psr\Http\Client\ClientInterface;
13+
use Qdrant\Config;
14+
15+
class Builder
16+
{
17+
protected ?ClientInterface $client;
18+
public function __construct(ClientInterface $client = null) {
19+
$this->client = $client ?: Psr18ClientDiscovery::find();
20+
}
21+
22+
public function build(Config $config): Transport
23+
{
24+
return new Transport(
25+
$this->client,
26+
$config
27+
);
28+
}
29+
}

src/Http/GuzzleClient.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Http/HttpClientInterface.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Http/Transport.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Transport.php
4+
*
5+
* @since Jan 2024
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Http;
10+
11+
use Psr\Http\Client\ClientInterface;
12+
use Psr\Http\Message\RequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Qdrant\Config;
15+
16+
class Transport implements ClientInterface
17+
{
18+
public function __construct(
19+
private readonly ClientInterface $client,
20+
private readonly Config $config
21+
)
22+
{
23+
}
24+
25+
private function prepareHeaders(RequestInterface $request): RequestInterface
26+
{
27+
$request = $request->withHeader('content-type', 'application/json')
28+
->withHeader('accept', 'application/json')
29+
->withUri(
30+
$request->getUri()
31+
->withHost($this->config->getHost())
32+
->withPort($this->config->getPort())
33+
->withScheme($this->config->getScheme())
34+
);
35+
36+
if ($this->config->getApiKey()) {
37+
$request = $request->withHeader('api-key', $this->config->getApiKey());
38+
}
39+
40+
return $request;
41+
}
42+
43+
public function sendRequest(RequestInterface $request): ResponseInterface
44+
{
45+
$request = $this->prepareHeaders($request);
46+
47+
return $this->client->sendRequest($request);
48+
}
49+
}

0 commit comments

Comments
 (0)