Commit f3b27cf0 authored by Brandon Lile's avatar Brandon Lile

Initial stream support commit

- Created new abstract base class for all future HTTP classes
- Added in new Mixer and Twitch namespace
parent 4c1274fb
Pipeline #819 passed with stages
in 3 minutes and 59 seconds
<?php
namespace Onyx\Mixer;
use Onyx\Mixer\Constants as MixerConstants;
use Onyx\Mixer\Helpers\Network\Http;
use Onyx\Mixer\Helpers\Network\MixerApiNetworkException;
/**
* Class Client
* @package Onyx\Mixer
*/
class Client extends Http
{
public function getStreamerInfo($streamer)
{
$url = sprintf(MixerConstants::$getStreamerInfo, $streamer);
$data = $this->getJson($url);
throw new MixerApiNetworkException('Could not load streamer information. Check that you entered the streamer correctly.');
}
}
\ No newline at end of file
<?php
namespace Onyx\Mixer;
class Constants
{
/**
* URL to obtain streamer's info
*
* @var string
*/
public static $getStreamerInfo = 'https://mixer.com/api/v1/channels/%1$s';
}
\ No newline at end of file
<?php
namespace Onyx\Mixer\Helpers\Network;
use GuzzleHttp\Client as Guzzle;
use Onyx\Objects\Network\BaseHttp;
use Onyx\Objects\Network\HttpBadResponseException;
class Http extends BaseHttp
{
public function getJson($url)
{
if (!$this->guzzle instanceof Guzzle) {
$this->setupGuzzle();
}
$response = $this->guzzle->get($url, [
'headers' => [
'Accept' => 'application/json',
],
]);
if ($response->getStatusCode() != 200) {
throw new MixerApiNetworkException();
}
return json_decode($response->getBody(), true);
}
}
class MixerApiNetworkException extends HttpBadResponseException
{
}
\ No newline at end of file
<?php
namespace Onyx\Objects\Network;
use GuzzleHttp\Client as Guzzle;
abstract class BaseHttp
{
/**
* @var \GuzzleHttp\Client
*/
protected $guzzle;
public function __construct()
{
$this->setupGuzzle();
}
public function setupGuzzle()
{
$this->guzzle = new Guzzle();
}
abstract public function getJson($url);
}
class HttpBadResponseException extends \Exception
{
}
\ No newline at end of file
<?php
namespace Onyx\Twitch;
use Onyx\Twitch\Constants as TwitchConstants;
use Onyx\Twitch\Helpers\Network\Http;
use Onyx\Twitch\Helpers\Network\TwitchApiNetworkException;
/**
* Class Client
* @package Onyx\Twitch
*/
class Client extends Http
{
public function getStreamerInfo($streamer)
{
$url = sprintf(TwitchConstants::$getStreamerInfo, $streamer);
$data = $this->getJson($url);
// Check if "stream" is not null
throw new TwitchApiNetworkException('Could not load streamer information. Check that you entered the streamer correctly.');
}
}
\ No newline at end of file
<?php
namespace Onyx\Twitch;
class Constants
{
/**
* URL to obtain streamer's info
*
* @var string
*/
public static $getStreamerInfo = '';
}
\ No newline at end of file
<?php
namespace Onyx\Twitch\Helpers\Network;
use GuzzleHttp\Client as Guzzle;
use Onyx\Objects\Network\BaseHttp;
use Onyx\Objects\Network\HttpBadResponseException;
class Http extends BaseHttp
{
public function getJson($url)
{
if (!$this->guzzle instanceof Guzzle) {
$this->setupGuzzle();
}
$response = $this->guzzle->get($url, [
'headers' => [
'Accept' => 'application/json',
'Client-ID' => ''
],
]);
if ($response->getStatusCode() != 200) {
throw new TwitchApiNetworkException();
}
return json_decode($response->getBody(), true);
}
}
class TwitchApiNetworkException extends HttpBadResponseException
{
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment