Created
July 5, 2019 15:52
-
-
Save robertuniqid/736b76b59d51f45e3c9ebc390f1a856b to your computer and use it in GitHub Desktop.
SendOwl - Simple API Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WPEPAddOnSalesEngine_SendOwlAPI { | |
/** | |
* @var string | |
*/ | |
private $_api_end_point_products = 'https://www.sendowl.com/api/v1_2/products'; | |
private $_api_end_point_product_subscriptions = 'https://www.sendowl.com/api/v1_3/subscriptions'; | |
/** | |
* @var string | |
*/ | |
private $_request_information; | |
/** | |
* WPEPAddOnSalesEngine_SendOwlAPI constructor. | |
* @param string $key | |
* @param string $secret | |
*/ | |
public function __construct( string $key, string $secret ) { | |
$this->_request_information = [ | |
'auth' => [ $key, $secret ] | |
]; | |
} | |
/** | |
* @param int $pagination_limit | |
* @param int $pagination_page | |
* @return array|mixed|object | |
*/ | |
public function get_products( $pagination_limit = 10, $pagination_page = 1 ) { | |
return $this->_request_get_entries( $this->_api_end_point_products, $pagination_limit, $pagination_page, 'product' ); | |
} | |
/** | |
* @param int $pagination_limit | |
* @param int $pagination_page | |
* @return array|mixed|object | |
*/ | |
public function get_subscriptions( $pagination_limit = 10, $pagination_page = 1 ) { | |
return $this->_request_get_entries( $this->_api_end_point_product_subscriptions, $pagination_limit, $pagination_page, 'subscription' ); | |
} | |
/** | |
* @param $endpoint | |
* @param int $pagination_limit | |
* @param int $pagination_page | |
* @param bool|string $response_item_inner_key | |
* @return array | |
*/ | |
private function _request_get_entries( $endpoint, $pagination_limit = 10, $pagination_page = 1, $response_item_inner_key = false ) { | |
if( $pagination_limit > 50 ) { | |
$so_pagination_limit_current_page = $pagination_page; | |
$response = []; | |
for( $so_pagination_limit_start = 0; $so_pagination_limit_start <= $pagination_limit; $so_pagination_limit_start++ ) { | |
$so_pagination_limit_response = $this->_request_get_entries( $endpoint, 50, $so_pagination_limit_current_page, $response_item_inner_key ); | |
if( empty( $so_pagination_limit_response ) ) | |
return $response; | |
$response = array_merge( $response, $so_pagination_limit_response ); | |
if( count( $so_pagination_limit_response ) < 50 ) | |
return $response; | |
$so_pagination_limit_current_page++; | |
} | |
return $response; | |
} | |
$response = Requests::get( $endpoint .'/?' . http_build_query( [ | |
'per_page' => $pagination_limit, | |
'page' => $pagination_page | |
] ), [ 'Accept' => 'application/json' ], $this->_request_information ); | |
if ( $response->success ) { | |
$end_point_response = json_decode( $response->body, true ); | |
if( !is_array( $end_point_response ) ) | |
return []; | |
if( $response_item_inner_key === false ) | |
return $end_point_response; | |
$response = []; | |
foreach( $end_point_response as $end_point_response_item ) | |
$response[] = $end_point_response_item[ $response_item_inner_key ]; | |
return $response; | |
} | |
return []; | |
} | |
/** | |
* @param int $product_id | |
* @return array|bool | |
*/ | |
public function get_product( $product_id ) { | |
$response = Requests::get( $this->_api_end_point_products .'/' . intval( $product_id ), [ 'Accept' => 'application/json' ], $this->_request_information ); | |
if ( $response->success ) { | |
$response = json_decode( $response->body, true ); | |
return ( is_array( $response ) ? $response : [] ); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment