Skip to content

Commit

Permalink
be able to use a different baseApiUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjohnnywang committed Feb 8, 2019
1 parent 7061f15 commit c9ccee6
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/Oauth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Oauth2Client
private $baseApiUrl = 'https://api.shapeways.com';

/**
* @var string $endpointVersion the default API endpoint version used to generate api urls
* @var string $apiVersion the api version used to generate api urls
*/
public $endpointVersion = 'v1';
public $apiVersion = 'v1';

/**
* Create a new \Shapeways\Oauth2CurlClient
Expand All @@ -56,19 +56,24 @@ class Oauth2Client
* @param string|null $redirectUrl your app callback url
* @param string|null $accessToken a users oauth token if it is already known
* @param string|null $refreshToken if it is already known
* @param string|null $baseApiUrl if it you want to use a different baseApiUrl
*/
public function __construct(
$clientId,
$clientSecret,
$redirectUrl = null,
$accessToken = null,
$refreshToken = null
$refreshToken = null,
$baseApiUrl = null
) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->redirectUrl = $redirectUrl;
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
if ($baseApiUrl !== null) {
$this->baseApiUrl = $baseApiUrl;
}
}

/**
Expand All @@ -80,7 +85,7 @@ public function __construct(
*
* Use "access_token" from result for other API calls
*
* @return array - json decoded api response
* @return object - json decoded api response
*/
public function generateAccessTokenClientCredentialGrant()
{
Expand All @@ -89,7 +94,7 @@ public function generateAccessTokenClientCredentialGrant()
);

$url = $this->baseApiUrl . '/oauth2/token';
return $this->postRequest($url, $params, array(), array($this->clientId, $this->clientSecret));
return $this->_post($url, $params, array(), array($this->clientId, $this->clientSecret));
}


Expand Down Expand Up @@ -137,7 +142,7 @@ public function handleAuthorizationGrantCallback($code)
);

$url = $this->baseApiUrl . '/oauth2/token';
return $this->postRequest($url, $params);
return $this->_post($url, $params);
}

/**
Expand All @@ -160,8 +165,8 @@ public function uploadModel($params)

$params['file'] = rawurlencode(base64_encode($params['file']));

$url = $this->baseApiUrl . '/models/' . $this->endpointVersion;
return $this->postRequest($url, $params,
$url = $this->baseApiUrl . '/models/' . $this->apiVersion;
return $this->_post($url, $params,
array('Authorization' => 'Bearer ' . $this->accessToken, 'Content-type' => 'application/json'));
}

Expand All @@ -175,9 +180,9 @@ public function uploadModel($params)
*/
public function getModelInfo($modelId)
{
$url = $this->baseApiUrl . '/models/' . $modelId . '/' . $this->endpointVersion;
$url = $this->baseApiUrl . '/models/' . $modelId . '/' .$this->apiVersion;

return $this->getRequest($url);
return $this->_get($url);
}


Expand All @@ -190,9 +195,9 @@ public function getModelInfo($modelId)
*/
public function getMaterials()
{
$url = $this->baseApiUrl . '/materials/' . $this->endpointVersion;
$url = $this->baseApiUrl . '/materials/'. $this->apiVersion;

return $this->getRequest($url);
return $this->_get($url);
}

/**
Expand Down Expand Up @@ -225,8 +230,8 @@ public function placeOrder($params)
}
}

$url = $this->baseApiUrl . '/orders/' . $this->endpointVersion;
return $this->postRequest($url, $params,
$url = $this->baseApiUrl . '/orders/' . $this->apiVersion;
return $this->_post($url, $params,
array('Authorization' => 'Bearer ' . $this->accessToken, 'Content-type' => 'application/json'));
}

Expand All @@ -241,20 +246,18 @@ public function placeOrder($params)
*/
public function getOrderInfo($oderId)
{
$url = $this->baseApiUrl . '/orders/' . $oderId . '/' . $this->endpointVersion;
return $this->getRequest($url);
$url = $this->baseApiUrl . '/orders/' . $oderId . '/' . $this->apiVersion;
return $this->_get($url);
}

/**
* http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
*
* @param $url
* @param array $params
* @param array $headers
* @param array $auth
* @return mixed
*/
private function postRequest($url, $params = array(), $headers = array(), $auth = array()) {
private function _post($url, $params = array(), $headers = array(), $auth = array()) {
$client = new \GuzzleHttp\Client();
if (array_key_exists('Content-type', $headers) && $headers['Content-type'] == 'application/json') {
$postOptions = array(\GuzzleHttp\RequestOptions::JSON => $params);
Expand All @@ -279,12 +282,10 @@ private function postRequest($url, $params = array(), $headers = array(), $auth
}

/**
* http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
*
* @param $url
* @return mixed
*/
private function getRequest($url)
private function _get($url)
{
$client = new \GuzzleHttp\Client();
try {
Expand Down

0 comments on commit c9ccee6

Please sign in to comment.