Skip to content

Commit

Permalink
Code cleanup and added methods for getting XML as string based on nex…
Browse files Browse the repository at this point in the history
…t and after parameter
  • Loading branch information
Ketil Stadskleiv committed Aug 30, 2017
1 parent a894b87 commit 7ea0684
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 30 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ All code examples below assumes that this variable exists:

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

$auth = Login('username', 'password');
$client = new Client($auth);
?>
```

## ONIX Exports
Expand All @@ -30,7 +33,6 @@ $onixClient = new Onix($client, Onix::URL_PROD, Onix::SUBSCRIPTION_EXTENDED);
?>
```


### Get ONIX for single ISBN
```php
<?php
Expand All @@ -48,7 +50,7 @@ $nextToken = $onixClient->downloadAfter(new \DateTime('2017-01-01'),'/onixFolder
?>
```

###Download ONIX to file based on a token
### Download ONIX to file based on a token
Use $nextToken to get all changes since last execution

```php
Expand All @@ -64,6 +66,20 @@ while($morePages){
?>
```

### Get XML as string

If you do not want to use the SDK to write files for you, use the following methods to get the full Response object for each request. This means you need to implement the header parsing logic yourself.

```php
<?php
$response = $onixClient->getAfter(new \DateTime('2017-01-01'));
//Implement your own parsing here and get token from header

//Use token to get more pages
$response = $onixClient->getNext($token);
?>
```

## Object Exports

Object reports has a similar process as ONIX download when it comes to paging. The SDK abstracts the report aspect and download the actual files for you.
Expand Down
12 changes: 6 additions & 6 deletions src/Export/ExportBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ abstract class ExportBase extends BaseClient
*/
protected $subscription;

const SUBSCRIPTION_SCHOOL = 'school';
public const SUBSCRIPTION_SCHOOL = 'school';

const SUBSCRIPTION_EXTENDED = 'extended';
public const SUBSCRIPTION_EXTENDED = 'extended';

const SUBSCRIPTION_BASIC = 'basic';
public const SUBSCRIPTION_BASIC = 'basic';

/**
*
Expand All @@ -67,7 +67,7 @@ public function __construct(Client $apiClient, $subscription = self::SUBSCRIPTIO
*
* @return ResponseInterface
*/
protected function executeGetRequest($nextToken, \DateTime $afterDate = null, $pageSize, $path)
protected function executeGetRequest($nextToken, \DateTime $afterDate = null, $pageSize, $path): ResponseInterface
{
$url = $path;

Expand All @@ -90,15 +90,15 @@ protected function executeGetRequest($nextToken, \DateTime $afterDate = null, $p
$url .= '?' . http_build_query($parameters);
}

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

/**
* Get the last next token returned from the API
*
* @return string
*/
public function getLastNextToken()
public function getLastNextToken():string
{
return $this->lastNextToken;
}
Expand Down
19 changes: 9 additions & 10 deletions src/Export/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Bokbasen\Metadata\Export;

use Bokbasen\Metadata\Exceptions\BokbasenMetadataAPIException;
use Http\Client\HttpClient;
use Psr\Http\Message\ResponseInterface;
use Bokbasen\Metadata\Formatters\DownloadFileFormatterInterface;
use Bokbasen\Metadata\Formatters\DefaultDownloadFileFormatter;
Expand Down Expand Up @@ -30,17 +29,17 @@
class Object extends ExportBase
{

const OBJECT_TYPE_AUDIO_SAMPLE = 'ly';
public const OBJECT_TYPE_AUDIO_SAMPLE = 'ly';

const OBJECT_COVER_IMAGE_SMALL = 'ol';
public const OBJECT_COVER_IMAGE_SMALL = 'ol';

const OBJECT_COVER_IMAGE_LARGE = 'os';
public const OBJECT_COVER_IMAGE_LARGE = 'os';

const OBJECT_COVER_IMAGE_ORIGINAL = 'org';
public const OBJECT_COVER_IMAGE_ORIGINAL = 'org';

const MAX_PAGE_SIZE = 5000;
public const MAX_PAGE_SIZE = 5000;

const PATH = 'export/object';
protected const PATH = 'export/object';

/**
*
Expand All @@ -59,7 +58,7 @@ class Object extends ExportBase
* @param int $pageSize
* @return boolean
*/
public function downloadNext(string $nextToken, string $targetPath, array $objectsTypesToDownload = [], array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, int $pageSize = self::MAX_PAGE_SIZE):bool
public function downloadNext(string $nextToken, string $targetPath, array $objectsTypesToDownload = [], array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, int $pageSize = self::MAX_PAGE_SIZE): bool
{
$response = $this->executeGetRequest($nextToken, null, $pageSize, self::PATH);

Expand All @@ -82,7 +81,7 @@ public function downloadNext(string $nextToken, string $targetPath, array $objec
*
* @return string
*/
public function downloadAfter(\DateTime $afterDate, string $targetPath, array $objectsTypesToDownload = [], bool $downloadAllPages = true, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null, int $pageSize = self::MAX_PAGE_SIZE):string
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->executeGetRequest(null, $afterDate, $pageSize, self::PATH);
$morePages = $this->downloadObjects($response, $objectsTypesToDownload, $targetPath, $isbnFilter, $filenameFormatter);
Expand Down Expand Up @@ -112,7 +111,7 @@ public function downloadAfter(\DateTime $afterDate, string $targetPath, array $o
*
* @return bool true if object report had books, false if object report was empty
*/
protected function downloadObjects(ResponseInterface $response, array $objectsTypesToDownload, string $targetPath, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null):bool
protected function downloadObjects(ResponseInterface $response, array $objectsTypesToDownload, string $targetPath, array $isbnFilter = [], DownloadFileFormatterInterface $filenameFormatter = null): bool
{
$xml = new \SimpleXMLElement((string) $response->getBody());

Expand Down
51 changes: 45 additions & 6 deletions src/Export/Onix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Bokbasen\Metadata\Export;

use Bokbasen\Metadata\Exceptions\BokbasenMetadataAPIException;
use Http\Client\HttpClient;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -28,9 +27,9 @@
class Onix extends ExportBase
{

const MAX_PAGE_SIZE = 1000;
public const MAX_PAGE_SIZE = 1000;

const PATH = 'export/onix';
protected const PATH = 'export/onix';

/**
* Get ONIX by ISBN ,returns XML as string
Expand All @@ -48,9 +47,41 @@ public function getByISBN(string $isbn): string
return (string) $response->getBody();
}

/**
* Execute metadata request with next-token, returns response object
*
* @param string $nextToken
* @param int $pageSize
* @return ResponseInterface
*/
public function getNext(string $nextToken, int $pageSize = self::MAX_PAGE_SIZE): ResponseInterface
{
return $this->executeGetRequest($nextToken, null, $pageSize, self::PATH);
}

/**
* Execute metadata request with after parameter, returns response object
*
* @param \DateTime $afterDate
* @param int $pageSize
* @return ResponseInterface
*/
public function getAfter(\DateTime $afterDate, int $pageSize = self::MAX_PAGE_SIZE): ResponseInterface
{
return $this->executeGetRequest(null, $afterDate, $pageSize, self::PATH);
}

/**
* Download XML to file based on next token, returns true if there are more pages to iterate over
*
* @param string $nextToken
* @param string $targetFolder
* @param int $pageSize
* @return bool
*/
public function downloadNext(string $nextToken, string $targetFolder, int $pageSize = self::MAX_PAGE_SIZE): bool
{
$response = $this->executeGetRequest($nextToken, null, $pageSize, self::PATH);
$response = $this->getNext($nextToken, $pageSize);

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

Expand All @@ -59,10 +90,18 @@ public function downloadNext(string $nextToken, string $targetFolder, int $pageS
return $response->hasHeader('Link');
}

/**
* Download XML to file based on after DateTime, returns token to use with downloadNext to iterate over further pages.
*
* @param \DateTime $afterDate
* @param string $targetFilename
* @param bool $downloadAllPages
* @param int $pageSize
* @return string
*/
public function downloadAfter(\DateTime $afterDate, string $targetFilename, bool $downloadAllPages = true, int $pageSize = self::MAX_PAGE_SIZE): string
{
$response = $this->executeGetRequest(null, $afterDate, $pageSize, self::PATH);

$response = $this->getAfter($afterDate, $pageSize);
$this->saveOnixToDisk($response, $targetFilename);
$morePages = $response->hasHeader('Link');
$this->lastNextToken = $response->getHeaderLine('Next');
Expand Down
6 changes: 3 additions & 3 deletions src/Import/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
class Object extends BaseClient
{

const TYPE_PRODUCT_IMAGE = 'productimage';
public const TYPE_PRODUCT_IMAGE = 'productimage';

const TYPE_AUDIO_SAMPLE = 'audiosample';
public const TYPE_AUDIO_SAMPLE = 'audiosample';

const TYPE_TABLE_OF_CONTENT = 'tableofcontents';
public const TYPE_TABLE_OF_CONTENT = 'tableofcontents';

const TYPE_CONTENT_MAPPING = [
self::TYPE_AUDIO_SAMPLE => HttpRequestOptions::CONTENT_TYPE_AUDIO_MPEG,
Expand Down

0 comments on commit 7ea0684

Please sign in to comment.