Skip to content

Update Resource.php #132

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
126 changes: 101 additions & 25 deletions src/Bigcommerce/Api/Client.php
Original file line number Diff line number Diff line change
@@ -29,7 +29,28 @@ class Client
* @var string
*/
static private $api_key;


/**
* Store Hash to connect to (oAuth)
*
* @var string
*/
static private $store_hash;

/**
* oAuth Client ID to connect to the store API with
*
* @var string
*/
static private $client_id;

/**
* oAuth Access Token
*
* @var string
*/
static private $oauth_token;

/**
* Connection instance
*
@@ -50,6 +71,27 @@ class Client
* @var string
*/
static private $path_prefix = '/api/v2';

/**
* oAuth API path prefix to be added to store URL for requests
*
* @var string
*/
static private $oauth_path_prefix = 'https://api.bigcommerce.com/stores/';

/**
* oAuth API Version to use (current at time is v2)
*
* @var string
*/
static private $api_version = '/v2';

/**
* Connect via oAuth. Default is basic authentication.
*
* @param bool
*/
static private $use_oauth = false;

/**
* Full URL path to the configured store API.
@@ -60,36 +102,64 @@ class Client

/**
* Configure the API client with the required credentials.
* Default is Basic Authentication.
*
* Requires a settings array to be passed in with the following keys:
*
* Basic Authentication:
* - store_url
* - username
* - api_key
*
* oAuth Authentication:
* - store_hash
* - client_id
* - oauth_token
*
* @param array $settings
* @param bool $oauth
* @throws \Exception
*/
public static function configure(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;
}
public static function configure(array $settings, $use_oauth = false)
{
if ($use_oauth) {
if (!isset($settings['store_hash'])) {
throw new Exception("'store_hash' must be provided for oAuth Authentication");
}

if (!isset($settings['client_id'])) {
throw new Exception("'client_id' must be provided for oAuth Authentication");
}

if (!isset($settings['oauth_token'])) {
throw new Exception("'oauth_token' must be provided for oAuth Authentication");
}

self::$store_hash = $settings['store_hash'];
self::$client_id = $settings['client_id'];
self::$oauth_token = $settings['oauth_token'];
self::$api_path = self::$oauth_path_prefix .self::$store_hash .self::$api_version;
self::$connection = false;
self::$use_oauth = true;
} else {
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.
@@ -157,9 +227,15 @@ public static function getLastError()
private static function connection()
{
if (!self::$connection) {
self::$connection = new Connection();
self::$connection->authenticate(self::$username, self::$api_key);
}
if (self::$use_oauth) {
self::$connection = new Connection();
self::$connection->authenticateOauth(self::$client_id, self::$oauth_token);

} else {
self::$connection = new Connection();
self::$connection->authenticateBasic(self::$username, self::$api_key);
}
}

return self::$connection;
}
38 changes: 33 additions & 5 deletions src/Bigcommerce/Api/Connection.php
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ class Connection
private $failOnError = false;

/**
* Manually follow location redirects. Used if CURLOPT_FOLLOWLOCATION
* Manually follow location redirects. Used if FOLLOWLOCATION
* is unavailable due to open_basedir restriction.
* @var boolean
*/
@@ -69,6 +69,20 @@ class Connection
* as XML. Defaults to false (using JSON).
*/
private $useXml = false;

/**
* oAuth Client ID
*
* @var string
*/
private $client_id;

/**
* oAuth Access Token
*
* @var string
*/
private $oauth_token;

/**
* Initializes the connection object.
@@ -118,11 +132,20 @@ public function failOnError($option = true)
/**
* Sets the HTTP basic authentication.
*/
public function authenticate($username, $password)
public function authenticateBasic($username, $password)
{
curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password");
}


/**
* Sets the HTTP oAuth autentication.
*/
public function authenticateOauth($client_id, $oauth_token)
{
$this->client_id = $client_id;
$this->oauth_token = $oauth_token;
}

/**
* Set a default timeout for the request. The client will error if the
* request takes longer than this to respond.
@@ -183,6 +206,11 @@ private function initializeRequest()
$this->responseHeaders = array();
$this->lastError = false;
$this->addHeader('Accept', $this->getContentType());

if (isset($this->client_id) && isset($this->oauth_token)) {
$this->addHeader('X-Auth-Client', $this->client_id);
$this->addHeader('X-Auth-Token', $this->oauth_token);
}

curl_setopt($this->curl, CURLOPT_POST, false);
curl_setopt($this->curl, CURLOPT_PUT, false);
@@ -299,7 +327,7 @@ public function get($url, $query = false)
*/
public function post($url, $body)
{
$this->addHeader('Content-Type', $this->getContentType());
//$this->addHeader('Content-Type', $this->getContentType());

if (!is_string($body)) {
$body = json_encode($body);
@@ -346,7 +374,7 @@ public function head($url)
*/
public function put($url, $body)
{
$this->addHeader('Content-Type', $this->getContentType());
//$this->addHeader('Content-Type', $this->getContentType());

if (!is_string($body)) {
$body = json_encode($body);
2 changes: 1 addition & 1 deletion src/Bigcommerce/Api/Resource.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class Resource
*
* @var stdclass
*/
protected $fields;
public $fields;

/**
* @var int