Skip to content

Commit

Permalink
Added this file because I added functionality to SoccerAPI that wasn'…
Browse files Browse the repository at this point in the history
…t in the orignal package

Added this file because I added functionality to SoccerAPI that wasn't in the orignal package
  • Loading branch information
sebastiaanspeck committed Oct 15, 2018
1 parent 6b31f6f commit b43d38b
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions soccerapi/SoccerAPIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Sportmonks\SoccerAPI;

use GuzzleHttp\Client;
use Sportmonks\SoccerAPI\Exceptions\ApiRequestException;

class SoccerAPIClient {

/* @var $client Client */
protected $client;

protected $apiToken;
protected $withoutData;
protected $include = [];
protected $leagues = [];
protected $perPage = 100;
protected $page = 1;
protected $timezone;

public function __construct()
{
$options = [
'base_uri' => 'https://soccer.sportmonks.com/api/v2.0/',
'verify' => app('env') === 'testing' ? false : true,
];
$this->client = new Client($options);

$this->apiToken = config('soccerapi.api_token');
if(empty($this->apiToken))
{
throw new \InvalidArgumentException('No API token set');
}
$this->timezone = empty(config('soccerapi.timezone')) ? config('app.timezone') : config('soccerapi.timezone');

$this->withoutData = empty(config('soccerapi.without_data')) ? false : config('soccerapi.without_data');
}

protected function call($url, $hasData = false)
{
$query = [
'api_token' => $this->apiToken,
'per_page' => $this->perPage,
'page' => $this->page
];
if(!empty($this->include))
{
$query['include'] = $this->include;
}
if ($this->timezone)
{
$query['tz'] = $this->timezone;
}
if(!empty($this->leagues))
{
$query['leagues'] = $this->leagues;
}

$response = $this->client->get($url, ['query' => $query]);

$body = json_decode($response->getBody()->getContents());

if(property_exists($body, 'error'))
{
if(is_object($body->error))
{
throw new ApiRequestException($body->error->message, $body->error->code);
}
else
{
throw new ApiRequestException($body->error, 500);
}

return $response;
}

if($hasData && $this->withoutData)
{
return $body->data;
}

return $body;
}

protected function callData($url)
{

return $this->call($url, true);
}

/**
* @param $include - string or array of relations to include with the query
*/
public function setInclude($include)
{
if(is_array($include) && !empty($include))
{
$include = implode(',', $include);
}

$this->include = $include;

return $this;
}

/**
* @param $leagues - string or array of leagues to return only specific leagues with the query
*/
public function setLeagues($leagues)
{
if(is_array($leagues) && !empty($leagues))
{
$leagues = implode(',', $leagues);
}

$this->leagues = $leagues;

return $this;
}

/**
* @param $perPage - int of per_page limit data in request
*/
public function setPerPage($perPage)
{
$this->perPage = $perPage;

return $this;
}

/**
* @param $page - int of requested page
*/
public function setPage($page)
{
$this->page = $page;

return $this;
}
}

0 comments on commit b43d38b

Please sign in to comment.