From 13a2e9063d65c41a65c864a6d3676eb042bccc4f Mon Sep 17 00:00:00 2001 From: Maciej Andraszyk Date: Fri, 11 Mar 2022 16:52:09 +0100 Subject: [PATCH] Add customHeaders param, update the changelog --- README.md | 12 ++++++++++++ src/ApiClient.php | 19 ++++++++++++++++++- src/VoucherifyClient.php | 5 +++-- test/ClientTest.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c9ac83d..6cc266e 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,17 @@ $apiUrl = "https://.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. @@ -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 diff --git a/src/ApiClient.php b/src/ApiClient.php index dc7386e..e4281cb 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -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"); @@ -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) diff --git a/src/VoucherifyClient.php b/src/VoucherifyClient.php index 737c1a6..6f1ce91 100644 --- a/src/VoucherifyClient.php +++ b/src/VoucherifyClient.php @@ -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 diff --git a/test/ClientTest.php b/test/ClientTest.php index 9387462..b71ff14 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -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)