Skip to content

Commit

Permalink
improve documentation + add response helper
Browse files Browse the repository at this point in the history
  • Loading branch information
SethSharp committed Jul 31, 2024
1 parent 9bd550e commit fa0f331
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ php artisan vendor:publish --tag="odds-api-config"
```

### Example Usages
You can simply create a new Client, passing in your api key and thats it!
You can simply create a new Client, passing in your api key and that's it! You can decide to bind the class in
your `AppServiceProvider`, but if not, the client can easily be initialised in any `__invoke` or `__construct`
```php
$client = new OddsClient(config('odds-api.api_key'));

Expand All @@ -45,9 +46,8 @@ $response = $client->setRegion('us')
return $response->json();
```

This package is setup in a way that all the params you may need are functions which can be chained together on one
line, as they all return `$this`. Once you call one of the API endpoints which return a response, you can no longer call
these functions.
This package is set up in a way that all the params you need can be built using chainable function helpers, as they all return `$this`.
Once you call one of the API endpoints which return a response, you can no longer call these function helpers.

**Another way to define your Client Class**

Expand All @@ -60,12 +60,14 @@ $this->app->bind(OddsClient::class, function () {
```
then your class may look like
```php
use HandlesOddsResponse;

public function __invoke(OddsClient $client): Response
{
$response = $client->setRegion('us')
->getOddsForSport(SportsEnum::RUGBYLEAGUE_NRL);

return $response->json();
return $this->extractJsonFromResponse($response);
}
```

Expand Down
13 changes: 13 additions & 0 deletions src/Traits/HandlesOddsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SethSharp\OddsApi\Traits;

use GuzzleHttp\Psr7\Response;

trait HandlesOddsResponse
{
protected function extractJsonFromResponse(Response $response): mixed
{
return json_decode($response->getBody(), true);
}
}

0 comments on commit fa0f331

Please sign in to comment.