Skip to content

Commit

Permalink
Merge branch 'pull-182' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Feb 6, 2017
2 parents 0562683 + 2dfc945 commit 4baf8da
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions src/codebird.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ public function oauth_authenticate($force_login = NULL, $screen_name = NULL, $ty
throw new \Exception('To get the ' . $type . ' URL, use the correct third parameter, or omit it.');
}
if ($this->_oauth_token === null) {
throw new \Exception('To get the ' . $type . ' URL, the OAuth token must be set.');
throw new CodebirdCredentialsException('To get the ' . $type . ' URL, the OAuth token must be set.');
}
$url = self::$_endpoints['oauth'] . 'oauth/' . $type . '?oauth_token=' . $this->_url($this->_oauth_token);
if ($force_login) {
Expand Down Expand Up @@ -1240,7 +1240,7 @@ private function _getProxyData($name)
protected function _oauth2TokenCurl()
{
if (self::$_consumer_key === null) {
throw new \Exception('To obtain a bearer token, the consumer key must be set.');
throw new CodebirdCredentialsException('To obtain a bearer token, the consumer key must be set.');
}
$post_fields = [
'grant_type' => 'client_credentials'
Expand All @@ -1258,7 +1258,7 @@ protected function _oauth2TokenCurl()

// catch request errors
if ($result === false) {
throw new \Exception('Request error for bearer token: ' . $this->_curl_error($connection));
throw new CodebirdAuthException('Request error for bearer token: ' . $this->_curl_error($connection));
}

// certificate validation results
Expand All @@ -1280,14 +1280,14 @@ protected function _oauth2TokenCurl()
protected function _oauth2TokenNoCurl()
{
if (self::$_consumer_key == null) {
throw new \Exception('To obtain a bearer token, the consumer key must be set.');
throw new CodebirdCredentialsException('To obtain a bearer token, the consumer key must be set.');
}

$url = self::$_endpoints['oauth'] . 'oauth2/token';
$hostname = parse_url($url, PHP_URL_HOST);

if ($hostname === false) {
throw new \Exception('Incorrect API endpoint host.');
throw new CodebirdEndpointException('Incorrect API endpoint host.');
}

$contextOptions = [
Expand Down Expand Up @@ -1479,7 +1479,7 @@ protected function _url($data)
protected function _sha1($data)
{
if (self::$_consumer_secret === null) {
throw new \Exception('To generate a hash, the consumer secret must be set.');
throw new CodebirdCredentialsException('To generate a hash, the consumer secret must be set.');
}
if (!function_exists('hash_hmac')) {
throw new \Exception('To generate a hash, the PHP hash extension must be available.');
Expand Down Expand Up @@ -1554,7 +1554,7 @@ protected function _getSignature($httpmethod, $method, $base_params)
protected function _sign($httpmethod, $method, $params = [])
{
if (self::$_consumer_key === null) {
throw new \Exception('To generate a signature, the consumer key must be set.');
throw new CodebirdCredentialsException('To generate a signature, the consumer key must be set.');
}
$sign_base_params = array_map(
[$this, '_url'],
Expand Down Expand Up @@ -1757,7 +1757,7 @@ protected function _getMultipartRequestFromParams($method_template, $border, $pa
foreach ($params as $key => $value) {
// is it an array?
if (is_array($value)) {
throw new \Exception('Using URL-encoded parameters is not supported for uploading media.');
throw new CodebirdMediaException('Using URL-encoded parameters is not supported for uploading media.');
}
$request .=
'--' . $border . "\r\n"
Expand Down Expand Up @@ -1888,7 +1888,7 @@ protected function _fetchRemoteFile($url)
) {
return $result;
}
throw new \Exception('Downloading a remote media file failed.');
throw new CodebirdMediaException('Downloading a remote media file failed.');
return false;
}
// no cURL
Expand All @@ -1908,7 +1908,7 @@ protected function _fetchRemoteFile($url)
) {
return $result;
}
throw new \Exception('Downloading a remote media file failed.');
throw new CodebirdMediaException('Downloading a remote media file failed.');
return false;
}

Expand Down Expand Up @@ -2039,7 +2039,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = []
&& $this->_oauth_token === null
&& substr($method, 0, 5) !== 'oauth'
) {
throw new \Exception('To call this API, the OAuth access token must be set.');
throw new CodebirdCredentialsException('To call this API, the OAuth access token must be set.');
}
// use separate API access for streaming API
if ($this->_detectStreaming($method) !== false) {
Expand Down Expand Up @@ -2138,7 +2138,7 @@ protected function _callApiNoCurl(

$hostname = parse_url($url, PHP_URL_HOST);
if ($hostname === false) {
throw new \Exception('Incorrect API endpoint host.');
throw new CodebirdEndpointException('Incorrect API endpoint host.');
}

$request_headers[] = 'Authorization: ' . $authorization;
Expand Down Expand Up @@ -2310,7 +2310,7 @@ protected function _getBearerAuthorization()
if (self::$_consumer_key === null
&& self::$_bearer_token === null
) {
throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.');
throw new CodebirdCredentialsException('To make an app-only auth API request, consumer key or bearer token must be set.');
}
// automatically fetch bearer token, if necessary
if (self::$_bearer_token === null) {
Expand Down Expand Up @@ -2387,7 +2387,7 @@ protected function _callApiStreaming(
$path = parse_url($url, PHP_URL_PATH);
$query = parse_url($url, PHP_URL_QUERY);
if ($hostname === false) {
throw new \Exception('Incorrect API endpoint host.');
throw new CodebirdEndpointException('Incorrect API endpoint host.');
}

$request_headers[] = 'Authorization: ' . $authorization;
Expand Down Expand Up @@ -2649,4 +2649,36 @@ protected function _parseApiReply($reply)
}
return $parsed;
}

}

/**
* Catch errors when authtoken is expired
*/
class CodebirdAuthException extends \Exception {

}


/**
* Catch error when credentials are not set correclty
*/
class CodebirdCredentialsException extends \Exception {

}

/**
* Catch errors r elated to bad endpoi ts
*/
class CodebirdEndpointException extends \Exception {

}

/*
* Catch errors relatedto media
*/

class CodebirdMediaException extends \Exception {

}

0 comments on commit 4baf8da

Please sign in to comment.