first commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExerciseDb;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class ApiService
|
||||
{
|
||||
public const PER_PAGE = 50;
|
||||
|
||||
public const API_BASE_URL = 'https://www.exercisedb.dev';
|
||||
public const EXERCISE_URI = '/api/v1/exercises';
|
||||
|
||||
/**
|
||||
* Fetch a page of exercises from ExerciseDB API.
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function fetch(int $page = 1): array
|
||||
{
|
||||
$results = $this->call(
|
||||
self::EXERCISE_URI,
|
||||
$this->pagination($page),
|
||||
);
|
||||
|
||||
$decoded = json_decode($results, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
private function pagination(int $page = 1): array
|
||||
{
|
||||
return [
|
||||
'offset' => ($page - 1) * self::PER_PAGE,
|
||||
'limit' => self::PER_PAGE,
|
||||
];
|
||||
}
|
||||
|
||||
private function call(
|
||||
string $uri,
|
||||
array $params = [],
|
||||
): string {
|
||||
$client = new Client();
|
||||
|
||||
$url = self::API_BASE_URL . $uri;
|
||||
|
||||
return $client->request('GET', $url, [
|
||||
'query' => $params,
|
||||
])->getBody()->getContents();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user