Skip to content

Commit

Permalink
Add customHeaders param, update the changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
mandraszyk committed Mar 11, 2022
1 parent 066beab commit 13a2e90
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ $apiUrl = "https://<region>.api.voucherify.io";
$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl);
```

### Custom Headers
It is possible to send custom headers in Voucherify API request.
```php
$apiVersion = null;
$apiUrl = null;
$customHeaders = [
"X-Custom-1" => "Value-1"
];

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl, $customHeaders);
```
### PHP autoloading

When you aren't using composer you can load Voucherify module by including `autoload.php` file from `/src` directory.
Expand Down Expand Up @@ -703,6 +714,7 @@ class Voucher extends CI_Controller {
Bug reports and pull requests are welcome through [GitHub Issues](https://github.com/rspective/voucherify-php-sdk/issues).

### Changelog
- **2022-03-11** - `2.1.0` - Add AsyncActions support and a `$customHeaders` param
- **2019-07-19** - `2.0.0` - Hide API versioning in `$apiUrl` param
- **2018-12-28** - `1.7.10` - Add Validation Rule Assignments
- **2018-03-18** - `1.7.9` - Add Utils with verifyWebhookSignature method
Expand Down
19 changes: 18 additions & 1 deletion src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class ApiClient
* @param string $apiKey
* @param string $apiVersion - default null
* @param string $apiUrl - default null
* @param array|stdClass $customHeaders - default null
*/
public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null)
public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null, $customHeaders = null)
{
if (!isset($apiId)) {
throw new \Exception("ApiId is required");
Expand All @@ -64,6 +65,22 @@ public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null)
if (isset($apiVersion)) {
$this->headers[] = "X-Voucherify-API-Version: " . $apiVersion;
}
$this->resolveCustomHeaders($customHeaders);
}

private function resolveCustomHeaders($customHeaders) {
if (!isset($customHeaders)) {
return;
}
if (!is_array($customHeaders) && !is_object($customHeaders)) {
throw new \Exception("CustomHeaders type not allowed. Must be an array or an object");
}
foreach ($customHeaders as $key => $value) {
if (is_null($value)) {
continue;
}
$this->headers[] = $key . ": " . $value;
}
}

private function encodeParams($params)
Expand Down
5 changes: 3 additions & 2 deletions src/VoucherifyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ class VoucherifyClient
* @param string $apiId
* @param string $apiKey
* @param string $apiVersion - Override the API version. When 'null' use default account API settings.
* @param array|stdClass $customHeaders - Provide custom headers
*/
public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null)
public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null, $customHeaders = null)
{
// PRIVATE
$this->client = new ApiClient($apiId, $apiKey, $apiVersion, $apiUrl);
$this->client = new ApiClient($apiId, $apiKey, $apiVersion, $apiUrl, $customHeaders);
$this->promotionTiers = new PromotionTiers($this->client);

// PUBLIC
Expand Down
28 changes: 28 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ public function testVersioning()
CurlMock::done();
}

public function testCustomHeaders()
{
$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK",
"X-Custom-1: Value-1",
"X-Custom-2: Value-2"
];

CurlMock::register("https://api.voucherify.io/v1", $headers)
->get("/vouchers/test-voucher-1")
->reply(200, [ "status" => "ok" ]);

$customHeaders = [
"X-Custom-1" => "Value-1",
"X-Custom-2" => "Value-2"
];

$client = new VoucherifyClient(self::$apiId, self::$apiKey, null, null, $customHeaders);

$result = $client->vouchers->get("test-voucher-1");
$this->assertEquals($result, (object)[ "status" => "ok" ]);

CurlMock::done();
}

public function testErrorHandling()
{
CurlMock::register("https://api.voucherify.io/v1", self::$headers)
Expand Down

0 comments on commit 13a2e90

Please sign in to comment.