Skip to content

Commit edec756

Browse files
committed
major changes and improvements
1 parent 527201f commit edec756

File tree

12 files changed

+230
-189
lines changed

12 files changed

+230
-189
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ A wrapper simplifies the usage and implementation of powerful APIs like "The Odd
1818
# Contribution Guide
1919
This is an open-source project, so contributions are welcome! Whether you want to add new features, fix bugs, or improve documentation, your help is appreciated. Submit your PR for review and I will review them as soon as possible.
2020

21+
**Sport Enum opportunity**
22+
This package uses an enum to define the sports you can pass to the endpoints, this ensures you don't make a typo or have
23+
to worry about remembering the exact grammar. Currently, there are only a couple sports so if you need the support for more,
24+
make a PR to add them in.
25+
2126
# Steps for Installation
2227
### Composer
2328
```bash
@@ -30,6 +35,57 @@ This file contains some essential information the Client requires to make succes
3035
php artisan vendor:publish --tag="odds-api-config"
3136
```
3237

38+
### Example Usages
39+
You can simply create a new Client, passing in your api key and thats it!
40+
```php
41+
$client = new OddsClient(config('odds-api.api_key'));
42+
43+
$response = $client->setRegion('us')
44+
->dateFormat('iso')
45+
->getOddsForSport(SportsEnum::RUGBYLEAGUE_NRL);
46+
47+
return $response->json();
48+
```
49+
50+
This package is setup in a way that all the params you may need are functions which can be chained together on one
51+
line, as they all return `$this`. Once you call one of the API endpoints which return a response, you can no longer call
52+
these functions.
53+
54+
**Another way to define your Client Class**
55+
56+
You can bind your Client class at runtime in the AppServiceProvider. Allowing you to simply define the Client
57+
in the constructor of your class, without having to constantly pass the api credentials.
58+
```php
59+
$this->app->bind(OddsClient::class, function () {
60+
return new OddsClient(config('odds-api.api_key'));
61+
});
62+
```
63+
then your class may look like
64+
```php
65+
public function __invoke(OddsClient $client): Response
66+
{
67+
$response = $client->setRegion('us')
68+
->getOddsForSport(SportsEnum::RUGBYLEAGUE_NRL);
69+
70+
return $response->json();
71+
}
72+
```
73+
74+
**Additional**
75+
When constructing the Client, it will have some default parameters
76+
```php
77+
$this->params = [
78+
'api_key' => $this->apiKey,
79+
'regions' => config('odds-api.default_region'),
80+
'oddsFormat' => config('odds-api.default_odds_format')
81+
];
82+
```
83+
This avoids having to define these on each request, but they can be overwritten with their corresponding class functions ie;
84+
`setRegions('au')`
85+
86+
Also if this API ever becomes outdated for a small period of time, and you require to use new parameters, you can utilise
87+
the `addParams()` function, which accepts an array where you can pass any new parameters.
88+
3389
## Credits
3490
- [Seth Sharp](https://github.com/SethSharp)
3591
- [All Contributors](https://github.com/SethSharp/odds-api/graphs/contributors)

config/odds-api.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
return [
44
'secret' => env('ODDS_API_KEY'),
5-
'version' => 'v4'
5+
'version' => 'v4',
6+
'default_region' => 'au',
7+
'default_odds_format' => 'decimal'
68
];

src/Enums/SportsEnum.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
enum SportsEnum: string
66
{
7-
// todo: fill in with the rest of the sports from https://the-odds-api.com/sports-odds-data/sports-apis.html
7+
// todo: https://the-odds-api.com/sports-odds-data/sports-apis.html
8+
// Benefits are that you don't need to remember the exact naming, just start typing the sport in the enum
89
case RUGBYLEAGUE_NRL = 'rugbyleague_nrl';
910
case AUSSIERULES_AFL = 'aussierules_afl';
1011
}

src/OddsClient.php

Lines changed: 169 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Response;
7+
use SethSharp\OddsApi\Enums\SportsEnum;
78
use GuzzleHttp\Exception\RequestException;
89
use SethSharp\OddsApi\Traits\UseHandleHeaders;
9-
use SethSharp\OddsApi\Traits\Endpoints\UseOdds;
10-
use SethSharp\OddsApi\Traits\Endpoints\UseSports;
1110

1211
class OddsClient
1312
{
14-
use UseOdds;
15-
use UseSports;
1613
use UseHandleHeaders;
1714

1815
private Client $client;
1916
private string $apiKey;
2017
private string $apiEndpoint = 'https://api.the-odds-api.com';
18+
private array $params = [];
2119

2220
public function __construct(string $apiKey, $apiEndpoint = null)
2321
{
@@ -33,29 +31,189 @@ public function __construct(string $apiKey, $apiEndpoint = null)
3331
'Content-Type' => 'application/json'
3432
]
3533
]);
34+
35+
$this->setDefaultParams();
3636
}
3737

3838
public function getApiEndpoint(): string
3939
{
4040
return $this->apiEndpoint;
4141
}
4242

43-
// todo: mocked endpoint tests
44-
protected function get($endpoint, $params = []): Response
43+
protected function get($endpoint): Response
4544
{
46-
$params['apiKey'] = $this->apiKey;
47-
4845
try {
4946
return $this->client->get('/' . config('odds-api.version') . $endpoint, [
50-
'query' => $params
47+
'query' => $this->params
5148
]);
5249
} catch (RequestException $e) {
5350
throw new \RuntimeException('Request failed: ' . $e->getMessage());
5451
}
5552
}
5653

57-
protected function decodeResponse($response)
54+
private function setDefaultParams(): void
55+
{
56+
$this->params = [
57+
'api_key' => $this->apiKey,
58+
'regions' => config('odds-api.default_region'),
59+
'oddsFormat' => config('odds-api.default_odds_format')
60+
];
61+
}
62+
63+
/**
64+
* @return Response
65+
*/
66+
public function getSports(): Response
67+
{
68+
return $this->get('/sports');
69+
}
70+
71+
/**
72+
* @param SportsEnum $sport
73+
* @return Response
74+
*/
75+
public function getScoresForSport(SportsEnum $sport): Response
76+
{
77+
return $this->get("/sports/{$sport->value}/scores");
78+
}
79+
80+
/**
81+
* @param SportsEnum $sport
82+
* @return Response
83+
*/
84+
public function getOddsForSport(SportsEnum $sport): Response
85+
{
86+
return $this->get("/sports/$sport->value/odds");
87+
}
88+
89+
/**
90+
* @param SportsEnum $sport
91+
* @param string $event
92+
* @return Response
93+
*/
94+
public function getOddsForEvent(SportsEnum $sport, string $event): Response
95+
{
96+
return $this->get("/sports/{$sport->value}/events/{$event}/odds");
97+
}
98+
99+
/**
100+
* @param string $regionCode
101+
* @return $this
102+
*/
103+
public function setRegion(string $regionCode): self
104+
{
105+
$this->params['regions'] = $regionCode;
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* https://the-odds-api.com/sports-odds-data/betting-markets.html
112+
*
113+
* @param mixed $markets
114+
* @return $this
115+
*/
116+
public function setMarkets(mixed $markets): self
117+
{
118+
if (is_array($markets)) {
119+
$marketString = http_build_query($markets, '', ',');
120+
}
121+
122+
$this->params['markets'] = $marketString ?? $markets;
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* https://the-odds-api.com/sports-odds-data/bookmaker-apis.html
129+
*
130+
* @param mixed $bookmakers
131+
* @return $this
132+
*/
133+
public function setBookmakers(mixed $bookmakers): self
134+
{
135+
if (is_array($bookmakers)) {
136+
$bookmakerString = http_build_query($bookmakers, '', ',');
137+
}
138+
139+
$this->params['markets'] = $bookmakerString ?? $bookmakers;
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* @param string $eventId
146+
* @return $this
147+
*/
148+
public function setEvent(string $eventId): self
149+
{
150+
$this->params['eventIds'] = $eventId;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* decimal or american
157+
*
158+
* @param string $format
159+
* @return $this
160+
*/
161+
public function oddsFormat(string $format): self
162+
{
163+
$this->params['oddsFormat'] = $format;
164+
165+
return $this;
166+
}
167+
168+
/**
169+
* Format of returned timestamps. Can be iso (ISO8601) or unix timestamp (seconds since epoch)
170+
*
171+
* @param string $dateFormat
172+
* @return $this
173+
*/
174+
public function dateFormat(string $dateFormat): self
58175
{
59-
return json_decode($response->getBody(), true);
176+
$this->params['dateForm'] = $dateFormat;
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* Filters the response to show events that commence on and after this parameter. Values are in ISO8601 format
183+
*
184+
* @param string $time
185+
* @return $this
186+
*/
187+
public function commenceTimeFrom(string $time): self
188+
{
189+
$this->params['commenceTimeFrom'] = $time;
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* Filters the response to show events that commence on and before this parameter. Values are in ISO8601 format
196+
*
197+
* @param string $time
198+
* @return $this
199+
*/
200+
public function commenceTimeTo(string $time): self
201+
{
202+
$this->params['commenceTimeTo'] = $time;
203+
204+
return $this;
205+
}
206+
207+
/**
208+
* Additional params in case API is not caught up with the latest API
209+
*
210+
* @param array $params
211+
* @return self
212+
*/
213+
public function addParams(array $params): self
214+
{
215+
$this->params = array_merge_recursive($this->params, $params);
216+
217+
return $this;
60218
}
61219
}

src/Traits/Endpoints/UseOdds.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Traits/Endpoints/UseSports.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Traits/UseValidatesParams.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/OddsClientTest.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)