From b43d38b07fb1e8f9ea67d9042cded2a2d6d18d13 Mon Sep 17 00:00:00 2001 From: Sebastiaan Speck Date: Mon, 15 Oct 2018 20:24:29 +0200 Subject: [PATCH] Added this file because I added functionality to SoccerAPI that wasn't in the orignal package Added this file because I added functionality to SoccerAPI that wasn't in the orignal package --- soccerapi/SoccerAPIClient.php | 140 ++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 soccerapi/SoccerAPIClient.php diff --git a/soccerapi/SoccerAPIClient.php b/soccerapi/SoccerAPIClient.php new file mode 100644 index 00000000..53772ca2 --- /dev/null +++ b/soccerapi/SoccerAPIClient.php @@ -0,0 +1,140 @@ + '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; + } +}