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

Added phpDoc for ApiRequestor, especially regarding thrown errors #472

Merged
merged 5 commits into from
Jun 13, 2018
Merged
Changes from 1 commit
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
124 changes: 124 additions & 0 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@
*/
class ApiRequestor
{
/**
* @var string|null
*/
private $_apiKey;

/**
* @var string
*/
private $_apiBase;

/**
* @var HttpClient\CurlClient
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
*/
private static $_httpClient;

/**
* ApiRequestor constructor.
*
* @param string|null $apiKey
* @param string|null $apiBase
*/
public function __construct($apiKey = null, $apiBase = null)
{
$this->_apiKey = $apiKey;
Expand All @@ -24,6 +39,13 @@ public function __construct($apiKey = null, $apiBase = null)
$this->_apiBase = $apiBase;
}

/**
* @static
*
* @param array|bool|mixed $d
*
* @return array|string|mixed
*/
private static function _encodeObjects($d)
{
if ($d instanceof ApiResource) {
Expand Down Expand Up @@ -51,6 +73,18 @@ private static function _encodeObjects($d)
*
* @return array An array whose first element is an API response and second
* element is the API key used to make the request.
* @throws Error\Api
* @throws Error\Authentication
* @throws Error\Card
* @throws Error\InvalidRequest
* @throws Error\OAuth\InvalidClient
* @throws Error\OAuth\InvalidGrant
* @throws Error\OAuth\InvalidRequest
* @throws Error\OAuth\InvalidScope
* @throws Error\OAuth\UnsupportedGrantType
* @throws Error\OAuth\UnsupportedResponseType
* @throws Error\Permission
* @throws Error\RateLimit
*/
public function request($method, $url, $params = null, $headers = null)
{
Expand All @@ -77,6 +111,15 @@ public function request($method, $url, $params = null, $headers = null)
* permissions.
* @throws Error\Card if the error is the error code is 402 (payment
* required)
* @throws Error\InvalidRequest if the error is caused by the user.
* @throws Error\OAuth\InvalidClient
* @throws Error\OAuth\InvalidGrant
* @throws Error\OAuth\InvalidRequest
* @throws Error\OAuth\InvalidScope
* @throws Error\OAuth\UnsupportedGrantType
* @throws Error\OAuth\UnsupportedResponseType
* @throws Error\Permission if the error is caused by insufficient
* permissions.
* @throws Error\RateLimit if the error is caused by too many requests
* hitting the API.
* @throws Error\Api otherwise.
Expand All @@ -102,6 +145,17 @@ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp)
throw $error;
}

/**
* @static
*
* @param string $rbody
* @param int $rcode
* @param array $rheaders
* @param array $resp
* @param array $errorData
*
* @return Error\Api|Error\Authentication|Error\Card|Error\InvalidRequest|Error\Permission|Error\RateLimit
*/
private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $errorData)
{
$msg = isset($errorData['message']) ? $errorData['message'] : null;
Expand Down Expand Up @@ -136,6 +190,17 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err
}
}

/**
* @static
*
* @param string|bool $rbody
* @param int $rcode
* @param array $rheaders
* @param array $resp
* @param array $errorCode
*
* @return null|Error\OAuth\InvalidClient|Error\OAuth\InvalidGrant|Error\OAuth\InvalidRequest|Error\OAuth\InvalidScope|Error\OAuth\UnsupportedGrantType|Error\OAuth\UnsupportedResponseType
*/
private static function _specificOAuthError($rbody, $rcode, $rheaders, $resp, $errorCode)
{
$description = isset($resp['error_description']) ? $resp['error_description'] : $errorCode;
Expand All @@ -158,6 +223,13 @@ private static function _specificOAuthError($rbody, $rcode, $rheaders, $resp, $e
return null;
}

/**
* @static
*
* @param $appInfo
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
*
* @return null|string
*/
private static function _formatAppInfo($appInfo)
{
if ($appInfo !== null) {
Expand All @@ -174,6 +246,14 @@ private static function _formatAppInfo($appInfo)
}
}

/**
* @static
*
* @param string $apiKey
* @param null $clientInfo
*
* @return array
*/
private static function _defaultHeaders($apiKey, $clientInfo = null)
{
$uaString = 'Stripe/v1 PhpBindings/' . Stripe::VERSION;
Expand Down Expand Up @@ -205,6 +285,16 @@ private static function _defaultHeaders($apiKey, $clientInfo = null)
return $defaultHeaders;
}

/**
* @param string $method
* @param string $url
* @param array $params
* @param array $headers
*
* @return array
* @throws Error\Api
* @throws Error\Authentication
*/
private function _requestRaw($method, $url, $params, $headers)
{
$myApiKey = $this->_apiKey;
Expand Down Expand Up @@ -273,6 +363,13 @@ private function _requestRaw($method, $url, $params, $headers)
return [$rbody, $rcode, $rheaders, $myApiKey];
}

/**
* @param resource $resource
* @param bool $hasCurlFile
*
* @return \CURLFile|string
* @throws Error\Api
*/
private function _processResourceParam($resource, $hasCurlFile)
{
if (get_resource_type($resource) !== 'stream') {
Expand All @@ -296,6 +393,25 @@ private function _processResourceParam($resource, $hasCurlFile)
}
}

/**
* @param bool|string $rbody
* @param int $rcode
* @param array $rheaders
*
* @return mixed
* @throws Error\Api
* @throws Error\Authentication
* @throws Error\Card
* @throws Error\InvalidRequest
* @throws Error\OAuth\InvalidClient
* @throws Error\OAuth\InvalidGrant
* @throws Error\OAuth\InvalidRequest
* @throws Error\OAuth\InvalidScope
* @throws Error\OAuth\UnsupportedGrantType
* @throws Error\OAuth\UnsupportedResponseType
* @throws Error\Permission
* @throws Error\RateLimit
*/
private function _interpretResponse($rbody, $rcode, $rheaders)
{
$resp = json_decode($rbody, true);
Expand All @@ -312,11 +428,19 @@ private function _interpretResponse($rbody, $rcode, $rheaders)
return $resp;
}

/**
* @static
*
* @param HttpClient\CurlClient $client
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function setHttpClient($client)
{
self::$_httpClient = $client;
}

/**
* @return HttpClient\CurlClient
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
*/
private function httpClient()
{
if (!self::$_httpClient) {
Expand Down