Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Integration to third party applications #59

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1- Install a web server (for example wamp for windows)

This project is an update to the [Cdiscount/API-MarketPlace-SDK-PHP](https://github.com/Cdiscount/API-MarketPlace-SDK-PHP) parent.

Main Changes include the ability to pass in the username, password, token url and cURL options into the API functionality without the need to modify the supplied `config.ini` file.

The reasons for these changes are to better support frameworks like Laravel and to disuade users from directly editing files in the `vendor` directory.

### 1.1 - Min requirements :

* Apache 2.2
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cdiscount/sdkphpapi",
"name": "autumndev/cdiscount-sdkphpapi",
"description": "PHP SDK for the Cdiscount Marketplace API",
"type": "project",
"type": "library",
"autoload": {
"files": ["sdk/autoload.php"]
},
Expand All @@ -14,5 +14,5 @@
"zendframework/zend-config": "^2.6"
},
"license": "GPL-3.0",
"version": "1.0.8"
"version": "1.1.10"
}
1 change: 1 addition & 0 deletions sdk/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
require_once __DIR__ . '/src/public/ApiClient/CDSApiClient.php';

require_once __DIR__ . '/src/public/Common/Filter.php';
require_once __DIR__ . '/src/public/AbstractPoint.php';


require_once __DIR__ . '/src/public/Discussion/DiscussionFilter.php';
Expand Down
29 changes: 22 additions & 7 deletions sdk/src/core/Auth/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Token
* @var string Token to communicate with the API
*/
private $_token = null;
/**
* @var string
*/
private $username = null;
private $password = null;
private $tokenUrl = null;

#endregion Private attributes

Expand All @@ -48,10 +54,13 @@ class Token
/**
* Token constructor.
*/
private function __construct()
private function __construct(string $username = null, string $password = null, string $tokenUrl = null)
{
$this->_isValid = false;
$this->_initdate = false;
$this->username = $username;
$this->password = $password;
$this->tokenUrl = $tokenUrl;
}

#endregion Constructor
Expand All @@ -62,11 +71,11 @@ private function __construct()
* Return a unique instance of the token class, initiate it if needed
* @return Token
*/
public static function getInstance()
public static function getInstance(string $username = null, string $password = null, string $tokenUrl = null)
{

if (is_null(self::$_instance)) {
self::$_instance = new Token();
self::$_instance = new Token($username, $password, $tokenUrl);
}
return self::$_instance;
}
Expand All @@ -77,11 +86,17 @@ public static function getInstance()

private function _generateNewToken()
{
$username = ConfigFileLoader::getInstance()->getConfAttribute('username');
$password = ConfigFileLoader::getInstance()->getConfAttribute('password');
$urlToken = ConfigFileLoader::getInstance()->getConfAttribute('urltoken');
if (null === $this->username) {
$this->username = ConfigFileLoader::getInstance()->getConfAttribute('username');
}
if (null === $this->password) {
$this->password = ConfigFileLoader::getInstance()->getConfAttribute('password');
}
if (null === $this->tokenUrl) {
$this->tokenUrl = ConfigFileLoader::getInstance()->getConfAttribute('urltoken');
}

$request = new CDSApiRequest($username, $password, $urlToken);
$request = new CDSApiRequest($this->username, $this->password, $this->tokenUrl);

libxml_use_internal_errors(true);
$xmlResult = simplexml_load_string($request->execute());
Expand Down
33 changes: 18 additions & 15 deletions sdk/src/core/HttpTools/CDSApiSoapRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class CDSApiSoapRequest
*/
private $_header = null;

private $curlOptions = [
CURLOPT_VERBOSE => false,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_SSLVERSION => 4,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 600
];

/**
* CDSApiSoapRequest constructor.
*
Expand All @@ -39,9 +49,9 @@ class CDSApiSoapRequest
* @param $apiURL
* @param $data
*/
public function __construct($method, $headerMethodURL, $apiURL, $data)
public function __construct($method, $headerMethodURL, $apiURL, $data, array $curlOptions = [])
{

$this->curlOptions = $curlOptions + $this->curlOptions;
$this->_client = new \Zend\Http\Client($apiURL);
$this->_client->setMethod('post');
$this->_client->setRawBody($data);
Expand All @@ -60,19 +70,12 @@ public function __construct($method, $headerMethodURL, $apiURL, $data)
* @param $url
*/
private function _setAdapaterOptions($data, $url)
{
{
$this->curlOptions[CURLOPT_URL] = $url;
$this->curlOptions[CURLOPT_POSTFIELDS] = $data;

$this->_adapter->setOptions(array(
'curloptions' => array(
CURLOPT_URL => $url,
CURLOPT_VERBOSE => false,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_SSLVERSION => 4,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_TIMEOUT => 600
)
'curloptions' => $this->curlOptions,
));
}

Expand All @@ -85,4 +88,4 @@ public function call()
return $response->getBody();
}

}
}
49 changes: 49 additions & 0 deletions sdk/src/public/AbstractPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Sdk;

use Sdk\ConfigTools\ConfigFileLoader;
use Sdk\HttpTools\CDSApiSoapRequest;

abstract class AbstractPoint
{
/**
* api end point urls
*
* @var string
*/
protected $headerRequestURL = null;
protected $apiUrl = null;
/**
* curl options
*
* @var array
*/
protected $curlOptions = [];

public function __construct(string $headerRequestURL = null, string $apiUrl = null, array $curlOptions = [])
{
$this->curlOptions = $curlOptions;
$this->headerRequestURL = $headerRequestURL;
if (null === $this->headerRequestURL) {
$this->headerRequestURL = ConfigFileLoader::getInstance()->getConfAttribute('methodurl');
}

$this->apiUrl = $apiUrl;
if (null === $this->apiUrl) {
$this->apiUrl = ConfigFileLoader::getInstance()->getConfAttribute('url');
}
}
/**
* @param $method
* @param $data
* @return string
*/
protected function _sendRequest($method, $data)
{
$request = new CDSApiSoapRequest($method, $this->headerRequestURL, $this->apiUrl, $data, $this->curlOptions);
$response = $request->call();

return $response;
}
}
30 changes: 19 additions & 11 deletions sdk/src/public/ApiClient/CDSApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ class CDSApiClient
*/
private $_sellerPoint = null;

private $apiUrl = null;
private $methodUrl = null;

private $curlOptions = [];

/**
* @return SellerPoint
*/
public function getSellerPoint()
{
if ($this->_sellerPoint == null) {
$this->_sellerPoint = new SellerPoint();
$this->_sellerPoint = new SellerPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_sellerPoint;
}
Expand All @@ -52,7 +57,7 @@ public function getSellerPoint()
public function getOrderPoint()
{
if ($this->_orderPoint == null) {
$this->_orderPoint = new OrderPoint();
$this->_orderPoint = new OrderPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_orderPoint;
}
Expand All @@ -68,7 +73,7 @@ public function getOrderPoint()
public function getOfferPoint()
{
if ($this->_offerPoint == null) {
$this->_offerPoint = new OfferPoint();
$this->_offerPoint = new OfferPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_offerPoint;
}
Expand All @@ -84,7 +89,7 @@ public function getOfferPoint()
public function getProductPoint()
{
if ($this->_productPoint == null) {
$this->_productPoint = new ProductPoint();
$this->_productPoint = new ProductPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_productPoint;
}
Expand All @@ -100,7 +105,7 @@ public function getProductPoint()
public function getFulfilmentPoint()
{
if ($this->_fulfilmentPoint == null) {
$this->_fulfilmentPoint = new FulfilmentPoint();
$this->_fulfilmentPoint = new FulfilmentPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_fulfilmentPoint;
}
Expand All @@ -116,7 +121,7 @@ public function getFulfilmentPoint()
public function getDiscussionPoint()
{
if ($this->_discussionPoint == null) {
$this->_discussionPoint = new DiscussionPoint();
$this->_discussionPoint = new DiscussionPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_discussionPoint;
}
Expand All @@ -132,7 +137,7 @@ public function getDiscussionPoint()
public function getRelaysPoint()
{
if ($this->_relaysPoint == null) {
$this->_relaysPoint = new RelaysPoint();
$this->_relaysPoint = new RelaysPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_relaysPoint;
}
Expand All @@ -148,17 +153,20 @@ public function getRelaysPoint()
public function getMailPoint()
{
if ($this->_mailPoint == null) {
$this->_mailPoint = new MailPoint();
$this->_mailPoint = new MailPoint($this->methodUrl, $this->apiUrl, $this->curlOptions);
}
return $this->_mailPoint;
}

/**
* Create and check the token
*/
public function init()
public function init(string $username = null, string $password = null, string $tokenUrl = null, string $apiUrl = null, string $methodUrl = null, array $curlOptions = [])
{
$token = Token::getInstance()->getToken();
$this->apiUrl = $apiUrl;
$this->methodUrl = $methodUrl;
$this->curlOptions = $curlOptions;
$token = Token::getInstance($username, $password, $tokenUrl)->getToken();
return $token;
}

Expand All @@ -169,4 +177,4 @@ public function isTokenValid()
{
return Token::getInstance()->isTokenValid();
}
}
}
25 changes: 2 additions & 23 deletions sdk/src/public/Discussion/DiscussionPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace Sdk\Discussion;

use Sdk\ConfigTools\ConfigFileLoader;
use Sdk\HttpTools\CDSApiSoapRequest;
use Sdk\AbstractPoint;
use Sdk\Soap\Common\Body;
use Sdk\Soap\Common\Envelope;
use Sdk\Soap\Discussion\ClaimFilterSoap;
Expand All @@ -25,7 +24,7 @@
use Sdk\Soap\Discussion\Response\GetOrderQuestionListResponse;
use Sdk\Soap\HeaderMessage\HeaderMessage;

class DiscussionPoint
class DiscussionPoint extends AbstractPoint
{

/**
Expand Down Expand Up @@ -151,24 +150,4 @@ private function _buildGenericListXML($questionList, $filterSoap, $filter, $name

return $envelopeXML;
}

/**
* @param $method
* @param $data
* @return string
*/
private function _sendRequest($method, $data)
{
$headerRequestURL = ConfigFileLoader::getInstance()->getConfAttribute('methodurl');

$apiURL = ConfigFileLoader::getInstance()->getConfAttribute('url');

$request = new CDSApiSoapRequest($method, $headerRequestURL, $apiURL, $data);
$response = $request->call();

return $response;
}



}
Loading