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

Begin Implementing V3 while remaining Backwards Compatible. #267

Open
wants to merge 6 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
93 changes: 71 additions & 22 deletions src/Bigcommerce/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Bigcommerce API Client.
*
* Note: Unless specified, the functions only support the API version 2
*/
class Client
{
Expand Down Expand Up @@ -50,7 +52,7 @@ class Client
*
* @var string
*/
private static $path_prefix = '/api/v2';
private static $path_prefix = '/api/';

/**
* Full URL path to the configured store API.
Expand All @@ -62,7 +64,7 @@ class Client
private static $store_hash;
private static $auth_token;
private static $client_secret;
private static $stores_prefix = '/stores/%s/v2';
private static $stores_prefix = '/stores/%s/';
private static $api_url = 'https://api.bigcommerce.com';
private static $login_url = 'https://login.bigcommerce.com';

Expand Down Expand Up @@ -256,11 +258,11 @@ public static function setConnection(Connection $connection = null)
* @param string $resource resource class to map individual items
* @return mixed array|string mapped collection or XML string if useXml is true
*/
public static function getCollection($path, $resource = 'Resource')
public static function getCollection($path, $resource = 'Resource', $version = 'v2')
{
$response = self::connection()->get(self::$api_path . $path);
$response = self::connection()->get(self::$api_path . $version . $path);

return self::mapCollection($resource, $response);
return self::mapCollection($resource, $response, $version);
}

/**
Expand All @@ -270,9 +272,9 @@ public static function getCollection($path, $resource = 'Resource')
* @param string $resource resource class to map individual items
* @return mixed Resource|string resource object or XML string if useXml is true
*/
public static function getResource($path, $resource = 'Resource')
public static function getResource($path, $resource = 'Resource', $version = 'v2')
{
$response = self::connection()->get(self::$api_path . $path);
$response = self::connection()->get(self::$api_path . $version . $path);

return self::mapResource($resource, $response);
}
Expand All @@ -283,9 +285,9 @@ public static function getResource($path, $resource = 'Resource')
* @param string $path api endpoint
* @return mixed int|string count value or XML string if useXml is true
*/
public static function getCount($path)
public static function getCount($path, $version = 'v2')
{
$response = self::connection()->get(self::$api_path . $path);
$response = self::connection()->get(self::$api_path . $version . $path);

if ($response == false || is_string($response)) {
return $response;
Expand All @@ -301,13 +303,13 @@ public static function getCount($path)
* @param mixed $object object or XML string to create
* @return mixed
*/
public static function createResource($path, $object)
public static function createResource($path, $object, $version = 'v2')
{
if (is_array($object)) {
$object = (object)$object;
}

return self::connection()->post(self::$api_path . $path, $object);
return self::connection()->post(self::$api_path . $version . $path, $object);
}

/**
Expand All @@ -317,13 +319,13 @@ public static function createResource($path, $object)
* @param mixed $object object or XML string to update
* @return mixed
*/
public static function updateResource($path, $object)
public static function updateResource($path, $object, $version = 'v2')
{
if (is_array($object)) {
$object = (object)$object;
}

return self::connection()->put(self::$api_path . $path, $object);
return self::connection()->put(self::$api_path . $version . $path, $object);
}

/**
Expand All @@ -332,9 +334,9 @@ public static function updateResource($path, $object)
* @param string $path api endpoint
* @return mixed
*/
public static function deleteResource($path)
public static function deleteResource($path, $version = 'v2')
{
return self::connection()->delete(self::$api_path . $path);
return self::connection()->delete(self::$api_path . $version . $path);
}

/**
Expand All @@ -344,8 +346,12 @@ public static function deleteResource($path)
* @param array $object object collection
* @return array
*/
private static function mapCollection($resource, $object)
private static function mapCollection($resource, $object, $version = 'v2')
{
if ($version === 'v3') {
$object = $object->data ?? false;
}

if ($object == false || is_string($object)) {
return $object;
}
Expand Down Expand Up @@ -453,9 +459,9 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp
*
* @return \DateTime
*/
public static function getTime()
public static function getTime($version = 'v2')
{
$response = self::connection()->get(self::$api_path . '/time');
$response = self::connection()->get(self::$api_path . $version . '/time');

if ($response == false || is_string($response)) {
return $response;
Expand All @@ -466,14 +472,27 @@ public static function getTime()

/**
* Returns the default collection of products.
*
* Supports v2 & v3
*
* @param array $filter
* @return mixed array|string list of products or XML string if useXml is true
*/
public static function getProducts($filter = array())
public static function getProducts($filter = array(), $version = 'v2')
{
$filter = Filter::create($filter);
return self::getCollection('/products' . $filter->toQuery(), 'Product');

switch ($version) {
case 'v3':
return self::getCollection('/catalog/products' . $filter->toQuery(), 'Product', $version);
break;

case 'v2':
default:
return self::getCollection('/products' . $filter->toQuery(), 'Product');
break;
}

}

/**
Expand Down Expand Up @@ -1321,9 +1340,9 @@ public static function getRequestLogs()
return self::getCollection('/requestlogs', 'RequestLog');
}

public static function getStore()
public static function getStore($version = 'v2')
{
$response = self::connection()->get(self::$api_path . '/store');
$response = self::connection()->get(self::$api_path . $version . '/store');
return $response;
}

Expand Down Expand Up @@ -1462,6 +1481,36 @@ public static function getOrderShippingAddresses($orderID, $filter = array())
return self::getCollection('/orders/' . $orderID . '/shipping_addresses' . $filter->toQuery(), 'Address');
}

/**
* Get Cart
*
* Only supports v3
*
* @param $cartID
* @return mixed
*/
public static function getCart($cartID) {
return self::getResource('/carts/' . $cartID, 'Resource', 'v3');
}

/**
* Apply Coupon to Checkout
*
* Only supports v3
*
* @param $checkoutID
* @param mixed $object the coupon code to apply
* @return mixed
*/
public static function addCoupon($checkoutID, $object)
{
if (is_array($object)) {
$object = (object)$object;
}

return self::createResource('/checkouts/' . $checkoutID . '/coupons', $object, 'v3');
}

/**
* Create a new currency.
*
Expand Down