Skip to content

Commit

Permalink
Merge branch 'release-2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Adena Internet committed Feb 8, 2018
2 parents f19ebaf + 7070b1f commit ee93f6d
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 85 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "vleks/sdk",
"description": "A PHP client SDK for the Vleks.com API",
"version": "1.0.0",
"version": "2.0.0",
"type": "library",
"keywords": ["api", "sdk", "vleks", "vleks.com"],
"homepage": "https://www.vleks.com",
"time": "2018-02-02",
"time": "2018-02-08",
"license": "MIT",
"authors": [
{
Expand Down
15 changes: 14 additions & 1 deletion examples/ListOrdersExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@

if ($result->hasOrders()) {
foreach ($result->getOrders() as $order) {
var_dump($order->getOrderID() . ':' . $order->getStatus());
echo 'Order ID: ' . $order->getOrderID() . "\n";
echo 'Status: ' . $order->getStatus() . "\n";

if ($order->hasOrderLines()) {
echo "\n" . 'Orderline(s):' . "\n";

$orderLines = $order->getOrderLines();

foreach ($orderLines->getOrderLine() as $orderLine) {
echo '#' . $orderLine->getOrderLineID() . ' (' . $orderLine->getQuantityOrdered() . 'x - ' . $orderLine->getTitle() . ')' . "\n";
}
}

echo "\n----------------------------\n\n";
}
}
} catch (Vleks\SDK\Exceptions\ClientException $clientException) {
Expand Down
70 changes: 60 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Client
{
const VERSION = '1.0.0';
const VERSION = '2.0.0';
const ENDPOINT = 'https://%s/api/vleks/2017-05/';
const MESSAGE_HEADERS = 'HEADERS';
const MESSAGE_BODY = 'BODY';
Expand All @@ -19,6 +19,7 @@ class Client
private $testMode = false;
private $skipSslVerification = false;
private $responseHeaders;
private $max_retries = 3;

public function __construct(
$publicKey,
Expand Down Expand Up @@ -524,8 +525,29 @@ private function invoke(array $converted)
$envelopeHeaders = $this->addRequiredEnvelopeHeaders($envelopeHeaders);
$converted[self::MESSAGE_HEADERS] = $envelopeHeaders;

$response = $this->performRequest($converted);
$statusCode = $response['Status'];
# Variables for retrying requests
$shouldRetry = false;
$retries = 0;

do {
$response = $this->performRequest($converted);
$statusCode = $response['Status'];
$shouldRetry = false;

if(500 <= $statusCode) {

# Will throw an exception if applicable, otherwise retry is allowed
$this->reportAnyErrors($response['ResponseBody'], $response['Status'], $response['ResponseHeaders'], false);
$shouldRetry = true;

if($shouldRetry && $retries < $this->max_retries) {
$this->pauseOnRetry(++$retries);
} else {
$shouldRetry = false;
}
}
} while($shouldRetry);


$this->reportAnyErrors($response['ResponseBody'], $response['Status'], $response['ResponseHeaders']);

Expand All @@ -535,8 +557,18 @@ private function invoke(array $converted)
);
}

private function reportAnyErrors($responseBody, $status, array $responseHeaders)
private function pauseOnRetry($retries)
{
$delay = (int) (pow(4, $retries) * 100000);
usleep($delay);
}

private function reportAnyErrors($responseBody, $status, array $responseHeaders, $checkFor500 = true)
{
if (false === $responseBody && empty($responseHeaders) && 0 === $status) {
throw new Exceptions\ClientException('Unable to connect to the API, please check your configuration settings');
}

$throw = false;
$exception = array (
'StatusCode' => $status,
Expand All @@ -554,7 +586,22 @@ private function reportAnyErrors($responseBody, $status, array $responseHeaders)
$exception['Severity'] = $xmlBody->Error->Severity;
$exception['Message'] = $xmlBody->Error->Message;
}
} else {

if (isset ($xmlBody->Response)) {
if (isset($xmlBody->Response->Severity)) {
switch ($xmlBody->Response->Severity) {
case E_ERROR:
case E_USER_ERROR:
$throw = true;

$exception['XML'] = $responseBody;
$exception['Severity'] = $xmlBody->Response->Severity;
$exception['Message'] = $xmlBody->Response->Message;
break;
}
}
}
} elseif ($checkFor500) {
if (500 <= $status) {
$throw = true;

Expand Down Expand Up @@ -654,21 +701,24 @@ private function configureCurlOptions(array $converted)

private function addRequiredHttpHeaders($message)
{
$requestDate = gmdate('Y-m-d\TH:i:s\Z');
$timestamp = floor(microtime(true)/30);
$contentType = 'application/xml; charset=UTF-8';

$signatureBase = "POST\n";
$signatureBase .= $contentType . "\n";
$signatureBase .= $requestDate . "\n";
$signatureBase .= 'x-vleksapi-date:' . $requestDate . "\n";
$signatureBase .= $timestamp . "\n";
$signatureBase .= 'x-vleksapi-date:' . $timestamp . "\n";
$signatureBase .= $message;

$signature = sprintf('%s:%s', $this->publicKey, base64_encode(hash_hmac('SHA256', $signatureBase, $this->privateKey, true)));

return array (
'Content-Type: ' . $contentType,
'X-VleksAPI-Date: ' . $requestDate,
'X-VleksAPI-Signature: ' . $signature
'X-VleksAPI-Date: ' . $timestamp,
'X-VleksAPI-Signature: ' . $signature,
'Expect: ',
'Accept: ',
'Transfer-Encoding: chunked'
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Entities/AttributesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ public function __construct($data = null)
parent::__construct($data);
}

public function setAttribute (array $value)
public function setAttribute(array $value)
{
$this->fields['Attribute']['value'] = $value;
return $this;
}

public function getAttribute ()
public function getAttribute()
{
return $this->fields['Attribute']['value'];
}

public function hasAttribute ()
public function hasAttribute()
{
return !is_null($this->fields['Attribute']['value']);
return !empty($this->fields['Attribute']['value']);
}

public function setAttributes (array $value)
public function setAttributes(array $value)
{
return $this->setAttribute($value);
}

public function getAttributes ()
public function getAttributes()
{
return $this->getAttribute();
}

public function hasAttributes ()
public function hasAttributes()
{
return $this->hasAttribute();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/FeedResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getResponse()
*/
public function hasResponse()
{
return !is_null($this->fields['Response']['value']);
return !empty($this->fields['Response']['value']);
}

/**
Expand Down
62 changes: 48 additions & 14 deletions src/Entities/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public function __construct($data = null)
'Status' => array ('value' => null, 'type' => 'string'),
'ChannelID' => array ('value' => null, 'type' => 'string'),
'IsBusinessOrder' => array ('value' => null, 'type' => 'bool'),
'Address' => array ('value' => null, 'type' => AddressData::class),
'IsFulfillmentOrder' => array ('value' => null, 'type' => 'bool'),
'BillingAddress' => array ('value' => null, 'type' => AddressData::class),
'ShippingAddress' => array ('value' => null, 'type' => AddressData::class),
'NumberOfShipments' => array ('value' => null, 'type' => 'int'),
'NumberOfItemsShipped' => array ('value' => null, 'type' => 'int'),
'NumberOfItemsUnshipped' => array ('value' => null, 'type' => 'int'),
'OrderTotal' => array ('value' => null, 'type' => Price::class),
'OrderLines' => array ('value' => array (), 'type' => array (OrderLines::class))
'OrderLines' => array ('value' => null, 'type' => OrderLines::class)
);

parent::__construct($data);
Expand All @@ -43,20 +45,20 @@ public function hasOrderID ()
return !is_null($this->fields['OrderID']['value']);
}

public function setReasonInfo ($value)
public function setReasonCode ($value)
{
$this->fields['ReasonInfo']['value'] = $value;
$this->fields['ReasonCode']['value'] = $value;
return $this;
}

public function getReasonInfo ()
public function getReasonCode ()
{
return $this->fields['ReasonInfo']['value'];
return $this->fields['ReasonCode']['value'];
}

public function hasReasonInfo ()
public function hasReasonCode ()
{
return !is_null($this->fields['ReasonInfo']['value']);
return !is_null($this->fields['ReasonCode']['value']);
}

public function setPurchaseDate ($value)
Expand Down Expand Up @@ -123,20 +125,52 @@ public function hasIsBusinessOrder ()
return !is_null($this->fields['IsBusinessOrder']['value']);
}

public function setAddress ($value)
public function setIsFulfillmentOrder ($value)
{
$this->fields['Address']['value'] = $value;
$this->fields['IsBusinessOrder']['value'] = (bool) $value;
return $this;
}

public function getIsFulfillmentOrder ()
{
return $this->fields['IsBusinessOrder']['value'];
}

public function hasIsFulfillmentOrder ()
{
return !is_null($this->fields['IsBusinessOrder']['value']);
}

public function setBillingAddress ($value)
{
$this->fields['BillingAddress']['value'] = $value;
return $this;
}

public function getBillingAddress ()
{
return $this->fields['BillingAddress']['value'];
}

public function hasBillingAddress ()
{
return !is_null($this->fields['BillingAddress']['value']);
}

public function setShippingAddress ($value)
{
$this->fields['ShippingAddress']['value'] = $value;
return $this;
}

public function getAddress ()
public function getShippingAddress ()
{
return $this->fields['Address']['value'];
return $this->fields['ShippingAddress']['value'];
}

public function hasAddress ()
public function hasShippingAddress ()
{
return !is_null($this->fields['Address']['value']);
return !is_null($this->fields['ShippingAddress']['value']);
}

public function setNumberOfShipments ($value)
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public function getStock ()

public function hasStock ()
{
return !is_null($this->fields['Stock']['value']);
return !empty($this->fields['Stock']['value']);
}

public function setStore ($value)
Expand All @@ -531,6 +531,6 @@ public function getStore ()

public function hasStore ()
{
return !is_null($this->fields['Store']['value']);
return !empty($this->fields['Store']['value']);
}
}
2 changes: 1 addition & 1 deletion src/Entities/StockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function getStockLocation ()

public function hasStockLocation ()
{
return !is_null($this->fields['StockLocation']['value']);
return !empty($this->fields['StockLocation']['value']);
}
}
2 changes: 1 addition & 1 deletion src/Entities/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setTransporterCode ($value)

public function getTransporterCode ()
{
return $this->fields['TransportedCode']['value'];
return $this->fields['TransporterCode']['value'];
}

public function hasTransporterCode()
Expand Down
8 changes: 0 additions & 8 deletions src/Exceptions/RateLimitException.php

This file was deleted.

Loading

0 comments on commit ee93f6d

Please sign in to comment.