Skip to content

Commit

Permalink
Merge pull request #2 from dpdconnect/feature/meta-data
Browse files Browse the repository at this point in the history
Updated SDK
  • Loading branch information
dpdplugin authored Jun 14, 2019
2 parents 1c339b7 + f236b25 commit 1394f81
Show file tree
Hide file tree
Showing 16 changed files with 708 additions and 79 deletions.
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dpdconnect/php-sdk",
"description": "DPD Connect PHP SDK",
"type": "library",
"description": "DPD Magento2 Shipping extension",
"type": "magento2-library",
"license": "OSL-3.0",
"authors": [
{
Expand All @@ -19,10 +19,14 @@
"DpdConnect\\Sdk\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"DpdConnect\\Sdk\\Test\\": "tests/"
}
},
"require": {
"php": "^5.5 || ^7.0",
"ext-json": "*",
"ext-curl": "*"
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~6.5.0",
Expand Down
8 changes: 8 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function __construct(
$this->country = new Country($this->resourceClient);
}

/**
* @return Authentication
*/
public function getAuthentication()
{
return $this->authentication;
}

/**
* @return Shipment
*/
Expand Down
10 changes: 6 additions & 4 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class ClientBuilder implements ClientBuilderInterface
/**
* @param string $endpoint
*/
public function __construct($endpoint = null, $meta = null)
public function __construct($endpoint = null, $meta = [])
{
if (!is_null($endpoint)) {
if (!is_null($endpoint) && $endpoint !== '') {
$this->endpoint = $endpoint;
} else {
$this->endpoint = Client::ENDPOINT;
}

$this->meta = $meta;
Expand All @@ -51,7 +53,7 @@ public function getEndpoint()
private function getHttpClient()
{
if (null === $this->httpClient) {
$this->httpClient = new HttpClient($this->endpoint, $this->meta);
$this->httpClient = new HttpClient($this->endpoint);
}

return $this->httpClient;
Expand All @@ -64,7 +66,6 @@ private function getHttpClient()
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;

return $this;
}

Expand Down Expand Up @@ -125,6 +126,7 @@ protected function setUp(Authentication $authentication)
$authentication
);


$resourceClient = new ResourceClient(
$authenticatedHttpClient
);
Expand Down
3 changes: 2 additions & 1 deletion src/Common/AuthenticatedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
->setRefreshToken($tokens['refresh_token']);

$headers[] = sprintf('Authorization: Bearer %s', $this->authentication->getAccessToken());
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body);
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body)
->setAuthentication($this->authentication);
}

return $response;
Expand Down
25 changes: 19 additions & 6 deletions src/Common/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HttpClient implements HttpClientInterface
/**
* @var Common\Authentication
*/
protected $Authentication;
protected $authentication;

/**
* @var int
Expand All @@ -41,7 +41,7 @@ class HttpClient implements HttpClientInterface
/**
* @var int
*/
private $connectionTimeout = 2;
private $connectionTimeout = 10;

/**
* @var array
Expand All @@ -53,13 +53,18 @@ class HttpClient implements HttpClientInterface
*/
private $httpOptions = [];

/**
* @var array
*/
private $meta;

/**
* @param string $endpoint
* @param int $timeout > 0
* @param int $connectionTimeout >= 0
* @param array $headers
*/
public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2, $headers = [])
public function __construct($endpoint, $timeout = 10, $connectionTimeout = 10, $headers = [])
{
$this->endpoint = $endpoint;

Expand All @@ -85,6 +90,14 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2, $h
$this->headers = $headers;
}

/**
* @param array $meta
*/
public function setMeta(array $meta = [])
{
$this->meta = $meta;
}

/**
* @param string $userAgent
*/
Expand All @@ -96,9 +109,9 @@ public function addUserAgentString($userAgent)
/**
* @param Common\Authentication $Authentication
*/
public function setAuthentication(Common\Authentication $Authentication)
public function setAuthentication(Common\Authentication $authentication)
{
$this->Authentication = $Authentication;
$this->authentication = $authentication;
}

/**
Expand Down Expand Up @@ -161,7 +174,7 @@ public function sendRequest($method, $resourceName, $query = null, $headers = []
{
$curl = curl_init();

// if ($this->Authentication === null) {
// if ($this->authentication === null) {
// throw new Exceptions\AuthenticateException('Can not perform API Request without Authentication');
// }

Expand Down
5 changes: 5 additions & 0 deletions src/Common/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

interface HttpClientInterface
{
/**
* @param array $meta
*/
public function setMeta(array $meta = []);

/**
* @param string $userAgent
*/
Expand Down
23 changes: 18 additions & 5 deletions src/Common/ResponseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class ResponseError implements \JsonSerializable

const NOT_FOUND = 20;

const NOT_ENOUGH_CREDIT = 25;
const NOT_ENOUGH_CREDIT = 'Bad credentials';

const CHAT_API_AUTH_ERROR = 1001;

const ACCESS_DENIED = 401;

public $errors = [];
public $validation = [];

Expand All @@ -43,8 +45,8 @@ public function __construct($body, $validation = [])
if (!empty($body['errors'])) {
foreach ($body['errors'] as $error) {
if (isset($error['code'])) {
if ($error['code'] === self::NOT_ENOUGH_CREDIT) {
throw new Exceptions\BalanceException($this->getExceptionMessage($error));
if ($error['code'] === self::ACCESS_DENIED) {
throw new Exceptions\AuthenticateException($this->getExceptionMessage($error));
} elseif ($error['code'] === self::REQUEST_NOT_ALLOWED) {
throw new Exceptions\AuthenticateException($this->getExceptionMessage($error));
} elseif ($error['code'] === self::CHAT_API_AUTH_ERROR) {
Expand All @@ -58,6 +60,18 @@ public function __construct($body, $validation = [])

return;
}

if (isset($body['code'])) {
if ($body['code'] === self::ACCESS_DENIED) {
throw new Exceptions\AuthenticateException($body['message']);
} elseif ($body['code'] === self::REQUEST_NOT_ALLOWED) {
throw new Exceptions\AuthenticateException($body['message']);
} elseif ($body['code'] === self::CHAT_API_AUTH_ERROR) {
throw new Exceptions\AuthenticateException($body['message']);
} else {
}
}

if (!empty($body['message'])) {
$this->errors[] = $body['message'];
}
Expand Down Expand Up @@ -96,8 +110,7 @@ public function getErrorString()
}
}


return implode(', ', $errorDescriptions);
return print_r($errorDescriptions, true);
}

/**
Expand Down
Loading

0 comments on commit 1394f81

Please sign in to comment.