Skip to content

Commit 661a78d

Browse files
committed
Return rate-limit info as object when returnformat=object
1 parent 1b75a97 commit 661a78d

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ codebird-php - changelog
1515
+ #120 Support Ads API
1616
+ Support WebP media format
1717
+ Fixes for non-multipart media uploads, see #141
18+
+ Return rate-limiting info as object, when return format = object
1819

1920
2.7.2 (2015-09-23)
2021
- #135 Invalid HTTP request headers in non-cURL mode

src/codebird.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,18 +1312,22 @@ protected function _parseBearerReply($result, $httpstatus)
13121312
*
13131313
* @param array $headers The CURL response headers
13141314
*
1315-
* @return null|array The rate-limiting information
1315+
* @return null|array|object The rate-limiting information
13161316
*/
13171317
protected function _getRateLimitInfo($headers)
13181318
{
13191319
if (! isset($headers['x-rate-limit-limit'])) {
13201320
return null;
13211321
}
1322-
return [
1322+
$rate = [
13231323
'limit' => $headers['x-rate-limit-limit'],
13241324
'remaining' => $headers['x-rate-limit-remaining'],
13251325
'reset' => $headers['x-rate-limit-reset']
13261326
];
1327+
if ($this->_return_format === CODEBIRD_RETURNFORMAT_OBJECT) {
1328+
return (object) $rate;
1329+
}
1330+
return $rate;
13271331
}
13281332

13291333
/**
@@ -1698,7 +1702,7 @@ protected function _getMultipartRequestFromParams($method_template, $border, $pa
16981702
*
16991703
* @param string $method_template The method template to call
17001704
* @param string $key The parameter name
1701-
* @param array $value The possible file name or URL
1705+
* @param string $value The possible file name or URL
17021706
*
17031707
* @return mixed
17041708
*/
@@ -2034,16 +2038,7 @@ protected function _callApiCurl(
20342038
$reply = $this->_parseApiReply($reply);
20352039
$rate = $this->_getRateLimitInfo($headers);
20362040

2037-
switch ($this->_return_format) {
2038-
case CODEBIRD_RETURNFORMAT_ARRAY:
2039-
$reply['httpstatus'] = $httpstatus;
2040-
$reply['rate'] = $rate;
2041-
break;
2042-
case CODEBIRD_RETURNFORMAT_OBJECT:
2043-
$reply->httpstatus = $httpstatus;
2044-
$reply->rate = $rate;
2045-
break;
2046-
}
2041+
$reply = $this->_appendHttpStatusAndRate($reply, $httpstatus, $rate);
20472042
return $reply;
20482043
}
20492044

@@ -2111,16 +2106,8 @@ protected function _callApiNoCurl(
21112106
$reply = $this->_parseApiReplyPrefillHeaders($headers, $reply);
21122107
$reply = $this->_parseApiReply($reply);
21132108
$rate = $this->_getRateLimitInfo($headers);
2114-
switch ($this->_return_format) {
2115-
case CODEBIRD_RETURNFORMAT_ARRAY:
2116-
$reply['httpstatus'] = $httpstatus;
2117-
$reply['rate'] = $rate;
2118-
break;
2119-
case CODEBIRD_RETURNFORMAT_OBJECT:
2120-
$reply->httpstatus = $httpstatus;
2121-
$reply->rate = $rate;
2122-
break;
2123-
}
2109+
2110+
$reply = $this->_appendHttpStatusAndRate($reply, $httpstatus, $rate);
21242111
return $reply;
21252112
}
21262113

@@ -2213,6 +2200,34 @@ protected function _callApiPreparationsPost(
22132200
return [$authorization, $params, $request_headers];
22142201
}
22152202

2203+
/**
2204+
* Appends HTTP status and rate limiting info to the reply
2205+
*
2206+
* @param array|object $reply The reply to append to
2207+
* @param string $httpstatus The HTTP status code to append
2208+
* @param mixed $rate The rate limiting info to append
2209+
*/
2210+
protected function _appendHttpStatusAndRate($reply, $httpstatus, $rate)
2211+
{
2212+
switch ($this->_return_format) {
2213+
case CODEBIRD_RETURNFORMAT_ARRAY:
2214+
$reply['httpstatus'] = $httpstatus;
2215+
$reply['rate'] = $rate;
2216+
break;
2217+
case CODEBIRD_RETURNFORMAT_OBJECT:
2218+
$reply->httpstatus = $httpstatus;
2219+
$reply->rate = $rate;
2220+
break;
2221+
case CODEBIRD_RETURNFORMAT_JSON:
2222+
$reply = json_decode($reply);
2223+
$reply->httpstatus = $httpstatus;
2224+
$reply->rate = $rate;
2225+
$reply = json_encode($reply);
2226+
break;
2227+
}
2228+
return $reply;
2229+
}
2230+
22162231
/**
22172232
* Get Bearer authorization string
22182233
*

0 commit comments

Comments
 (0)