Skip to content

Commit

Permalink
Allow to set the voucherify-channel header
Browse files Browse the repository at this point in the history
  • Loading branch information
mandraszyk committed Mar 14, 2022
1 parent 13a2e90 commit 8a00de6
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 85 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,25 @@ $customHeaders = [
];

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl, $customHeaders);

# RESULT:
# x-custom-1: Value-1
```

Special: Voucherify-Channel customization
```php
$apiVersion = null;
$apiUrl = null;
$customHeaders = [
"V-Voucherify-Channel" => "Value-1"
];

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl, $customHeaders);

# RESULT:
# x-voucherify-channel: PHP-SDK-Value-1
```

### PHP autoloading

When you aren't using composer you can load Voucherify module by including `autoload.php` file from `/src` directory.
Expand Down
46 changes: 30 additions & 16 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,44 @@ public function __construct($apiId, $apiKey, $apiVersion = null, $apiUrl = null,

$this->apiId = $apiId;
$this->apiKey = $apiKey;
$this->headers = [
"Content-Type: application/json",
"X-App-Id: " . $this->apiId,
"X-App-Token: " . $this->apiKey,
"X-Voucherify-Channel: PHP-SDK"
];

if (isset($apiUrl)) {
$this->basePath = $apiUrl;
}
if (isset($apiVersion)) {
$this->headers[] = "X-Voucherify-API-Version: " . $apiVersion;
}
$this->resolveCustomHeaders($customHeaders);

$this->setHeaders($apiVersion, $customHeaders);
}

private function resolveCustomHeaders($customHeaders) {
if (!isset($customHeaders)) {
return;
private function setHeaders($apiVersion, $customHeaders) {
$channel = "PHP-SDK";

$headers = [
"content-type" => "application/json",
"x-app-id" => $this->apiId,
"x-app-token" => $this->apiKey,
"x-voucherify-channel" => $channel
];

if (isset($apiVersion)) {
$headers["x-voucherify-api-version"] = $apiVersion;
}
if (isset($customHeaders)) {
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;
}
$headers[strtolower($key)] = $value;
}
}
if (!is_array($customHeaders) && !is_object($customHeaders)) {
throw new \Exception("CustomHeaders type not allowed. Must be an array or an object");
if (substr($headers["x-voucherify-channel"], 0, strlen($channel)) !== $channel) {
$headers["x-voucherify-channel"] = $channel . "-" . $headers["x-voucherify-channel"];
}
foreach ($customHeaders as $key => $value) {
$this->headers = array();

foreach ($headers as $key => $value) {
if (is_null($value)) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions test/CampaignsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
95 changes: 70 additions & 25 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];

CurlMock::enable();
Expand Down Expand Up @@ -50,10 +50,10 @@ public function testInitialization()
public function testCustomApiUrl()
{
$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];

CurlMock::register("https://api.voucherify.io/v1", $headers)
Expand Down Expand Up @@ -81,10 +81,10 @@ public function testCustomApiUrl()
public function testVersioning()
{
$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];

CurlMock::register("https://api.voucherify.io/v1", $headers)
Expand All @@ -97,11 +97,11 @@ public function testVersioning()
$this->assertEquals($result, (object)[ "status" => "ok" ]);

$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK",
"X-Voucherify-API-Version: v2017-04-05",
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK",
"x-voucherify-api-version: v2017-04-05"
];

CurlMock::register("https://api.voucherify.io/v1", $headers)
Expand All @@ -119,28 +119,73 @@ public function testVersioning()
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"
"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"
"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" ]);

$headers = [
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK-Value-2",
"x-custom-1: Value-1"
];

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

$customHeaders = [
"x-custom-1" => "Value-1",
"X-Voucherify-ChanneL" => "Value-2"
];

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

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


$headers = [
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK-value-2",
"x-custom-1: value-1"
];

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

$customHeaders = [
"x-custom-1" => "value-1",
"X-VoucherifY-ChanneL" => "value-2"
];

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

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

CurlMock::done();
}

Expand Down
8 changes: 4 additions & 4 deletions test/CustomersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/DistributionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/ProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/PromotionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/RedemptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/SegmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/ValidationRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/ValidationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down
8 changes: 4 additions & 4 deletions test/VouchersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function setUpBeforeClass()
self::$apiId = "c70a6f00-cf91-4756-9df5-47628850002b";
self::$apiKey = "3266b9f8-e246-4f79-bdf0-833929b1380c";
self::$headers = [
"Content-Type: application/json",
"X-App-Id: " . self::$apiId,
"X-App-Token: " . self::$apiKey,
"X-Voucherify-Channel: PHP-SDK"
"content-type: application/json",
"x-app-id: " . self::$apiId,
"x-app-token: " . self::$apiKey,
"x-voucherify-channel: PHP-SDK"
];
self::$client = new VoucherifyClient(self::$apiId, self::$apiKey);

Expand Down

0 comments on commit 8a00de6

Please sign in to comment.