Skip to content

Commit

Permalink
Merge pull request #8 from hootener/v5-updates
Browse files Browse the repository at this point in the history
Error Handling and v5 Updates
  • Loading branch information
PetterKraabol committed Jun 29, 2017
2 parents d8b7b24 + f159f61 commit 530f937
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
25 changes: 16 additions & 9 deletions config/twitch-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
'client_secret' => env('TWITCH_SECRET', ''),
'redirect_url' => env('TWITCH_REDIRECT_URI', ''),
'scopes' => [
'user_read',
'user_blocks_edit',
'user_blocks_read',
'user_follows_edit',
'channel_read',
'channel_editor',
'channel_check_subscription',
'channel_commercial',
'channel_editor',
'channel_feed_edit',
'channel_feed_read',
'channel_read',
'channel_stream',
'channel_subscriptions',
'user_subscriptions',
'channel_check_subscription',
'chat_login',
'collections_edit',
'communities_edit',
'communities_moderate',
'openid',
'user_blocks_edit',
'user_blocks_read',
'user_follows_edit',
'user_read',
'user_subscriptions',
'viewing_activity_read'
],
];
];
39 changes: 29 additions & 10 deletions src/API/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

use Zarlach\TwitchApi\Exceptions\RequestRequiresAuthenticationException;
use Zarlach\TwitchApi\Exceptions\RequestRequiresClientIdException;

Expand All @@ -21,6 +23,7 @@ class Api
*
* @var clientId
*/

protected $clientId;

/**
Expand Down Expand Up @@ -49,7 +52,7 @@ public function __construct($token = null, $clientId = null)
$this->setClientId($clientId);
} elseif (config('twitch-api.client_id')) {
$this->setClientId(config('twitch-api.client_id'));
}
}

// GuzzleHttp Client with default parameters.
$this->client = new Client([
Expand Down Expand Up @@ -146,15 +149,15 @@ public function sendRequest($type = 'GET', $path = '', $token = false, $options
* This variable can either be removed completed or used for something
* more useful, like telling developers what options they have.
*/

// URL parameters
//$path = $this->generateUrl($path, $token, $options, $availableOptions);
$path = $this->generateUrl($path, $token, $options, $availableOptions);

// Headers
$data = [
'headers' => [
'Client-ID' => $this->getClientId(),
'Accept' => 'application/vnd.twitchtv.v3+json',
'Accept' => 'application/vnd.twitchtv.v5+json',
],
];

Expand All @@ -163,14 +166,30 @@ public function sendRequest($type = 'GET', $path = '', $token = false, $options
$data['headers']['Authorization'] = 'OAuth '.$this->getToken($token);
}

// Request object
$request = new Request($type, $path, $data);
try{
// Request object
$request = new Request($type, $path, $data);
// Send request
$response = $this->client->send($request);

// Send request
$response = $this->client->send($request);

// Return body in JSON data
return json_decode($response->getBody(), true);
// Return body in JSON data
return json_decode($response->getBody(), true);
}
catch(RequestException $e){
if ($e->hasResponse()) {
$exception = (string) $e->getResponse()->getBody();
$exception = json_decode($exception);
return $exception;
} else {
//503
return array(
'error' => 'Service Unavailable',
'status' => 503,
'message' => $e->getMessage()
);
}
}

}

/**
Expand Down

0 comments on commit 530f937

Please sign in to comment.