-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from answear/dev-release-v1.0
Connection to BoxNow
- Loading branch information
Showing
39 changed files
with
1,729 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Answear\BoxNowBundle\Client; | ||
|
||
use Answear\BoxNowBundle\Exception\RequestException; | ||
use Answear\BoxNowBundle\Logger\BoxNowLogger; | ||
use Answear\BoxNowBundle\Request\RequestInterface; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Client | ||
{ | ||
public function __construct( | ||
private readonly RequestTransformerInterface $requestTransformer, | ||
private readonly BoxNowLogger $logger, | ||
private ?ClientInterface $client = null, | ||
) { | ||
$this->client ??= new GuzzleClient(); | ||
} | ||
|
||
public function request(RequestInterface $request): ResponseInterface | ||
{ | ||
$this->logger->setRequestId(uniqid('BOXNOW-', more_entropy: true)); | ||
|
||
try { | ||
$psrRequest = $this->requestTransformer->transform($request); | ||
$this->logger->logRequest($request->getEndpoint(), $psrRequest); | ||
|
||
$response = $this->client->send($psrRequest); | ||
|
||
$this->logger->logResponse($request->getEndpoint(), $psrRequest, $response); | ||
|
||
if ($response->getBody()->isSeekable()) { | ||
$response->getBody()->rewind(); | ||
} | ||
} catch (GuzzleException $exception) { | ||
$this->logger->logError($request->getEndpoint(), $exception); | ||
|
||
throw new RequestException($exception->getMessage(), $exception->getCode(), $exception); | ||
} catch (\Throwable $throwable) { | ||
$this->logger->logError($request->getEndpoint(), $throwable); | ||
|
||
throw $throwable; | ||
} finally { | ||
$this->logger->clearRequestId(); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Answear\BoxNowBundle\Client; | ||
|
||
use Answear\BoxNowBundle\ConfigProvider; | ||
use Answear\BoxNowBundle\Request\RequestInterface; | ||
use Answear\BoxNowBundle\Serializer\Serializer; | ||
use GuzzleHttp\Psr7\Request as HttpRequest; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Psr\Http\Message\RequestInterface as PsrRequestInterface; | ||
|
||
class RequestTransformer implements RequestTransformerInterface | ||
{ | ||
public function __construct( | ||
private readonly Serializer $serializer, | ||
private readonly ConfigProvider $configProvider, | ||
) { | ||
} | ||
|
||
public function transform(RequestInterface $request): PsrRequestInterface | ||
{ | ||
$url = $this->configProvider->apiUrl . $request->getEndpoint(); | ||
|
||
if (!is_null($request->getUrlQuery())) { | ||
$url .= '?' . $request->getUrlQuery(); | ||
} | ||
|
||
$body = 'GET' === $request->getMethod() ? null : $this->serializer->serialize($request); | ||
|
||
return new HttpRequest( | ||
$request->getMethod(), | ||
new Uri($url), | ||
['Content-Type' => 'application/json'] + $request->getHeaders(), | ||
$body | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Answear\BoxNowBundle\Client; | ||
|
||
use Answear\BoxNowBundle\Request\RequestInterface; | ||
use Psr\Http\Message\RequestInterface as PsrRequestInterface; | ||
|
||
interface RequestTransformerInterface | ||
{ | ||
public function transform(RequestInterface $request): PsrRequestInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Answear\BoxNowBundle\DTO; | ||
|
||
class PickupPointDTO | ||
{ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly string $type, | ||
public readonly string $name, | ||
public readonly string $address, | ||
public readonly ?string $title = null, | ||
public readonly ?string $image = null, | ||
public readonly ?float $latitude = null, | ||
public readonly ?float $longitude = null, | ||
public readonly ?string $postalCode = null, | ||
public readonly ?string $country = null, | ||
public readonly ?string $note = null, | ||
public readonly ?string $additionalAddress = null, | ||
public readonly ?string $expectedDeliveryTime = null, | ||
public readonly ?string $region = null, | ||
) { | ||
} | ||
|
||
public function getFullAddress(string $separator = ' '): string | ||
{ | ||
return $this->address . $separator . $this->additionalAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Answear\BoxNowBundle\Exception; | ||
|
||
class CoreException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Answear\BoxNowBundle\Exception; | ||
|
||
class RequestException extends CoreException | ||
{ | ||
} |
Oops, something went wrong.