Skip to content

Commit 8e43673

Browse files
committed
psr/http-factory-implementation added. And HttpFactorytrait created. getScheme added into into Config.
1 parent 4ca0a36 commit 8e43673

7 files changed

Lines changed: 90 additions & 56 deletions

File tree

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
"psr/log": "^1.0|^2.0|^3.0",
1111
"webmozart/assert": "^1.11",
1212
"psr/http-factory": "^1.0",
13-
"php-http/discovery": "^1.19"
13+
"php-http/discovery": "^1.19",
14+
"psr/http-factory-implementation": "^1.0"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "^10.0",
1718
"mockery/mockery": "^1.5",
1819
"phpunit/php-code-coverage": "^10.1@dev",
19-
"nyholm/psr7": "^1.8@dev",
20-
"symfony/http-client": "^6.0|^7.0"
20+
"symfony/http-client": "7.1.x-dev",
21+
"nyholm/psr7": "^1.8@dev"
2122
},
2223
"autoload": {
2324
"psr-4": {

src/Config.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@ public function __construct(protected string $host, protected int $port = 6333)
1616
{
1717
}
1818

19-
public function getHost()
19+
public function getHost(): string
2020
{
21-
return $this->host;
21+
return parse_url($this->host, PHP_URL_HOST) ?: $this->host;
2222
}
2323

24-
public function getPort()
24+
public function getPort(): int
2525
{
2626
return $this->port;
2727
}
2828

29+
public function getScheme(): string
30+
{
31+
return parse_url($this->host, PHP_URL_SCHEME) ?: 'http';
32+
}
33+
2934
public function getDomain(): string
3035
{
3136
return $this->host . ':' . $this->port;

src/Endpoints/AbstractEndpoint.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
*/
88
namespace Qdrant\Endpoints;
99

10-
use Http\Discovery\Psr17FactoryDiscovery;
11-
use Psr\Http\Message\RequestInterface;
1210
use Qdrant\ClientInterface;
1311
use Qdrant\Exception\InvalidArgumentException;
1412

1513
abstract class AbstractEndpoint
1614
{
1715
protected ?string $collectionName = null;
1816

17+
use HttpFactoryTrait;
18+
1919
public function __construct(protected ClientInterface $client)
2020
{
2121
}
@@ -38,34 +38,4 @@ public function getCollectionName(): string
3838
}
3939
return $this->collectionName;
4040
}
41-
42-
protected function queryBuild(array $params): string
43-
{
44-
$p = [];
45-
foreach ($params as $k => $v) {
46-
$p[] = urldecode($k) . "=" . urlencode($v);
47-
}
48-
49-
return '?' . implode('&', $p);
50-
}
51-
52-
/**
53-
* @throws InvalidArgumentException
54-
*/
55-
protected function createRequest(string $method, string $uri, array $body = []): RequestInterface
56-
{
57-
$httpFactory = Psr17FactoryDiscovery::findRequestFactory();
58-
$request = $httpFactory->createRequest($method, $uri);
59-
if ($body) {
60-
try {
61-
$request = $request->withBody(
62-
$httpFactory->createStream(json_encode($body, JSON_THROW_ON_ERROR))
63-
);
64-
} catch (\JsonException $e) {
65-
throw new InvalidArgumentException('Json parse error!');
66-
}
67-
}
68-
69-
return $request;
70-
}
7141
}

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!');
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/Transport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private function prepareHeaders(RequestInterface $request): RequestInterface
3030
$request->getUri()
3131
->withHost($this->config->getHost())
3232
->withPort($this->config->getPort())
33-
->withScheme('http')
33+
->withScheme($this->config->getScheme())
3434
);
3535

3636
if ($this->config->getApiKey()) {

src/Qdrant.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Qdrant\Endpoints\Collections;
1212
use Qdrant\Endpoints\Service;
1313
use Qdrant\Endpoints\Snapshots;
14+
use Qdrant\Exception\InvalidArgumentException;
15+
use Qdrant\Exception\ServerException;
1416
use Qdrant\Http\Transport;
1517

1618
class Qdrant implements ClientInterface
@@ -41,22 +43,22 @@ public function service(): Service
4143

4244
public function execute(RequestInterface $request): Response
4345
{
44-
try {
45-
$res = $this->transport->sendRequest($request);
46-
47-
return new Response($res);
48-
} catch (RequestExceptionInterface $e) {
49-
var_dump('RequestException', $e->getMessage(), $request); exit();
50-
/*$statusCode = $e->getResponse()->getStatusCode();
51-
if ($statusCode >= 400 && $statusCode < 500) {
52-
$errorResponse = new Response($e->getResponse());
53-
throw (new InvalidArgumentException($e->getMessage(), $statusCode))
54-
->setResponse($errorResponse);
55-
} elseif ($statusCode >= 500) {
56-
$errorResponse = new Response($e->getResponse());
57-
throw (new ServerException($e->getMessage(), $statusCode))
58-
->setResponse($errorResponse);
59-
}*/
46+
$res = $this->transport->sendRequest($request);
47+
$statusCode = $res->getStatusCode();
48+
if ($statusCode >= 400 && $statusCode < 500) {
49+
$errorResponse = new Response($res);
50+
throw (new InvalidArgumentException(
51+
$errorResponse['status']['error'] ?? 'Invalid Argument Exception',
52+
$statusCode)
53+
)->setResponse($errorResponse);
54+
} elseif ($statusCode >= 500) {
55+
$errorResponse = new Response($res);
56+
throw (new ServerException(
57+
$errorResponse['status']['error'] ?? '500 Interval Service Error',
58+
$statusCode)
59+
)->setResponse($errorResponse);
6060
}
61+
62+
return new Response($res);
6163
}
6264
}

tests/Integration/Endpoints/ClusterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function testClusterInfo(): void
3131
public function testClusterRecover(): void
3232
{
3333
$this->expectException(ServerException::class);
34-
$this->expectExceptionMessage('500 Internal Server Error');
3534
$this->expectExceptionCode(500);
3635

3736
$cluster = new Cluster($this->client);

0 commit comments

Comments
 (0)