-
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.
* Upgrade to PHP 7.3 * Fix phpunit.xml * Create AbstractModel * Make sure that all models extend AbstractModel * Create AbstractNormalizer * Make sure that all normalizers extend AbstractNormalizer * Create HttpClient * Use custom HttpClient in Client * Create Fetch enum * Create RawEncoder * Create AbstractResource * Make sure that all resources extend AbstractResource * Fix DealNormalizerTest * Upgrade copmposer packages to support PHP 7.3 * Remove relations from models * Update composer.json
- Loading branch information
1 parent
9464fd3
commit 175a9f2
Showing
80 changed files
with
1,280 additions
and
1,431 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,9 @@ | |
|
||
namespace Snagshout\Promote; | ||
|
||
use Closure; | ||
use DateTime; | ||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\Handler\CurlHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; | ||
use Http\Message\MessageFactory\GuzzleMessageFactory; | ||
use Joli\Jane\OpenApi\Runtime\Client\Resource; | ||
use Joli\Jane\Runtime\Encoder\RawEncoder; | ||
use Psr\Http\Message\RequestInterface; | ||
use Snagshout\Promote\Encoder\RawEncoder; | ||
use Snagshout\Promote\Normalizer\NormalizerFactory; | ||
use Snagshout\Promote\Resource\AbstractResource; | ||
use Snagshout\Promote\Resource\DealsResource; | ||
use Snagshout\Promote\Resource\FrontResource; | ||
use Snagshout\Promote\Resource\UsersResource; | ||
|
@@ -34,132 +25,27 @@ | |
/** | ||
* Class Client. | ||
* | ||
* @package Snagshout\Promote | ||
* | ||
* @author Eduardo Trujillo <[email protected]> | ||
* @author Andrii Prykhodko <[email protected]> | ||
* @package Snagshout\Promote | ||
*/ | ||
class Client | ||
{ | ||
/** | ||
* @var string | ||
* @var HttpClient | ||
*/ | ||
protected $publicId; | ||
protected $httpClient; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $secretKey; | ||
|
||
/** | ||
* @var Uri | ||
*/ | ||
protected $endpoint; | ||
|
||
protected $baseUrl = 'http://localhost:8000'; | ||
|
||
/** | ||
* SyndicationClient constructor. | ||
* Client constructor. | ||
* | ||
* @param string $publicId | ||
* @param string $secretKey | ||
* @param string $baseUrl | ||
*/ | ||
public function __construct($publicId, $secretKey) | ||
{ | ||
$this->publicId = $publicId; | ||
$this->secretKey = $secretKey; | ||
|
||
$this->endpoint = new Uri($this->baseUrl); | ||
|
||
$stack = new HandlerStack(); | ||
|
||
$stack->setHandler(new CurlHandler()); | ||
|
||
$stack->push($this->makeAuthHandler()); | ||
|
||
$this->client = new HttpClient( | ||
[ | ||
'handler' => $stack, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param string $publicId | ||
*/ | ||
public function setPublicId($publicId) | ||
public function __construct(string $publicId, string $secretKey, string $baseUrl) | ||
{ | ||
$this->publicId = $publicId; | ||
} | ||
|
||
/** | ||
* @param string $secretKey | ||
*/ | ||
public function setSecretKey($secretKey) | ||
{ | ||
$this->secretKey = $secretKey; | ||
} | ||
|
||
/** | ||
* @param Uri $endpoint | ||
*/ | ||
public function setEndpoint(Uri $endpoint) | ||
{ | ||
$this->endpoint = $endpoint; | ||
} | ||
|
||
/** | ||
* Computes the hash of a string using the secret key. | ||
* | ||
* @param string $content | ||
* | ||
* @return string | ||
*/ | ||
protected function hash($content) | ||
{ | ||
$timestamp = (new DateTime())->format('Y-m-d H'); | ||
|
||
return hash_hmac( | ||
'sha512', | ||
$content . $timestamp, | ||
$this->secretKey | ||
); | ||
} | ||
|
||
/** | ||
* A Guzzle middleware that automatically adds the required Authorization | ||
* and Content-Hash headers required by the Promote API. | ||
* | ||
* @return Closure | ||
*/ | ||
protected function makeAuthHandler() | ||
{ | ||
return function (callable $handler) { | ||
return function ( | ||
RequestInterface $request, | ||
array $options | ||
) use ($handler) { | ||
$contentHash = $this->hash($request->getBody()); | ||
|
||
$partialUri = $request->getUri(); | ||
$uri = $this->endpoint | ||
->withPath( | ||
$this->endpoint->getPath() | ||
. $partialUri->getPath() | ||
) | ||
->withQuery($partialUri->getQuery()) | ||
->withFragment($partialUri->getFragment()); | ||
|
||
$request = $request | ||
->withUri($uri) | ||
->withHeader( | ||
'Authorization', | ||
vsprintf('Hash %s', [$this->publicId]) | ||
) | ||
->withHeader('Content-Hash', $contentHash); | ||
|
||
return $handler($request, $options); | ||
}; | ||
}; | ||
$this->httpClient = new HttpClient($publicId, $secretKey, $baseUrl); | ||
} | ||
|
||
/** | ||
|
@@ -169,19 +55,14 @@ protected function makeAuthHandler() | |
* | ||
* @return Resource | ||
*/ | ||
protected function buildResource($resourceClass) | ||
protected function buildResource(string $resourceClass): AbstractResource | ||
{ | ||
return new $resourceClass( | ||
new GuzzleAdapter($this->client), | ||
new GuzzleMessageFactory(), | ||
$this->httpClient, | ||
new Serializer( | ||
NormalizerFactory::create(), | ||
[ | ||
new JsonEncoder( | ||
new JsonEncode(), | ||
new JsonDecode() | ||
), | ||
new RawEncoder(), | ||
new JsonEncoder(new JsonEncode(), new JsonDecode()), | ||
] | ||
) | ||
); | ||
|
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,60 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2016-2023, Snagshout <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* This file is part of the Merchant package | ||
*/ | ||
|
||
namespace Snagshout\Promote\Encoder; | ||
|
||
use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
|
||
/** | ||
* Class RawEncoder. | ||
* | ||
* Encoder with no encoding (keeps the same output as the input) | ||
* | ||
* @author Andrii Prykhodko <[email protected]> | ||
* @package Snagshout\Promote\Encoder | ||
*/ | ||
class RawEncoder implements DecoderInterface, EncoderInterface | ||
{ | ||
const FORMAT = 'raw'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decode($data, $format, array $context = array()) | ||
{ | ||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDecoding($format) | ||
{ | ||
return self::FORMAT === $format; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($data, $format, array $context = array()) | ||
{ | ||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsEncoding($format) | ||
{ | ||
return self::FORMAT === $format; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2016-2023, Snagshout <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* This file is part of the Merchant package | ||
*/ | ||
|
||
namespace Snagshout\Promote\Enum; | ||
|
||
/** | ||
* Class Fetch. | ||
* | ||
* @author Andrii Prykhodko <[email protected]> | ||
* @package Snagshout\Promote\Resource | ||
*/ | ||
final class Fetch | ||
{ | ||
public const OBJECT = 'object'; | ||
public const PROMISE = 'promise'; | ||
public const RESPONSE = 'response'; | ||
} |
Oops, something went wrong.