Skip to content

Commit

Permalink
Merge pull request #6 from tanelt/master
Browse files Browse the repository at this point in the history
Updated API semantics
  • Loading branch information
actualreports authored Nov 26, 2017
2 parents 6cfcc04 + 66b2c4a commit 5012135
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ composer require actualreports/pdfgeneratorapi-php

### Usage
```php
$client = new \ActualReports\PDFGeneratorAPI\Client($token, $secret);
$client = new \ActualReports\PDFGeneratorAPI\Client($key, $secret);
$client->setWorkspace('unique@workspace.com');
```

Expand Down
43 changes: 17 additions & 26 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Client
/**
* @var string
*/
private $token;
private $key;
/**
* @var string
*/
Expand Down Expand Up @@ -51,13 +51,13 @@ class Client
/**
* Client constructor.
*
* @param $token
* @param $secret
* @param string $key
* @param string $secret
* @param string $workspace
*/
public function __construct($token, $secret, $workspace = null)
public function __construct($key, $secret, $workspace = null)
{
$this->token = $token;
$this->key = $key;
$this->secret = $secret;
$this->workspace = $workspace;
}
Expand Down Expand Up @@ -118,9 +118,9 @@ protected static function data($data)
*/
protected function createSignature($resource)
{
if (!$this->token)
if (!$this->key)
{
throw new Exception('Missing api token');
throw new Exception('Missing api key');
}
if (!$this->secret)
{
Expand All @@ -131,9 +131,15 @@ protected function createSignature($resource)
throw new Exception('Missing workspace id');
}

$data = $this->token.$this->workspace.$resource;
$data = [
'key' => $this->key,
'workspace' => $this->workspace,
'resource' => $resource
];

ksort($data);

return hash_hmac('sha256', $data, $this->secret);
return hash_hmac('sha256', implode('', $data), $this->secret);
}

/**
Expand All @@ -151,21 +157,6 @@ protected function getHttpClient()
return $this->httpClient;
}

/**
* @param string $resource
*
* @return array
*/
protected function getRequestHeaders($resource)
{
return [
'Token' => $this->token,
'Workspace' => $this->workspace,
'Signature' => $this->createSignature($resource),
'Content-Type' => 'application/json'
];
}

/**
* @param string $method
* @param string $resource
Expand All @@ -182,7 +173,7 @@ public function request($method = self::REQUEST_POST, $resource, array $params =

$options = [
'headers' => array_merge($headers, [
'X-Auth-Token' => $this->token,
'X-Auth-Key' => $this->key,
'X-Auth-Workspace' => $this->workspace,
'X-Auth-Signature' => $signature,
'Content-Type' => 'application/json; charset=utf-8',
Expand Down Expand Up @@ -391,7 +382,7 @@ public function editor($template, $data = null, array $params = [])
{
$resource = 'templates/'.$template.'/editor';
$params = array_merge([
'token' => $this->token,
'key' => $this->key,
'workspace' => $this->workspace,
'signature' => $this->createSignature($resource)
], $params);
Expand Down
8 changes: 4 additions & 4 deletions tests/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Client extends \PHPUnit_Framework_TestCase
* @var \ActualReports\PDFGeneratorAPI\Client
*/
protected $client;
protected $token = '61e5f04ca1794253ed17e6bb986c1702';
protected $key = '61e5f04ca1794253ed17e6bb986c1702';
protected $secret = '68db1902ad1bb26d34b3f597488b9b28';
protected $workspace = 'demo.example@actualreports.com';
protected $host = 'http://127.0.0.3';// 'https://staging.pdfgeneratorapi.com';
Expand All @@ -23,7 +23,7 @@ public function setUp()
{
parent::setUp();

$this->client = new \ActualReports\PDFGeneratorAPI\Client($this->token, $this->secret, $this->workspace);
$this->client = new \ActualReports\PDFGeneratorAPI\Client($this->key, $this->secret, $this->workspace);
$this->client->setBaseUrl($this->host.'/api/v3');
}

Expand Down Expand Up @@ -68,14 +68,14 @@ public function testOutputDataString()
public function testEditorDataUrl()
{
$url = $this->client->editor($this->templateId, $this->host.'/assets/web/data/qbo_invoice.json');
$compareUrl = $this->host.'/api/v3/templates/21650/editor?token=61e5f04ca1794253ed17e6bb986c1702&workspace=demo.example%40actualreports.com&signature=f119b90f5a0a10b09f735be6f7b46d27c02b82b03b31097f577fc0fba7e617b2&data=http%3A%2F%2F127.0.0.3%2Fassets%2Fweb%2Fdata%2Fqbo_invoice.json';
$compareUrl = $this->host.'/api/v3/templates/21650/editor?key=61e5f04ca1794253ed17e6bb986c1702&workspace=demo.example%40actualreports.com&signature=1887f3df76acee7965758160f1baf39ce271e804cbf98f1677ae7a90716a31a8&data=http%3A%2F%2F127.0.0.3%2Fassets%2Fweb%2Fdata%2Fqbo_invoice.json';
$this->assertEquals($compareUrl, $url);
}

public function testEditorDataArray()
{
$url = $this->client->editor($this->templateId, ['DocNumber' => 1123123123]);
$compareUrl = $this->host.'/api/v3/templates/21650/editor?token=61e5f04ca1794253ed17e6bb986c1702&workspace=demo.example%40actualreports.com&signature=f119b90f5a0a10b09f735be6f7b46d27c02b82b03b31097f577fc0fba7e617b2&data=%7B%22DocNumber%22%3A1123123123%7D';
$compareUrl = $this->host.'/api/v3/templates/21650/editor?key=61e5f04ca1794253ed17e6bb986c1702&workspace=demo.example%40actualreports.com&signature=1887f3df76acee7965758160f1baf39ce271e804cbf98f1677ae7a90716a31a8&data=%7B%22DocNumber%22%3A1123123123%7D';
$this->assertEquals($compareUrl, $url);
}

Expand Down

0 comments on commit 5012135

Please sign in to comment.