Skip to content

Commit

Permalink
Update Curl
Browse files Browse the repository at this point in the history
Update Curl Response Wrapper, improved Response, Fixed Void Response.
  • Loading branch information
Viaprestige committed Apr 8, 2020
1 parent f1892bb commit 9e28bff
Show file tree
Hide file tree
Showing 25 changed files with 198 additions and 49 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jakiboy/apaapi",
"version" : "1.0.3",
"version" : "1.0.4",
"type": "library",
"description": "Amazon Product Advertising API v5",
"keywords": ["php","amazon","product","api","apaapi","paapi5","jakiboy"],
Expand All @@ -11,6 +11,11 @@
"name": "Jihad Sinnaour",
"email": "[email protected]",
"role": "Founder"
},
{
"name": "Softgine",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ItemOperationInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/OperationInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/RequestInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ResponseInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ItemOperation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Operation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/lib/OperationProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Request.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
159 changes: 159 additions & 0 deletions src/lib/RequestClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
*/

namespace Apaapi\lib;

use Apaapi\interfaces\RequestInterface;

/**
* Basic Apaapi Curl Wrapper Class
*/
class RequestClient
{
/**
* @access public
* @var mixed $body Amazon API Response
* @var mixed $error Response Error
*/
public $body = false;
public $error = false;

/**
* @access private
* @var RequestInterface $request
* @var Curl $handler
* @var boolean $forceSSL
*/
private $request;
private $handler;
private $forceSSL = false;

/**
* @param RequestInterface $request
* @param boolean $forceSSL
* @param boolean $failOnError
* @return void
*/
public function __construct(RequestInterface $request, $forceSSL = false)
{
$this->request = $request;
$this->forceSSL = $forceSSL;
$this->init();
}

/**
* @param void
* @return void
*/
private function init()
{
$this->handler = curl_init();
curl_setopt($this->handler, CURLOPT_URL, "https://{$this->request->endpoint}");
curl_setopt($this->handler, CURLOPT_POSTFIELDS, $this->request->params['http']['content']);
curl_setopt($this->handler, CURLOPT_POST, true);
curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->handler, CURLOPT_SSL_VERIFYHOST, $this->forceSSL);
curl_setopt($this->handler, CURLOPT_SSL_VERIFYPEER, $this->forceSSL);
$this->setHeader();
}

/**
* @param void
* @return void
*/
private function close()
{
curl_close($this->handler);
}

/**
* @param void
* @return void
*/
private function setHeader()
{
$header = explode("\n", $this->request->params['http']['header']);
curl_setopt($this->handler, CURLOPT_HTTPHEADER, $header);
}

/**
* @param void
* @return boolean
*/
private function isValid()
{
if ( !curl_errno($this->handler) ) {
return true;
}
return false;
}

/**
* @param void
* @return int
*/
private function getHttpCode()
{
return curl_getinfo($this->handler)['http_code'];
}

/**
* @param void
* @return string
*/
private function getError()
{
return curl_error($this->handler);
}

/**
* @param void
* @return void
*/
private function setHttpError()
{
if ( $this->getHttpCode() == 400 ) {
$this->error = 'The partner tag is not mapped to a valid associate store with your access key';

} elseif ( $this->getHttpCode() == 401 ) {
$this->error = 'The Access Key ID or security token included in the request is invalid';

} elseif ( $this->getHttpCode() == 403 ) {
$this->error = 'Your access key is not mapped to Primary of approved associate store';

} else {
$this->error = 'Unestablished Server Connection';
}
}

/**
* @param void
* @return void
*/
public function getBody()
{
// Exec Curl
$reponse = curl_exec($this->handler);
// Validate curl response
if ( $this->isValid() ) {
// Get Amazon JSON response
if ( $this->getHttpCode() == 200 ) {
$this->body = $reponse;
} else {
// Set Amazon API errors
$this->setHttpError();
}
} else {
// Get Curl errors
$this->error = $this->getError();
}
// End Curl
$this->close();
}
}
2 changes: 1 addition & 1 deletion src/lib/Resource.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
37 changes: 11 additions & 26 deletions src/lib/Response.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand All @@ -13,44 +13,29 @@
use Apaapi\interfaces\RequestInterface;

/**
* Basic Apaapi ResponseCurl Wrapper Class
* @todo Use PSR-7
* Basic Apaapi Response Wrapper Class
*/
class Response implements ResponseInterface
{
/**
* @access public
* @var JSON|False $body, Amazon API Response
* @var mixed $body Amazon API Response
* @var mixed $error Response Error
*/
public $body = false;
public $body = false;
public $error = false;

/**
* @param RequestInterface $request
* @return void
*/
public function __construct(RequestInterface $request)
{
$handler = curl_init();
$header = explode("\n", $request->params['http']['header']);

curl_setopt($handler, CURLOPT_URL, "https://{$request->endpoint}");
curl_setopt($handler, CURLOPT_POSTFIELDS, ($request->params['http']['content']));
curl_setopt($handler, CURLOPT_HTTPHEADER, $header);
curl_setopt($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);

$data = curl_exec($handler);

if ($data) {
if ($data !== false && strpos($data, 'Errors') === false) {
$this->body = $data;
}
else if (strpos($data, 'Errors') !== true) {
$data = json_decode($data);
$this->error = $data->Errors[0]->Message;
}
$response = new RequestClient($request);
$response->getBody();
if ($response->error) {
$this->error = $response->error;
}
$this->body = $response->body;
}
}
2 changes: 1 addition & 1 deletion src/lib/SignatureRequest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/operations/GetBrowseNodes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/operations/GetItems.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/operations/GetVariations.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/operations/SearchItems.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/resources/BrowseNodeInfo.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/resources/BrowseNodes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
2 changes: 1 addition & 1 deletion src/resources/Images.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package Amazon Product Advertising API
* @copyright Copyright (c) 2019 Jakiboy
* @copyright (c) 2019 - 2020 Jakiboy
* @author Jihad Sinnaour <[email protected]>
* @link https://jakiboy.github.io/apaapi/
* @license MIT
Expand Down
Loading

0 comments on commit 9e28bff

Please sign in to comment.