From 913c33a8665920b59ed80f39e6f2eccf1672e0fb Mon Sep 17 00:00:00 2001 From: Brian Fenton Date: Tue, 26 May 2015 10:18:04 -0700 Subject: [PATCH 1/5] Reimplementing PR 88 with corrections for underlying code drift More doc cleanup Removed unused "isComplete" property Split auth methods into basic and OAuth versions --- src/Bigcommerce/Api/Client.php | 75 +++++++++++++++++++++++++++++- src/Bigcommerce/Api/Connection.php | 67 ++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 11 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index c689bb20..21ad263e 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -57,6 +57,58 @@ class Client * @var string */ static public $api_path; + static private $client_id; + static private $store_hash; + static private $auth_token; + static private $stores_prefix = '/stores/%s/v2'; + static private $api_url = 'https://api.bigcommerce.com'; + static private $login_url = 'https://login.bigcommerce.com'; + + /** + * Configure the API client with the required settings to access + * the API for a store. + * + * Accepts both OAuth and Basic Auth credentials + * + * @param array $settings + */ + public static function configure($settings) + { + if (isset($settings['client_id'])) { + self::configureOAuth($settings); + } else { + self::configureBasicAuth($settings); + } + } + + /** + * Configure the API client with the required OAuth credentials. + * + * Requires a settings array to be passed in with the following keys: + * + * - client_id + * - auth_token + * - store_hash + * + * @param array $settings + * @throws \Exception + */ + public static function configureOAuth($settings) + { + if (!isset($settings['auth_token'])) { + throw new Exception("'auth_token' must be provided"); + } + + if (!isset($settings['store_hash'])) { + throw new Exception("'store_hash' must be provided"); + } + + self::$client_id = $settings['client_id']; + self::$auth_token = $settings['auth_token']; + self::$store_hash = $settings['store_hash']; + self::$api_path = self::$api_url . sprintf(self::$stores_prefix, self::$store_hash); + self::$connection = false; + } /** * Configure the API client with the required credentials. @@ -70,7 +122,7 @@ class Client * @param array $settings * @throws \Exception */ - public static function configure(array $settings) + public static function configureBasicAuth(array $settings) { if (!isset($settings['store_url'])) { throw new Exception("'store_url' must be provided"); @@ -162,7 +214,11 @@ private static function connection() { if (!self::$connection) { self::$connection = new Connection(); - self::$connection->authenticate(self::$username, self::$api_key); + if (self::$client_id) { + self::$connection->authenticateOauth(self::$client_id, self::$auth_token); + } else { + self::$connection->authenticateBasic(self::$username, self::$api_key); + } } return self::$connection; @@ -342,6 +398,21 @@ private static function mapCount($object) return $object->count; } + /** + * Swaps a temporary access code for a long expiry auth token. + * + * @param \stdClass $object + * @return \stdClass + */ + public static function getAuthToken($object) + { + $context = array_merge(array('grant_type' => 'authorization_code'), (array)$object); + $connection = new Connection(); + $connection->useUrlEncoded(); + + return $connection->post(self::$login_url . '/oauth2/token', $context); + } + /** * Pings the time endpoint to test the connection to a store. * diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 84bdb198..5bec4769 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -7,6 +7,18 @@ */ class Connection { + /** + * XML media type. + */ + const MEDIA_TYPE_XML = 'application/xml'; + /** + * JSON media type. + */ + const MEDIA_TYPE_JSON = 'application/json'; + /** + * Default urlencoded media type. + */ + const MEDIA_TYPE_WWW = 'application/x-www-form-urlencoded'; /** * @var resource cURL resource @@ -60,15 +72,19 @@ class Connection /** * Deal with failed requests if failOnError is not set. - * @var string | false + * @var string|false */ private $lastError = false; /** - * Determines whether requests and responses should be treated - * as XML. Defaults to false (using JSON). + * Determines whether the response body should be returned as a raw string. */ - private $useXml = false; + private $rawResponse = false; + + /** + * Determines the default content type to use with requests and responses. + */ + private $contentType; /** * Initializes the connection object. @@ -96,7 +112,23 @@ public function __construct() */ public function useXml($option = true) { - $this->useXml = $option; + if ($option) { + $this->contentType = self::MEDIA_TYPE_XML; + $this->rawResponse = true; + } + } + + /** + * Controls whether requests or responses should be treated + * as urlencoded form data. + * + * @param bool $option the new state of this feature + */ + public function useUrlEncoded($option = true) + { + if ($option) { + $this->contentType = self::MEDIA_TYPE_WWW; + } } /** @@ -125,11 +157,23 @@ public function failOnError($option = true) * @param string $username * @param string $password */ - public function authenticate($username, $password) + public function authenticateBasic($username, $password) { curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password"); } + /** + * Sets Oauth authentication headers + * + * @param string $clientId + * @param string $authToken + */ + public function authenticateOauth($clientId, $authToken) + { + $this->addHeader('X-Auth-Client', $clientId); + $this->addHeader('X-Auth-Token', $authToken); + } + /** * Set a default timeout for the request. The client will error if the * request takes longer than this to respond. @@ -168,6 +212,9 @@ public function verifyPeer($option = false) /** * Add a custom header to the request. + * + * @param string $header + * @param string $value */ public function addHeader($header, $value) { @@ -176,6 +223,7 @@ public function addHeader($header, $value) /** * Remove a header from the request. + * * @param string $header */ public function removeHeader($header) @@ -185,10 +233,12 @@ public function removeHeader($header) /** * Get the MIME type that should be used for this request. + * + * Defaults to application/json */ private function getContentType() { - return ($this->useXml) ? 'application/xml' : 'application/json'; + return ($this->contentType) ? $this->contentType : self::MEDIA_TYPE_JSON; } /** @@ -197,7 +247,6 @@ private function getContentType() */ private function initializeRequest() { - $this->isComplete = false; $this->responseBody = ''; $this->responseHeaders = array(); $this->lastError = false; @@ -222,7 +271,7 @@ private function handleResponse() throw new NetworkError(curl_error($this->curl), curl_errno($this->curl)); } - $body = ($this->useXml) ? $this->getBody() : json_decode($this->getBody()); + $body = ($this->rawResponse) ? $this->getBody() : json_decode($this->getBody()); $status = $this->getStatus(); From 03fccfc4052eb9370cbf6a75e5d0f199826e70f9 Mon Sep 17 00:00:00 2001 From: Brian Fenton Date: Tue, 26 May 2015 11:54:08 -0700 Subject: [PATCH 2/5] removing basic auth from the client in favor of oauth. including removing no longer used properties --- src/Bigcommerce/Api/Client.php | 98 +++++--------------------- src/Bigcommerce/Api/Connection.php | 17 ++--- test/Unit/Api/ClientTest.php | 108 +++++++++++++++++------------ 3 files changed, 85 insertions(+), 138 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 21ad263e..1104262d 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -9,27 +9,6 @@ */ class Client { - /** - * Full Store URL to connect to - * - * @var string - */ - static private $store_url; - - /** - * Username to connect to the store API with - * - * @var string - */ - static private $username; - - /** - * API key - * - * @var string - */ - static private $api_key; - /** * Connection instance * @@ -45,41 +24,37 @@ class Client static private $resource; /** - * API path prefix to be added to store URL for requests + * Full URL path to the configured store API. * * @var string */ - static private $path_prefix = '/api/v2'; - + static public $api_path; /** - * Full URL path to the configured store API. + * Client ID from app installation * * @var string */ - static public $api_path; static private $client_id; static private $store_hash; static private $auth_token; + /** + * Appended to API URL to create API endpoints + * + * @var string + */ static private $stores_prefix = '/stores/%s/v2'; - static private $api_url = 'https://api.bigcommerce.com'; - static private $login_url = 'https://login.bigcommerce.com'; - /** - * Configure the API client with the required settings to access - * the API for a store. + * Base URL for the Bigcommerce API * - * Accepts both OAuth and Basic Auth credentials + * @var string + */ + static private $api_url = 'https://api.bigcommerce.com'; + /** + * Endpoint for getting tokens * - * @param array $settings + * @var string */ - public static function configure($settings) - { - if (isset($settings['client_id'])) { - self::configureOAuth($settings); - } else { - self::configureBasicAuth($settings); - } - } + static private $login_url = 'https://login.bigcommerce.com'; /** * Configure the API client with the required OAuth credentials. @@ -93,7 +68,7 @@ public static function configure($settings) * @param array $settings * @throws \Exception */ - public static function configureOAuth($settings) + public static function configure($settings) { if (!isset($settings['auth_token'])) { throw new Exception("'auth_token' must be provided"); @@ -110,39 +85,6 @@ public static function configureOAuth($settings) self::$connection = false; } - /** - * Configure the API client with the required credentials. - * - * Requires a settings array to be passed in with the following keys: - * - * - store_url - * - username - * - api_key - * - * @param array $settings - * @throws \Exception - */ - public static function configureBasicAuth(array $settings) - { - if (!isset($settings['store_url'])) { - throw new Exception("'store_url' must be provided"); - } - - if (!isset($settings['username'])) { - throw new Exception("'username' must be provided"); - } - - if (!isset($settings['api_key'])) { - throw new Exception("'api_key' must be provided"); - } - - self::$username = $settings['username']; - self::$api_key = $settings['api_key']; - self::$store_url = rtrim($settings['store_url'], '/'); - self::$api_path = self::$store_url . self::$path_prefix; - self::$connection = false; - } - /** * Configure the API client to throw exceptions when HTTP errors occur. * @@ -214,11 +156,7 @@ private static function connection() { if (!self::$connection) { self::$connection = new Connection(); - if (self::$client_id) { - self::$connection->authenticateOauth(self::$client_id, self::$auth_token); - } else { - self::$connection->authenticateBasic(self::$username, self::$api_key); - } + self::$connection->authenticate(self::$client_id, self::$auth_token); } return self::$connection; diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 5bec4769..b642287c 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -152,23 +152,12 @@ public function failOnError($option = true) } /** - * Sets the HTTP basic authentication. - * - * @param string $username - * @param string $password - */ - public function authenticateBasic($username, $password) - { - curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password"); - } - - /** - * Sets Oauth authentication headers + * Sets OAuth authentication headers * * @param string $clientId * @param string $authToken */ - public function authenticateOauth($clientId, $authToken) + public function authenticate($clientId, $authToken) { $this->addHeader('X-Auth-Client', $clientId); $this->addHeader('X-Auth-Token', $authToken); @@ -530,6 +519,8 @@ public function getBody() /** * Access given header from the response. * + * @param string $header + * * @return string|void */ public function getHeader($header) diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index 960d3897..79ee8d26 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -46,26 +46,20 @@ public function setUp() public function tearDown() { - Client::configure(array('username' => '', 'api_key' => '', 'store_url' => '')); + Client::configure(array('client_id' => '', 'auth_token' => '', 'store_hash' => '')); unset($this->connection); } - public function testConfigureRequiresStoreUrl() + public function testConfigureRequiresAuthToken() { - $this->setExpectedException('\\Exception', "'store_url' must be provided"); - Client::configure(array('username' => 'whatever', 'api_key' => 'whatever')); + $this->setExpectedException('\\Exception', "'auth_token' must be provided"); + Client::configure(array('client_id' => 'whatever', 'store_hash' => 'whatever')); } - public function testConfigureRequiresUsername() + public function testConfigureRequiresStoreHash() { - $this->setExpectedException('\\Exception', "'username' must be provided"); - Client::configure(array('store_url' => 'whatever', 'api_key' => 'whatever')); - } - - public function testConfigureRequiresApiKey() - { - $this->setExpectedException('\\Exception', "'api_key' must be provided"); - Client::configure(array('username' => 'whatever', 'store_url' => 'whatever')); + $this->setExpectedException('\\Exception', "'store_hash' must be provided"); + Client::configure(array('client_id' => 'whatever', 'auth_token' => 'whatever')); } public function testFailOnErrorPassesThroughToConnection() @@ -122,10 +116,14 @@ public function testGetResourceReturnsSpecifiedType() { $this->connection->expects($this->once()) ->method('get') - ->with('http://storeurl' . $this->basePath . '/whatever', false) + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) ->will($this->returnValue(array(array()))); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $resource = Client::getResource('/whatever'); $this->assertInstanceOf('Bigcommerce\\Api\\Resource', $resource); @@ -135,10 +133,14 @@ public function testGetCountReturnsSpecifiedCount() { $this->connection->expects($this->once()) ->method('get') - ->with('http://storeurl' . $this->basePath . '/whatever', false) + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) ->will($this->returnValue((object)array('count' => 5))); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $count = Client::getCount('/whatever'); $this->assertSame(5, $count); @@ -148,10 +150,14 @@ public function testGetCollectionReturnsCollectionOfSpecifiedTypes() { $this->connection->expects($this->once()) ->method('get') - ->with('http://storeurl' . $this->basePath . '/whatever', false) + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) ->will($this->returnValue(array(array(), array()))); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $resources = Client::getCollection('/whatever'); $this->assertInternalType('array', $resources); @@ -165,10 +171,14 @@ public function testCreateResourcePostsToTheRightPlace() $new = array(rand() => rand()); $this->connection->expects($this->once()) ->method('post') - ->with('http://storeurl' . $this->basePath . '/whatever', (object)$new) + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$new) ->will($this->returnValue($new)); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::createResource('/whatever', $new); $this->assertSame($new, $result); @@ -179,10 +189,14 @@ public function testUpdateResourcePutsToTheRightPlace() $update = array(rand() => rand()); $this->connection->expects($this->once()) ->method('put') - ->with('http://storeurl' . $this->basePath . '/whatever', (object)$update) + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$update) ->will($this->returnValue($update)); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::updateResource('/whatever', $update); $this->assertSame($update, $result); @@ -192,10 +206,14 @@ public function testDeleteResourceDeletesToTheRightPlace() { $this->connection->expects($this->once()) ->method('delete') - ->with('http://storeurl' . $this->basePath . '/whatever') + ->with('https://api.bigcommerce.com/stores/hash/v2/whatever') ->will($this->returnValue("Successfully deleted")); - Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); + Client::configure(array( + 'store_hash' => 'hash', + 'client_id' => 'whatever', + 'auth_token' => 'whatever' + )); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::deleteResource('/whatever'); $this->assertSame("Successfully deleted", $result); @@ -249,18 +267,18 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable() public function collections() { return array( - // path function classname - array('products', 'getProducts', 'Product'), - array('brands', 'getBrands', 'Brand'), - array('orders', 'getOrders', 'Order'), - array('customers', 'getCustomers', 'Customer'), - array('coupons', 'getCoupons', 'Coupon'), + // path function classname + array('products', 'getProducts', 'Product'), + array('brands', 'getBrands', 'Brand'), + array('orders', 'getOrders', 'Order'), + array('customers', 'getCustomers', 'Customer'), + array('coupons', 'getCoupons', 'Coupon'), array('order_statuses', 'getOrderStatuses', 'OrderStatus'), - array('categories', 'getCategories', 'Category'), - array('options', 'getOptions', 'Option'), - array('optionsets', 'getOptionSets', 'OptionSet'), - array('products/skus', 'getSkus', 'Sku'), - array('requestlogs', 'getRequestLogs', 'RequestLog'), + array('categories', 'getCategories', 'Category'), + array('options', 'getOptions', 'Option'), + array('optionsets', 'getOptionSets', 'OptionSet'), + array('products/skus', 'getSkus', 'Sku'), + array('requestlogs', 'getRequestLogs', 'RequestLog'), ); } @@ -303,15 +321,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat public function resources() { return array( - // path function classname - array('products', '%sProduct', 'Product'), - array('brands', '%sBrand', 'Brand'), - array('orders', '%sOrder', 'Order'), - array('customers', '%sCustomer', 'Customer'), - array('categories', '%sCategory', 'Category'), - array('options', '%sOption', 'Option'), - array('optionsets', '%sOptionSet', 'OptionSet'), - array('coupons', '%sCoupon', 'Coupon'), + // path function classname + array('products', '%sProduct', 'Product'), + array('brands', '%sBrand', 'Brand'), + array('orders', '%sOrder', 'Order'), + array('customers', '%sCustomer', 'Customer'), + array('categories', '%sCategory', 'Category'), + array('options', '%sOption', 'Option'), + array('optionsets', '%sOptionSet', 'OptionSet'), + array('coupons', '%sCoupon', 'Coupon'), ); } From 4e8086ab43752e7580294db2874c8b3bba6db1e0 Mon Sep 17 00:00:00 2001 From: Brian Fenton Date: Tue, 9 Jun 2015 10:00:23 -0700 Subject: [PATCH 3/5] Revert "removing basic auth from the client in favor of oauth. including removing no longer used properties" This reverts commit 03fccfc4052eb9370cbf6a75e5d0f199826e70f9. --- src/Bigcommerce/Api/Client.php | 98 +++++++++++++++++++++----- src/Bigcommerce/Api/Connection.php | 17 +++-- test/Unit/Api/ClientTest.php | 108 ++++++++++++----------------- 3 files changed, 138 insertions(+), 85 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 1104262d..21ad263e 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -9,6 +9,27 @@ */ class Client { + /** + * Full Store URL to connect to + * + * @var string + */ + static private $store_url; + + /** + * Username to connect to the store API with + * + * @var string + */ + static private $username; + + /** + * API key + * + * @var string + */ + static private $api_key; + /** * Connection instance * @@ -24,37 +45,41 @@ class Client static private $resource; /** - * Full URL path to the configured store API. + * API path prefix to be added to store URL for requests * * @var string */ - static public $api_path; + static private $path_prefix = '/api/v2'; + /** - * Client ID from app installation + * Full URL path to the configured store API. * * @var string */ + static public $api_path; static private $client_id; static private $store_hash; static private $auth_token; - /** - * Appended to API URL to create API endpoints - * - * @var string - */ static private $stores_prefix = '/stores/%s/v2'; - /** - * Base URL for the Bigcommerce API - * - * @var string - */ static private $api_url = 'https://api.bigcommerce.com'; + static private $login_url = 'https://login.bigcommerce.com'; + /** - * Endpoint for getting tokens + * Configure the API client with the required settings to access + * the API for a store. * - * @var string + * Accepts both OAuth and Basic Auth credentials + * + * @param array $settings */ - static private $login_url = 'https://login.bigcommerce.com'; + public static function configure($settings) + { + if (isset($settings['client_id'])) { + self::configureOAuth($settings); + } else { + self::configureBasicAuth($settings); + } + } /** * Configure the API client with the required OAuth credentials. @@ -68,7 +93,7 @@ class Client * @param array $settings * @throws \Exception */ - public static function configure($settings) + public static function configureOAuth($settings) { if (!isset($settings['auth_token'])) { throw new Exception("'auth_token' must be provided"); @@ -85,6 +110,39 @@ public static function configure($settings) self::$connection = false; } + /** + * Configure the API client with the required credentials. + * + * Requires a settings array to be passed in with the following keys: + * + * - store_url + * - username + * - api_key + * + * @param array $settings + * @throws \Exception + */ + public static function configureBasicAuth(array $settings) + { + if (!isset($settings['store_url'])) { + throw new Exception("'store_url' must be provided"); + } + + if (!isset($settings['username'])) { + throw new Exception("'username' must be provided"); + } + + if (!isset($settings['api_key'])) { + throw new Exception("'api_key' must be provided"); + } + + self::$username = $settings['username']; + self::$api_key = $settings['api_key']; + self::$store_url = rtrim($settings['store_url'], '/'); + self::$api_path = self::$store_url . self::$path_prefix; + self::$connection = false; + } + /** * Configure the API client to throw exceptions when HTTP errors occur. * @@ -156,7 +214,11 @@ private static function connection() { if (!self::$connection) { self::$connection = new Connection(); - self::$connection->authenticate(self::$client_id, self::$auth_token); + if (self::$client_id) { + self::$connection->authenticateOauth(self::$client_id, self::$auth_token); + } else { + self::$connection->authenticateBasic(self::$username, self::$api_key); + } } return self::$connection; diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index b642287c..5bec4769 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -152,12 +152,23 @@ public function failOnError($option = true) } /** - * Sets OAuth authentication headers + * Sets the HTTP basic authentication. + * + * @param string $username + * @param string $password + */ + public function authenticateBasic($username, $password) + { + curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password"); + } + + /** + * Sets Oauth authentication headers * * @param string $clientId * @param string $authToken */ - public function authenticate($clientId, $authToken) + public function authenticateOauth($clientId, $authToken) { $this->addHeader('X-Auth-Client', $clientId); $this->addHeader('X-Auth-Token', $authToken); @@ -519,8 +530,6 @@ public function getBody() /** * Access given header from the response. * - * @param string $header - * * @return string|void */ public function getHeader($header) diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index 79ee8d26..960d3897 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -46,20 +46,26 @@ public function setUp() public function tearDown() { - Client::configure(array('client_id' => '', 'auth_token' => '', 'store_hash' => '')); + Client::configure(array('username' => '', 'api_key' => '', 'store_url' => '')); unset($this->connection); } - public function testConfigureRequiresAuthToken() + public function testConfigureRequiresStoreUrl() { - $this->setExpectedException('\\Exception', "'auth_token' must be provided"); - Client::configure(array('client_id' => 'whatever', 'store_hash' => 'whatever')); + $this->setExpectedException('\\Exception', "'store_url' must be provided"); + Client::configure(array('username' => 'whatever', 'api_key' => 'whatever')); } - public function testConfigureRequiresStoreHash() + public function testConfigureRequiresUsername() { - $this->setExpectedException('\\Exception', "'store_hash' must be provided"); - Client::configure(array('client_id' => 'whatever', 'auth_token' => 'whatever')); + $this->setExpectedException('\\Exception', "'username' must be provided"); + Client::configure(array('store_url' => 'whatever', 'api_key' => 'whatever')); + } + + public function testConfigureRequiresApiKey() + { + $this->setExpectedException('\\Exception', "'api_key' must be provided"); + Client::configure(array('username' => 'whatever', 'store_url' => 'whatever')); } public function testFailOnErrorPassesThroughToConnection() @@ -116,14 +122,10 @@ public function testGetResourceReturnsSpecifiedType() { $this->connection->expects($this->once()) ->method('get') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) + ->with('http://storeurl' . $this->basePath . '/whatever', false) ->will($this->returnValue(array(array()))); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $resource = Client::getResource('/whatever'); $this->assertInstanceOf('Bigcommerce\\Api\\Resource', $resource); @@ -133,14 +135,10 @@ public function testGetCountReturnsSpecifiedCount() { $this->connection->expects($this->once()) ->method('get') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) + ->with('http://storeurl' . $this->basePath . '/whatever', false) ->will($this->returnValue((object)array('count' => 5))); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $count = Client::getCount('/whatever'); $this->assertSame(5, $count); @@ -150,14 +148,10 @@ public function testGetCollectionReturnsCollectionOfSpecifiedTypes() { $this->connection->expects($this->once()) ->method('get') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', false) + ->with('http://storeurl' . $this->basePath . '/whatever', false) ->will($this->returnValue(array(array(), array()))); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $resources = Client::getCollection('/whatever'); $this->assertInternalType('array', $resources); @@ -171,14 +165,10 @@ public function testCreateResourcePostsToTheRightPlace() $new = array(rand() => rand()); $this->connection->expects($this->once()) ->method('post') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$new) + ->with('http://storeurl' . $this->basePath . '/whatever', (object)$new) ->will($this->returnValue($new)); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::createResource('/whatever', $new); $this->assertSame($new, $result); @@ -189,14 +179,10 @@ public function testUpdateResourcePutsToTheRightPlace() $update = array(rand() => rand()); $this->connection->expects($this->once()) ->method('put') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever', (object)$update) + ->with('http://storeurl' . $this->basePath . '/whatever', (object)$update) ->will($this->returnValue($update)); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::updateResource('/whatever', $update); $this->assertSame($update, $result); @@ -206,14 +192,10 @@ public function testDeleteResourceDeletesToTheRightPlace() { $this->connection->expects($this->once()) ->method('delete') - ->with('https://api.bigcommerce.com/stores/hash/v2/whatever') + ->with('http://storeurl' . $this->basePath . '/whatever') ->will($this->returnValue("Successfully deleted")); - Client::configure(array( - 'store_hash' => 'hash', - 'client_id' => 'whatever', - 'auth_token' => 'whatever' - )); + Client::configure(array('store_url' => 'http://storeurl', 'username' => 'whatever', 'api_key' => 'whatever')); Client::setConnection($this->connection); // re-set the connection since Client::configure unsets it $result = Client::deleteResource('/whatever'); $this->assertSame("Successfully deleted", $result); @@ -267,18 +249,18 @@ public function testGetRequestsRemainingRequestsTimeWhenNoValueAvailable() public function collections() { return array( - // path function classname - array('products', 'getProducts', 'Product'), - array('brands', 'getBrands', 'Brand'), - array('orders', 'getOrders', 'Order'), - array('customers', 'getCustomers', 'Customer'), - array('coupons', 'getCoupons', 'Coupon'), + // path function classname + array('products', 'getProducts', 'Product'), + array('brands', 'getBrands', 'Brand'), + array('orders', 'getOrders', 'Order'), + array('customers', 'getCustomers', 'Customer'), + array('coupons', 'getCoupons', 'Coupon'), array('order_statuses', 'getOrderStatuses', 'OrderStatus'), - array('categories', 'getCategories', 'Category'), - array('options', 'getOptions', 'Option'), - array('optionsets', 'getOptionSets', 'OptionSet'), - array('products/skus', 'getSkus', 'Sku'), - array('requestlogs', 'getRequestLogs', 'RequestLog'), + array('categories', 'getCategories', 'Category'), + array('options', 'getOptions', 'Option'), + array('optionsets', 'getOptionSets', 'OptionSet'), + array('products/skus', 'getSkus', 'Sku'), + array('requestlogs', 'getRequestLogs', 'RequestLog'), ); } @@ -321,15 +303,15 @@ public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($pat public function resources() { return array( - // path function classname - array('products', '%sProduct', 'Product'), - array('brands', '%sBrand', 'Brand'), - array('orders', '%sOrder', 'Order'), - array('customers', '%sCustomer', 'Customer'), - array('categories', '%sCategory', 'Category'), - array('options', '%sOption', 'Option'), - array('optionsets', '%sOptionSet', 'OptionSet'), - array('coupons', '%sCoupon', 'Coupon'), + // path function classname + array('products', '%sProduct', 'Product'), + array('brands', '%sBrand', 'Brand'), + array('orders', '%sOrder', 'Order'), + array('customers', '%sCustomer', 'Customer'), + array('categories', '%sCategory', 'Category'), + array('options', '%sOption', 'Option'), + array('optionsets', '%sOptionSet', 'OptionSet'), + array('coupons', '%sCoupon', 'Coupon'), ); } From e6902e1ba82a6ce3cdc23424824d787fd99d4d68 Mon Sep 17 00:00:00 2001 From: Brian Fenton Date: Tue, 9 Jun 2015 14:02:48 -0700 Subject: [PATCH 4/5] marking basic auth as deprecated --- src/Bigcommerce/Api/Client.php | 5 ++++- src/Bigcommerce/Api/Connection.php | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 21ad263e..c1f307fb 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -68,7 +68,7 @@ class Client * Configure the API client with the required settings to access * the API for a store. * - * Accepts both OAuth and Basic Auth credentials + * Accepts OAuth and (for now!) Basic Auth credentials * * @param array $settings */ @@ -119,6 +119,9 @@ public static function configureOAuth($settings) * - username * - api_key * + * @deprecated Please migrate to OAuth + * @see https://developer.bigcommerce.com/api/guides/oauth-transition + * * @param array $settings * @throws \Exception */ diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 5bec4769..6ebe1149 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -154,6 +154,7 @@ public function failOnError($option = true) /** * Sets the HTTP basic authentication. * + * @deprecated Use OAuth for authentication * @param string $username * @param string $password */ @@ -530,6 +531,8 @@ public function getBody() /** * Access given header from the response. * + * @param string $header Header name to retrieve + * * @return string|void */ public function getHeader($header) From 7a68f075209d9ce538ceffda220cb68a8ca14b05 Mon Sep 17 00:00:00 2001 From: Brian Fenton Date: Fri, 19 Jun 2015 14:35:12 -0700 Subject: [PATCH 5/5] not marking oauth as deprecated yet as per product --- src/Bigcommerce/Api/Client.php | 3 --- src/Bigcommerce/Api/Connection.php | 1 - 2 files changed, 4 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index c1f307fb..5242cc57 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -119,9 +119,6 @@ public static function configureOAuth($settings) * - username * - api_key * - * @deprecated Please migrate to OAuth - * @see https://developer.bigcommerce.com/api/guides/oauth-transition - * * @param array $settings * @throws \Exception */ diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 6ebe1149..12fb659e 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -154,7 +154,6 @@ public function failOnError($option = true) /** * Sets the HTTP basic authentication. * - * @deprecated Use OAuth for authentication * @param string $username * @param string $password */