Skip to content

Commit

Permalink
1.1.7 Check API key before creating a shipment
Browse files Browse the repository at this point in the history
  • Loading branch information
Reindert committed Jul 17, 2017
1 parent ede441e commit 58a234b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "myparcelnl/sdk",
"version": "v1.1.6",
"version": "v1.1.7",
"description": "This package is designed to send and receive data from MyParcel by means of an API.",
"homepage": "https://www.myparcel.nl",
"tags": ["MyParcel", "My Parcel", "Post NL", "PostNL"],
Expand Down
1 change: 1 addition & 0 deletions src/AutoLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
include_once ('Helper/MyParcelCollection.php');
include_once ('Helper/MyParcelCurl.php');
include_once ('Services/CheckApiKeyService.php');
include_once ('Model/MyParcelRequest.php');
include_once ('Model/MyParcelClassConstants.php');
include_once ('Model/MyParcelConsignment.php');
Expand Down
5 changes: 2 additions & 3 deletions src/Model/MyParcelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MyParcelRequest
/**
* Supported request types.
*/
const REQUEST_TYPE_CREATE_CONSIGNMENT = 'shipments';
const REQUEST_TYPE_SHIPMENTS = 'shipments';
const REQUEST_TYPE_RETRIEVE_LABEL = 'shipment_labels';

/**
Expand Down Expand Up @@ -98,7 +98,7 @@ public function setRequestParameters($apiKey, $body = '', $requestHeader = '')
* @return MyParcelRequest|array|false|string
* @throws \Exception
*/
public function sendRequest($method = 'POST', $uri = self::REQUEST_TYPE_CREATE_CONSIGNMENT)
public function sendRequest($method = 'POST', $uri = self::REQUEST_TYPE_SHIPMENTS)
{
if (!$this->checkConfigForRequest()) {
return false;
Expand Down Expand Up @@ -198,7 +198,6 @@ public function sendRequest($method = 'POST', $uri = self::REQUEST_TYPE_CREATE_C
$request->close();

if ($this->getError()) {
throw new \Exception('Error in MyParcel API request: ' . $this->getError() . '. Url: ' . $url . ' Request: ' . $this->body);
}

return $this;
Expand Down
67 changes: 67 additions & 0 deletions src/Services/CheckApiKeyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* A services to check if the API-key is correct
*
* If you want to add improvements, please create a fork in our GitHub:
* https://github.com/myparcelnl
*
* @author Reindert Vetter <[email protected]>
* @copyright 2010-2017 MyParcel
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
* @link https://github.com/myparcelnl/sdk
* @since File available since Release v1.1.7
*/

namespace MyParcelNL\Sdk\src\Services;

use MyParcelNL\Sdk\src\Model\MyParcelRequest;

class CheckApiKeyService
{
private $api_key;

/**
* @return mixed
*/
public function getApiKey()
{
return $this->api_key;
}

/**
* @param mixed $api_key
*/
public function setApiKey($api_key)
{
$this->api_key = $api_key;

return $this;
}

public function apiKeyIsCorrect()
{
try {
$request = (new MyParcelRequest());

$userAgent = $request->getUserAgentFromComposer();
$request
->setUserAgent($userAgent)
->setRequestParameters(
$this->getApiKey(),
'',
MyParcelRequest::REQUEST_HEADER_RETRIEVE_SHIPMENT
)
->sendRequest('GET');

if ($request->getResult() === null) {
throw new \Exception('Unable to connect to MyParcel.');
}
} catch (\Exception $exception) {
if (strpos($exception, 'Access Denied') > 1) {
return false;
}
}

return true;
}
}

0 comments on commit 58a234b

Please sign in to comment.