Skip to content

Commit

Permalink
Refactored SDK to use the new Generic Bokbasen API client instead and…
Browse files Browse the repository at this point in the history
… upped code to PHP 7.1.
  • Loading branch information
Ketil Stadskleiv committed Jun 26, 2017
1 parent 1716b8a commit a894b87
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 129 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ The SDK assumes working knowledge of the API, see documentation: https://bokbase

## Authentication

This SDK requires the Auth SDK to work, see [php-sdk-auth](https://github.com/Bokbasen/php-sdk-auth) for details on how to authenticate.
This SDK requires the Bokbasen API Client, see [php-api-client](https://github.com/Bokbasen/php-api-client) for details on how to implement.

All code examples below assumes that this variable exists:

```php
<?php
$auth = new \Bokbasen\Auth\Login('username', 'password');
$client = new \BokbasenApiClient\Client($auth);
?>
```

Expand All @@ -25,7 +26,7 @@ Use auth object and set your subscription when creating the export object (subsc
```php
<?php
use Bokbasen\Metadata\Export\Onix;
$onixClient = new Onix($auth, Onix::URL_PROD, Onix::SUBSCRIPTION_EXTENDED);
$onixClient = new Onix($client, Onix::URL_PROD, Onix::SUBSCRIPTION_EXTENDED);
?>
```

Expand Down
68 changes: 37 additions & 31 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
{
"name": "bokbasen/php-sdk-met-api",
"license": "MIT",
"type": "library",
"description": "PHP SDK Bokbasen's Metadata API",
"homepage": "https://github.com/Bokbasen/php-sdk-order-api",
"autoload": {
"psr-4" : {
"Bokbasen\\Metadata\\" : "src"
}
},
"autoload-dev": {
"psr-4": {
"Bokbasen\\Metadata\\Tests\\" : "tests"
}
},
"authors": [
{
"name": "Ketil Stadskleiv",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.0",
"bokbasen/php-sdk-auth": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5",
"php-http/guzzle6-adapter": "^1.0",
"php-http/mock-client": "^0.3"
}
}
"name" : "bokbasen/php-sdk-met-api",
"license" : "MIT",
"type" : "library",
"description" : "PHP SDK Bokbasen's Metadata API",
"homepage" : "https://github.com/Bokbasen/php-sdk-met-api",
"prefer-stable": true,
"minimum-stability": "dev",
"autoload" : {
"psr-4" : {
"Bokbasen\\Metadata\\" : "src"
}
},
"autoload-dev" : {
"psr-4" : {
"Bokbasen\\Metadata\\Tests\\" : "tests"
}
},
"authors" : [{
"name" : "Ketil Stadskleiv",
"email" : "[email protected]"
}
],
"require" : {
"php" : ">=7.1.0",
"bokbasen/php-api-client": "^1.0"
},
"require-dev" : {
"phpunit/phpunit" : "^6",
"php-http/guzzle6-adapter" : "^1.0",
"php-http/mock-client" : "^0.3"
},
"repositories": [
{
"type": "vcs", "url": "https://github.com/Bokbasen/php-api-client"
}
]
}
23 changes: 14 additions & 9 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace Bokbasen\Metadata;

use Bokbasen\Auth\Login;
use Http\Client\HttpClient;
use Bokbasen\ApiClient\Client;

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -24,22 +24,27 @@
*/
abstract class BaseClient
{
use \Bokbasen\Http\HttpMethodsTrait;

const URL_PROD = 'https://api.boknett.no/metadata/';

const URL_TEST = 'https://api.boknett.webbe.no/metadata/';

const URL_DEV = 'https://api.boknett.dev.webbe.no/metadata/';

const AFTER_PARAMETER_DATE_FORMAT = 'YmdHis';

/**
*
* @var Client
*/
protected $apiClient;

/**
*
* @param \Bokbasen\Auth\Login $auth
* @param string $baseUrl
* @param Client $auth
*/
public function __construct(Login $auth, $url = self::URL_PROD, HttpClient $httpClient = null)
public function __construct(Client $apiClient)
{
$this->auth = $auth;
$this->url = $url;
$this->setHttpClient($httpClient);
$this->apiClient = $apiClient;
}
}
27 changes: 12 additions & 15 deletions src/Export/ExportBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Bokbasen\Metadata\Export;

use Bokbasen\Metadata\BaseClient;
use Bokbasen\Auth\Login;
use Http\Client\HttpClient;
use Psr\Http\Message\ResponseInterface;
use Bokbasen\ApiClient\Client;

/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Expand Down Expand Up @@ -48,12 +48,12 @@ abstract class ExportBase extends BaseClient

/**
*
* @param \Bokbasen\Auth\Login $auth
* @param string $baseUrl
* @param Client $apiClient
* @param string $subscription
*/
public function __construct(Login $auth, $url = self::URL_PROD, $subscription = self::SUBSCRIPTION_EXTENDED, HttpClient $httpClient = null)
public function __construct(Client $apiClient, $subscription = self::SUBSCRIPTION_EXTENDED)
{
parent::__construct($auth, $url, $httpClient);
parent::__construct($apiClient);
$this->subscription = $subscription;
}

Expand All @@ -65,15 +65,14 @@ public function __construct(Login $auth, $url = self::URL_PROD, $subscription =
* @param int $pageSize
* @param string $path
*
* @return \Psr\Http\Message\RequestInterface
* @return ResponseInterface
*/
protected function createRequest($nextToken, \DateTime $afterDate = null, $pageSize, $path)
protected function executeGetRequest($nextToken, \DateTime $afterDate = null, $pageSize, $path)
{
$url = $this->url . $path;
$url = $path;

$parameters = [];

$parameters = []

;
if (! empty($this->subscription)) {
$parameters['subscription'] = $this->subscription;
}
Expand All @@ -91,9 +90,7 @@ protected function createRequest($nextToken, \DateTime $afterDate = null, $pageS
$url .= '?' . http_build_query($parameters);
}

$request = $this->getMessageFactory()->createRequest('GET', $url, $this->makeHeadersArray($this->auth));

return $request;
return $this->apiClient->get($url, null);
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/Export/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class Object extends ExportBase
* @param int $pageSize
* @return boolean
*/
public function downloadNext($nextToken, $targetPath, array $objectsTypesToDownload = [], array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, $pageSize = self::MAX_PAGE_SIZE)
public function downloadNext(string $nextToken, string $targetPath, array $objectsTypesToDownload = [], array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, int $pageSize = self::MAX_PAGE_SIZE):bool
{
$response = $this->httpClient->sendRequest($this->createRequest($nextToken, null, $pageSize, self::PATH));
$response = $this->executeGetRequest($nextToken, null, $pageSize, self::PATH);

$status = $this->downloadObjects($response, $objectsTypesToDownload, $targetPath, $isbnFilter, $filenameFormatter);

Expand All @@ -82,9 +82,9 @@ public function downloadNext($nextToken, $targetPath, array $objectsTypesToDownl
*
* @return string
*/
public function downloadAfter(\DateTime $afterDate, $targetPath, array $objectsTypesToDownload = [], $downloadAllPages = true, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, $pageSize = self::MAX_PAGE_SIZE)
public function downloadAfter(\DateTime $afterDate, string $targetPath, array $objectsTypesToDownload = [], bool $downloadAllPages = true, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, int $pageSize = self::MAX_PAGE_SIZE):string
{
$response = $this->httpClient->sendRequest($this->createRequest(null, $afterDate, $pageSize, self::PATH));
$response = $this->executeGetRequest(null, $afterDate, $pageSize, self::PATH);
$morePages = $this->downloadObjects($response, $objectsTypesToDownload, $targetPath, $isbnFilter, $filenameFormatter);
$this->lastNextToken = $response->getHeaderLine('Next');

Expand Down Expand Up @@ -112,7 +112,7 @@ public function downloadAfter(\DateTime $afterDate, $targetPath, array $objectsT
*
* @return bool true if object report had books, false if object report was empty
*/
protected function downloadObjects(ResponseInterface $response, array $objectsTypesToDownload, $targetPath, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null)
protected function downloadObjects(ResponseInterface $response, array $objectsTypesToDownload, string $targetPath, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null):bool
{
$xml = new \SimpleXMLElement((string) $response->getBody());

Expand All @@ -134,9 +134,7 @@ protected function downloadObjects(ResponseInterface $response, array $objectsTy
$targetFilename = $filenameFormatter->getFilename($object);
$url = (string) $object->REFERENCE;

// @todo this will put entire file into memory, replace with streams?
$request = $this->getMessageFactory()->createRequest('GET', $url, $this->makeHeadersArray($this->auth));
$response = $this->httpClient->sendRequest($request);
$response = $this->apiClient->executeHttpRequest('GET', [], null, $url);

$status = file_put_contents($targetPath . $targetFilename, (string) $response->getBody());
if ($status === false) {
Expand Down
23 changes: 15 additions & 8 deletions src/Export/Onix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ class Onix extends ExportBase

const PATH = 'export/onix';

public function getByISBN($isbn)
/**
* Get ONIX by ISBN ,returns XML as string
*
* @param string $isbn
* @return string
*/
public function getByISBN(string $isbn): string
{
$subscription = $this->subscription;
$this->subscription = null;
$response = $this->httpClient->sendRequest($this->createRequest(null, null, null, self::PATH . '/' . $isbn));
$response = $this->apiClient->get(self::PATH . '/' . $isbn);
$this->subscription = $subscription;

return (string) $response->getBody();
}

public function downloadNext($nextToken, $targetFolder, $pageSize = self::MAX_PAGE_SIZE)
public function downloadNext(string $nextToken, string $targetFolder, int $pageSize = self::MAX_PAGE_SIZE): bool
{
$response = $this->httpClient->sendRequest($this->createRequest($nextToken, null, $pageSize, self::PATH));
$response = $this->executeGetRequest($nextToken, null, $pageSize, self::PATH);

$this->saveOnixToDisk($response, $targetFolder);

Expand All @@ -53,9 +59,10 @@ public function downloadNext($nextToken, $targetFolder, $pageSize = self::MAX_PA
return $response->hasHeader('Link');
}

public function downloadAfter(\DateTime $afterDate, $targetFilename, $downloadAllPages = true, $pageSize = self::MAX_PAGE_SIZE)
public function downloadAfter(\DateTime $afterDate, string $targetFilename, bool $downloadAllPages = true, int $pageSize = self::MAX_PAGE_SIZE): string
{
$response = $this->httpClient->sendRequest($this->createRequest(null, $afterDate, $pageSize, self::PATH));
$response = $this->executeGetRequest(null, $afterDate, $pageSize, self::PATH);

$this->saveOnixToDisk($response, $targetFilename);
$morePages = $response->hasHeader('Link');
$this->lastNextToken = $response->getHeaderLine('Next');
Expand All @@ -69,14 +76,14 @@ public function downloadAfter(\DateTime $afterDate, $targetFilename, $downloadAl
return $this->lastNextToken;
}

protected function makeFilename($folder)
protected function makeFilename(string $folder): string
{
$filename = microtime(true) . '-onix';

return $folder . $filename . '.xml';
}

protected function saveOnixToDisk(ResponseInterface $response, $targetFolder)
protected function saveOnixToDisk(ResponseInterface $response, string $targetFolder): void
{
$targetFilename = $this->makeFilename($targetFolder);

Expand Down
4 changes: 2 additions & 2 deletions src/Formatters/DefaultDownloadFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class DefaultDownloadFileFormatter implements DownloadFileFormatterInterface
{

public function getFilename(\SimpleXMLElement $objectReportXml)
public function getFilename(\SimpleXMLElement $objectReportXml):string
{
if (isset($objectReportXml->ISBN13) && ! empty($objectReportXml->ISBN13)) {
$ean = $objectReportXml->ISBN13;
Expand All @@ -33,7 +33,7 @@ public function getFilename(\SimpleXMLElement $objectReportXml)
return $ean . '-' . $objectReportXml->TYPE . $this->getFileExtension($objectReportXml->TYPE);
}

protected function getFileExtension($type)
protected function getFileExtension($type):string
{
if ($type == \Bokbasen\Metadata\Export\Object::OBJECT_TYPE_AUDIO_SAMPLE) {
$ext = '.mp3';
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/DownloadFileFormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
* @license https://opensource.org/licenses/MIT
*/
interface DownloadFileFormatterInterface{
public function getFilename(\SimpleXMLElement $objectReportXml);
public function getFilename(\SimpleXMLElement $objectReportXml):string;
}
Loading

0 comments on commit a894b87

Please sign in to comment.