Skip to content

Commit

Permalink
Merge pull request #8 from Shapeways/jw-oauth2-updates
Browse files Browse the repository at this point in the history
Updating composer to allow a higher ext-oauth version
  • Loading branch information
wangjohnnywang committed Feb 11, 2019
2 parents b9e8c08 + c9ccee6 commit a5b72a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"minimum-stability": "stable",
"require": {
"ext-oauth": "1.2.*",
"ext-oauth": ">=1.2.0",
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0"
},
Expand Down
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 a5b72a2

Please sign in to comment.