Skip to content

Commit cd8a42c

Browse files
committed
Upgrade farzai/transport to v2.1
- Update farzai/transport dependency from ^1.2 to ^2.1 - Migrate ClientBuilder to use new TransportBuilder API (withBaseUri, setLogger) - Replace Request class with RequestBuilder in PendingRequest - Remove PsrResponseTrait usage and implement PSR-7 delegation directly in AbstractResponseDecorator - Add jsonOrNull() and toArray() methods to AbstractResponseDecorator - Fix typo: isSuccessfull() → isSuccessful() - Fix throw() return type to static for proper chaining - Add guzzlehttp/guzzle as dev dependency - Add .phpunit.cache to .gitignore
1 parent cfb89a4 commit cd8a42c

7 files changed

Lines changed: 132 additions & 31 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/.fleet
44
.php_cs
55
.php_cs.cache
6+
.phpunit.cache
67
.phpunit.result.cache
78
build
89
composer.lock

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
"require": {
1717
"php": "^8.1",
1818
"farzai/support": "^1.2",
19-
"farzai/transport": "^1.2",
19+
"farzai/transport": "^2.1",
2020
"phrity/websocket": "^3.0"
2121
},
2222
"require-dev": {
23+
"guzzlehttp/guzzle": "^7.8",
2324
"pestphp/pest": "^2.15",
2425
"laravel/pint": "^1.0",
2526
"spatie/ray": "^1.28"

src/ClientBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ public function build()
100100

101101
$logger = $this->logger ?? new NullLogger();
102102

103-
$builder = TransportBuilder::make();
103+
$builder = TransportBuilder::make()
104+
->withBaseUri(sprintf('https://%s', self::DEFAULT_HOST))
105+
->setLogger($logger);
106+
104107
if ($this->httpClient) {
105-
$builder->setClient($this->httpClient);
108+
$builder = $builder->setClient($this->httpClient);
106109
}
107110

108-
$builder->setLogger($logger);
109-
110111
$transport = $builder->build();
111-
$transport->setUri(sprintf('https://%s', self::DEFAULT_HOST));
112112

113113
$client = new Client(
114114
config: $this->config,

src/Requests/PendingRequest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Farzai\Bitkub\Contracts\RequestInterceptor;
77
use Farzai\Bitkub\Responses\ResponseWithValidateErrorCode;
88
use Farzai\Transport\Contracts\ResponseInterface;
9-
use Farzai\Transport\Request;
9+
use Farzai\Transport\RequestBuilder;
1010
use Farzai\Transport\Response;
1111
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
1212
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
@@ -146,25 +146,27 @@ public function createRequest(string $method, string $path, array $options = [])
146146
// Normalize path
147147
$path = '/'.trim($path, '/');
148148

149+
$builder = (new RequestBuilder)->method($method)->uri($path);
150+
149151
// Query
150152
if (isset($options['query']) && is_array($options['query']) && ! empty($options['query'])) {
151-
$path .= '?'.http_build_query($options['query']);
153+
$builder = $builder->withQuery($options['query']);
152154
}
153155

154156
// Set body
155157
if (isset($options['body'])) {
156158
$body = $options['body'];
157-
158-
// Convert array to json
159-
if (is_array($body)) {
160-
$body = json_encode($body);
161-
}
159+
$builder = is_array($body)
160+
? $builder->withJson($body)
161+
: $builder->withBody($body);
162162
}
163163

164164
// Set headers
165-
$headers = $options['headers'] ?? [];
165+
if (! empty($options['headers'])) {
166+
$builder = $builder->withHeaders($options['headers']);
167+
}
166168

167-
return new Request($method, $path, $headers, $body ?? null);
169+
return $builder->build();
168170
}
169171

170172
/**

src/Responses/AbstractResponseDecorator.php

Lines changed: 105 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Farzai\Bitkub\Responses;
44

55
use Farzai\Transport\Contracts\ResponseInterface;
6-
use Farzai\Transport\Traits\PsrResponseTrait;
76
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
7+
use Psr\Http\Message\StreamInterface;
88

99
abstract class AbstractResponseDecorator implements ResponseInterface
1010
{
11-
use PsrResponseTrait;
12-
1311
protected ResponseInterface $response;
1412

1513
/**
@@ -45,11 +43,11 @@ public function headers(): array
4543
}
4644

4745
/**
48-
* Check if the response is successfull.
46+
* Check if the response is successful.
4947
*/
50-
public function isSuccessfull(): bool
48+
public function isSuccessful(): bool
5149
{
52-
return $this->response->isSuccessfull();
50+
return $this->response->isSuccessful();
5351
}
5452

5553
/**
@@ -61,15 +59,31 @@ public function json(?string $key = null): mixed
6159
}
6260

6361
/**
64-
* Throw an exception if the response is not successfull.
65-
*
66-
* @param callable<\Farzai\Transport\Contracts\ResponseInterface> $callback Custom callback to throw an exception.
62+
* Return the json decoded response or null.
63+
*/
64+
public function jsonOrNull(?string $key = null): mixed
65+
{
66+
return $this->response->jsonOrNull($key);
67+
}
68+
69+
/**
70+
* Return the response as an array.
71+
*/
72+
public function toArray(): array
73+
{
74+
return $this->response->toArray();
75+
}
76+
77+
/**
78+
* Throw an exception if the response is not successful.
6779
*
6880
* @throws \Psr\Http\Client\ClientExceptionInterface
6981
*/
70-
public function throw(?callable $callback = null)
82+
public function throw(?callable $callback = null): static
7183
{
72-
return $this->response->throw($callback);
84+
$this->response->throw($callback);
85+
86+
return $this;
7387
}
7488

7589
/**
@@ -79,4 +93,84 @@ public function getPsrRequest(): PsrRequestInterface
7993
{
8094
return $this->response->getPsrRequest();
8195
}
96+
97+
// PSR-7 ResponseInterface delegation
98+
99+
public function getStatusCode(): int
100+
{
101+
return $this->response->getStatusCode();
102+
}
103+
104+
public function withStatus(int $code, string $reasonPhrase = ''): static
105+
{
106+
return $this->cloneWithResponse($this->response->withStatus($code, $reasonPhrase));
107+
}
108+
109+
public function getReasonPhrase(): string
110+
{
111+
return $this->response->getReasonPhrase();
112+
}
113+
114+
public function getProtocolVersion(): string
115+
{
116+
return $this->response->getProtocolVersion();
117+
}
118+
119+
public function withProtocolVersion(string $version): static
120+
{
121+
return $this->cloneWithResponse($this->response->withProtocolVersion($version));
122+
}
123+
124+
public function getHeaders(): array
125+
{
126+
return $this->response->getHeaders();
127+
}
128+
129+
public function hasHeader(string $name): bool
130+
{
131+
return $this->response->hasHeader($name);
132+
}
133+
134+
public function getHeader(string $name): array
135+
{
136+
return $this->response->getHeader($name);
137+
}
138+
139+
public function getHeaderLine(string $name): string
140+
{
141+
return $this->response->getHeaderLine($name);
142+
}
143+
144+
public function withHeader(string $name, $value): static
145+
{
146+
return $this->cloneWithResponse($this->response->withHeader($name, $value));
147+
}
148+
149+
public function withAddedHeader(string $name, $value): static
150+
{
151+
return $this->cloneWithResponse($this->response->withAddedHeader($name, $value));
152+
}
153+
154+
public function withoutHeader(string $name): static
155+
{
156+
return $this->cloneWithResponse($this->response->withoutHeader($name));
157+
}
158+
159+
public function getBody(): StreamInterface
160+
{
161+
return $this->response->getBody();
162+
}
163+
164+
public function withBody(StreamInterface $body): static
165+
{
166+
return $this->cloneWithResponse($this->response->withBody($body));
167+
}
168+
169+
private function cloneWithResponse(ResponseInterface $response): static
170+
{
171+
$clone = clone $this;
172+
$clone->response = $response;
173+
174+
return $clone;
175+
}
82176
}

src/Responses/ResponseWithValidateErrorCode.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
class ResponseWithValidateErrorCode extends AbstractResponseDecorator
99
{
1010
/**
11-
* Throw an exception if the response is not successfull.
11+
* Throw an exception if the response is not successful.
1212
*
1313
*
1414
* @throws \Psr\Http\Client\ClientExceptionInterface
1515
*/
16-
public function throw(?callable $callback = null)
16+
public function throw(?callable $callback = null): static
1717
{
18-
return parent::throw($callback ?? function (ResponseInterface $response, ?\Exception $e) use ($callback) {
19-
if ($this->json('error') !== null && $this->json('error') !== 0) {
18+
parent::throw($callback ?? function (ResponseInterface $response, ?\Exception $e) use ($callback) {
19+
$errorCode = $this->json('error');
20+
if ($errorCode !== null && $errorCode !== 0) {
2021
throw new BitkubResponseErrorCodeException($response);
2122
}
2223

2324
return $callback ? $callback($response, $e) : $response;
2425
});
26+
27+
return $this;
2528
}
2629
}

tests/ResponseErrorCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
expect($response->headers())->toBe([
2828
'Content-Type' => 'application/json',
2929
]);
30-
expect($response->isSuccessfull())->toBeTrue();
30+
expect($response->isSuccessful())->toBeTrue();
3131
expect($response->json('error'))->toBe(0);
3232
expect($response->getPsrRequest())->toBe($psrRequest);
3333
});

0 commit comments

Comments
 (0)