Commit c3b9aec9 authored by Patrick Barroca's avatar Patrick Barroca 😁
Browse files

dev #152400 : electre ng handles previous rest v2 calls

parent dd2e731b
Pipeline #16798 passed with stage
in 35 seconds
......@@ -23,6 +23,12 @@ class ElectreNg extends Provider {
$_id,
$_secret;
public static function handles(string $provider_id) : bool {
// handle previous electre provider code
return in_array($provider_id, [static::ID, 'electre_rest_2']);
}
public function __construct($credentials) {
$this->_id = $credentials[static::ID_KEY] ?? '';
$this->_secret = $credentials[static::SECRET_KEY] ?? '';
......
<?php
namespace Pellicule\Providers;
use \Fig\Http\Message\StatusCodeInterface;
use \Pellicule\Traits\TimeSource;
use \Pellicule\Models\Record;
use \Pellicule\Models\Media;
use \Pellicule\FileSystem;
class ElectreRest2 extends Provider {
use TimeSource;
const
ID = 'electre_rest_2',
API_URL = 'http://restapi.electre.com/Service/couverture/{ean}.jpg',
ID_KEY = 'client_id',
SECRET_KEY = 'client_secret';
protected $_id, $_secret;
public function __construct($credentials) {
$this->_id = isset($credentials[static::ID_KEY])
? $credentials[static::ID_KEY]
: '';
$this->_secret = isset($credentials[static::SECRET_KEY])
? $credentials[static::SECRET_KEY]
: '';
}
public function providerName() {
return 'Electre';
}
public function fetchRecord() {
if (!$this->_id || !$this->_secret)
return $this->_newError(StatusCodeInterface::STATUS_UNAUTHORIZED, 'invalid_credentials');
$identifier = $this->_getIsbnOrEan();
$base_url = str_replace('{ean}', $identifier, static::API_URL);
$params = ['oauth_consumer_key' => $this->_id,
'oauth_nonce' => $this->_nonce(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => static::getTimeSource()->time(),
'oauth_version' => '1.0'];
$params['oauth_signature'] = $this->_sign($base_url, $params);
$url = $base_url . '?' . http_build_query($params);
try {
$response = static::newHttpClient()->get($url);
} catch(\Exception $e) {
return $this->_newError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT,
'no_answer_from_gateway');
}
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK
|| (!$body = $response->getBody())
|| 0 == $body->getSize())
return $this->_newError(StatusCodeInterface::STATUS_BAD_GATEWAY,
'provider_error');
$media = (new Media())
->setProvider($this->providerName())
->setUrl($base_url)
->beCover()
->setFullsizeFromIdentifier($identifier);
if (!(new FileSystem)->writeMediaFrom($media, $response))
return $this->_newError(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR,
'error_writing_cover');
$record = (new Record())
->updateAttributes($this->_search_args)
->setMedia([$media]);
return (new FetchRecordResult())->beSuccess($record);
}
protected function _nonce() {
return bin2hex(random_bytes(8));
}
protected function _sign($url, $params) {
$base_string = 'GET&' . rawurlencode($url) . '&' . rawurlencode(http_build_query($params));
$key = rawurlencode($this->_secret) . '&';
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
}
}
......@@ -11,10 +11,18 @@ use \Fig\Http\Message\StatusCodeInterface;
abstract class Provider {
use HttpClientAware;
const ID = '';
protected
$_search_args = [],
$_headers = [];
public static function handles(string $provider_id) : bool {
return static::ID === $provider_id;
}
public static function newProvider($request, $args) {
return static::_realNewProvider($request, $args)
->setSearchArgs($args);
......
......@@ -11,9 +11,7 @@ class RemoteProviders {
public function __construct() {
$this->_providers = new Collection([ElectreNg::class,
ElectreRest2::class,
Orb::class]);
$this->_providers = new Collection([ElectreNg::class, Orb::class]);
}
......@@ -24,11 +22,11 @@ class RemoteProviders {
$provider = $this->_providers
->detect(function($provider) use($credentials)
{
return $provider::ID == $credentials[static::PROVIDER_KEY];
return $provider::handles($credentials[static::PROVIDER_KEY]);
});
return $provider
? new $provider($credentials)
: null;
}
}
\ No newline at end of file
}
......@@ -16,9 +16,14 @@ abstract class MediaElectreNgTestCase extends TestCase {
public function setUp() {
parent::setUp();
$this->_credentials = base64_encode(json_encode(['provider'=> 'electre_ng',
'client_id' => 'leeloo',
'client_secret' => 'multipass']));
$this->_credentials = $this->_buildCredentials();
}
protected function _buildCredentials() {
return base64_encode(json_encode(['provider'=> 'electre_ng',
'client_id' => 'leeloo',
'client_secret' => 'multipass']));
}
......@@ -272,3 +277,16 @@ class MediaElectreNgGoodAuthenticationGoodApiTest extends MediaElectreNgTestCase
$this->_json_response->media[0]->url);
}
}
class MediaElectreNgHandlesElectreRest2LegacyProviderTest
extends MediaElectreNgGoodAuthenticationGoodApiTest {
protected function _buildCredentials() {
return base64_encode(json_encode(['provider'=> 'electre_rest_2',
'client_id' => 'leeloo',
'client_secret' => 'multipass']));
}
}
\ No newline at end of file
<?php
namespace Pellicule\Tests;
use \Pellicule\FileSystem;
use \Pellicule\Models\Media;
use \Pellicule\Providers\ElectreRest2;
use \Fig\Http\Message\StatusCodeInterface;
use \Slim\Psr7\Headers;
class MediaElectreRest2Test extends TestCase {
public function setUp() {
parent::setUp();
Media::getFileSystem()->whenCalled('fileExists')->answers(true);
FileSystem::setBasePath('./images');
FileSystem::getFileSystem()
->whenCalled('fileExists')->answers(true)
->whenCalled('filePutContents')->answers(null)
;
$this->_http_client
->whenCalled('get')
->answers($this->_forgePSR7Response(['status' => 200,
'headers' => new Headers(['Content-Type' => 'image/jpg']),
'content' => '1']));
$provider = base64_encode(json_encode(['provider'=> 'electre_rest_2',
'client_id' => 'leeloo',
'client_secret' => 'multipass']));
$this->dispatch('/1.0/media/isbn/9782818976173',
['Authorization' => ['Pellicule ' . $provider]]);
}
public function tearDown() {
parent::tearDown();
}
/** @test */
public function fileShouldBeWriten() {
$call_params = FileSystem::getFileSystem()
->getAttributesForLastCallOn('filePutContents');
$this->assertEquals('./images/fullsize/cover/9/7/8/2/9782818976173.jpg',
$call_params[0]);
}
/** @test */
public function responseShouldBeSuccess() {
$this->assertResponseCode(StatusCodeInterface::STATUS_OK);
}
public function urlParts() {
return [
['http://restapi.electre.com/Service/couverture/9782818976173.jpg?'],
['oauth_consumer_key=leeloo'],
];
}
/**
* @test
* @dataProvider urlParts
*/
public function electreUrlShouldBeCalledWithPart($part) {
$this->assertContains($part,
$this->_http_client->getAttributesForLastCallOn('get')[0]);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment