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

Add response headers to ApiResponse to gather API limits information #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions Instagram/Core/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,16 +534,14 @@ public function deleteMediaComment( $media_id, $comment_id ) {
*/
private function apiCall( $method, $url, array $params = null, $throw_exception = true ){

$raw_response = $this->client->$method(
$response = $this->client->$method(
$url,
array(
'access_token' => $this->access_token,
'client_id' => isset( $params['client_id'] ) ? $params['client_id'] : $this->client_id
) + (array) $params
);

$response = new \Instagram\Net\ApiResponse( $raw_response );

if ( !$response->isValid() ) {
if ( $throw_exception ) {
if ( $response->getErrorType() == 'OAuthAccessTokenException' ) {
Expand Down
44 changes: 41 additions & 3 deletions Instagram/Net/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ class ApiResponse {
*/
protected $response;

/**
* Response headers
*
* @var array
* @access protected
*/
protected $responseHeaders;

/**
* Constructor
*
* @param $raw_response Response from teh API
* @param $response_headers Response headers
* @access public
*/
public function __construct( $raw_response ){
public function __construct( $raw_response, array $response_headers = null ){
$this->response = json_decode( $raw_response );
$this->responseHeaders = $response_headers;
if ( !$this->isValidApiResponse() ) {
$this->response = new \StdClass;
$this->response->meta = new \StdClass;
Expand Down Expand Up @@ -88,7 +98,15 @@ public function getRawData() {
return isset( $this->response ) ? $this->response : null;
}


/**
* Get the response headers
*
* @return array Return the response's headers
* @access public
*/
public function getHeaders() {
return $this->responseHeaders;
}

/**
* Get the response's error message
Expand Down Expand Up @@ -137,7 +155,27 @@ public function getErrorType() {
}
return null;
}


/**
* Gets the Rate limit returned from API
*
* @return mixed Returns the Rate limit returned from API
* @access public
*/
public function getRateLimit() {
return isset($this->responseHeaders['x-ratelimit-limit']) ? (int) $this->responseHeaders['x-ratelimit-limit'] : null;
}

/**
* Gets the Remaining Rate limit returned from API
*
* @return mixed Returns the Remaining Rate limit returned from API
* @access public
*/
public function getRateLimitRemaining() {
return isset($this->responseHeaders['x-ratelimit-remaining']) ? (int) $this->responseHeaders['x-ratelimit-remaining'] : null;
}

/**
* Magic to string method
*
Expand Down
33 changes: 31 additions & 2 deletions Instagram/Net/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class CurlClient implements ClientInterface {
*/
protected $curl = null;

/**
* Curl Response header
*
* @var array
*/
protected $curlRequestHeaders = array();

/**
* Constructor
*
Expand Down Expand Up @@ -97,14 +104,15 @@ protected function initializeCurl() {
$this->curl = curl_init();
curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $this->curl, CURLOPT_HEADERFUNCTION, array($this, 'fetchHeader') );
}

/**
* Fetch
*
* Execute the curl object
*
* @return StdClass
* @return ApiResponse
* @access protected
* @throws \Instagram\Core\ApiException
*/
Expand All @@ -114,7 +122,28 @@ protected function fetch() {
if ( $error ) {
throw new \Instagram\Core\ApiException( $error, 666, 'CurlError' );
}
return $raw_response;

$response = new ApiResponse($raw_response, $this->curlRequestHeaders);
return $response;
}

/**
* Method parses the headers returned by CURL and
* stores them into private array.
*
* @return ApiResponse
* @access protected
* @throws \Instagram\Core\ApiException
*/
protected function fetchHeader($ch, $header) {
$i = strpos($header, ':');
if (!empty($i)) {
$key = strtolower(substr($header, 0, $i));
$value = trim(substr($header, $i + 2));
$this->curlRequestHeaders[$key] = $value;
}

return strlen($header);
}

}