Skip to content

Commit

Permalink
Add options
Browse files Browse the repository at this point in the history
  • Loading branch information
parsilver committed Dec 9, 2023
1 parent 065cf22 commit 13ea085
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 83 deletions.
8 changes: 8 additions & 0 deletions src/Exceptions/MaxRetriesExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Farzai\Transport\Exceptions;

class MaxRetriesExceededException extends \RuntimeException
{
//
}
55 changes: 0 additions & 55 deletions src/LoggerAdapter.php

This file was deleted.

161 changes: 148 additions & 13 deletions src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,62 @@

namespace Farzai\Transport;

use Farzai\Transport\Exceptions\MaxRetriesExceededException;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Client\ClientInterface as PsrClientInterface;
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Psr\Log\LoggerInterface as PsrLoggerInterface;

class Transport implements PsrClientInterface
{
const CLIENT_NAME = 'fz';

const VERSION = '1.0.0';
const VERSION = '1.2.0';

/**
* The client instance.
*/
private PsrClientInterface $client;

/**
* The logger instance.
*/
private PsrLoggerInterface $logger;

/**
* The base URI.
*/
private string $uri = '/';

/**
* The headers.
*
* @var array<string, string>
*/
private array $headers = [];

private int $timeout = 30;

private int $retries = 0;

/**
* Create a new client instance.
*/
public function __construct(PsrClientInterface $client)
public function __construct(PsrClientInterface $client, PsrLoggerInterface $logger)
{
$this->client = $client;
$this->logger = $logger;
}

/**
* Send the request.
* Set the base URI.
*/
public function sendRequest(PsrRequestInterface $request): PsrResponseInterface
public function setUri(string $uri): self
{
$uri = $request->getUri();
$this->uri = $uri;

if (empty($uri->getHost())) {
$request = $request->withUri(new Uri($this->getUri().$uri->getPath()));
}

$request = $request->withHeader('User-Agent', self::CLIENT_NAME.'/'.self::VERSION);

return $this->client->sendRequest($request);
return $this;
}

/**
Expand All @@ -58,10 +71,132 @@ public function getUri(): string
}

/**
* Get psr client.
* Set the timeout.
*/
public function setTimeout(int $timeout): self

Check warning on line 76 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L76

Added line #L76 was not covered by tests
{
if ($timeout < 0) {
throw new \InvalidArgumentException('Timeout must be greater than or equal to 0.');

Check warning on line 79 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L78-L79

Added lines #L78 - L79 were not covered by tests
}

$this->timeout = $timeout;

Check warning on line 82 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L82

Added line #L82 was not covered by tests

return $this;

Check warning on line 84 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L84

Added line #L84 was not covered by tests
}

/**
* Get the timeout.
*/
public function getTimeout(): int

Check warning on line 90 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L90

Added line #L90 was not covered by tests
{
return $this->timeout;

Check warning on line 92 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L92

Added line #L92 was not covered by tests
}

/**
* Set the retries.
*/
public function setRetries(int $retries): self
{
if ($retries < 0) {
throw new \InvalidArgumentException('Retries must be greater than or equal to 0.');

Check warning on line 101 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L101

Added line #L101 was not covered by tests
}

$this->retries = $retries;

return $this;
}

/**
* Get the retries.
*/
public function getRetries(): int
{
return $this->retries;
}

/**
* Set the headers.
*
* @param array<string, string> $headers
*/
public function setHeaders(array $headers): self
{
$this->headers = array_merge($this->headers, $headers);

return $this;
}

/**
* Get the headers.
*
* @return array<string, string>
*/
public function getHeaders(): array
{
return $this->headers;
}

public function getLogger(): PsrLoggerInterface
{
return $this->logger;
}

public function sendRequest(PsrRequestInterface $request): PsrResponseInterface
{
$request = $this->setupRequest($request);

while ($this->retries >= 0) {
try {
return $this->client->sendRequest($request);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());

Check warning on line 152 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L151-L152

Added lines #L151 - L152 were not covered by tests
}

$this->retries--;

Check warning on line 155 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L155

Added line #L155 was not covered by tests

if ($this->retries >= 0) {
$this->logger->info('Retrying request...');

Check warning on line 158 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L157-L158

Added lines #L157 - L158 were not covered by tests
}
}

throw ($e ?? new MaxRetriesExceededException('Max retries exceeded.'));

Check warning on line 162 in src/Transport.php

View check run for this annotation

Codecov / codecov/patch

src/Transport.php#L162

Added line #L162 was not covered by tests
}

public function getPsrClient(): PsrClientInterface
{
return $this->client;
}

public function setupRequest(PsrRequestInterface $request): PsrRequestInterface
{
$uri = $request->getUri();

if (empty($uri->getHost())) {
$request = $this->setupConnectionUri($request);
}

$request = $this->decorateRequest($request);

return $request;
}

public function setupConnectionUri(PsrRequestInterface $request): PsrRequestInterface
{
$uri = new Uri($this->uri);
$uri = $uri->withPath($uri->getPath().$request->getUri()->getPath());
$uri = $uri->withQuery($request->getUri()->getQuery());

return $request->withUri($uri);
}

public function decorateRequest(PsrRequestInterface $request): PsrRequestInterface
{
$request = $request->withHeader('User-Agent', self::CLIENT_NAME.'/'.self::VERSION);

foreach ($this->headers as $name => $value) {
$request = $request->withHeader($name, $value);
}

return $request;
}
}
21 changes: 16 additions & 5 deletions src/TransportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class TransportBuilder
final class TransportBuilder
{
/**
* The client instance.
Expand Down Expand Up @@ -39,6 +39,11 @@ public function setClient(ClientInterface $client)
return $this;
}

public function getClient(): ?ClientInterface

Check warning on line 42 in src/TransportBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/TransportBuilder.php#L42

Added line #L42 was not covered by tests
{
return $this->client;

Check warning on line 44 in src/TransportBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/TransportBuilder.php#L44

Added line #L44 was not covered by tests
}

/**
* Set the logger
*/
Expand All @@ -49,16 +54,22 @@ public function setLogger(LoggerInterface $logger)
return $this;
}

public function getLogger(): ?LoggerInterface

Check warning on line 57 in src/TransportBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/TransportBuilder.php#L57

Added line #L57 was not covered by tests
{
return $this->logger;

Check warning on line 59 in src/TransportBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/TransportBuilder.php#L59

Added line #L59 was not covered by tests
}

/**
* Build the transport.
*/
public function build(): Transport
{
$logger = $this->logger ?? new NullLogger();
$client = $this->client ?? new GuzzleClient();

return new Transport(
client: new LoggerAdapter(
client: $this->client ?? new GuzzleClient(),
logger: $this->logger ?? new NullLogger(),
),
client: $client,
logger: $logger,
);
}
}
Loading

0 comments on commit 13ea085

Please sign in to comment.