diff --git a/composer.json b/composer.json index 9f4a16f..e166d21 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,21 @@ { "name": "retroachievements/api", - "description": "RetroAchievements Web API Client", "type": "library", + "description": "RetroAchievements Web API Client", + "license": "MIT", "keywords": [ - "RetroAchievmenets", + "RetroAchievements", "retro", "achievements", "API" ], "homepage": "https://github.com/retroachievements/api-php", - "license": "MIT", "require": { - "php": "^8.2" + "php": "^8.2", + "guzzlehttp/guzzle": "^6.5.8|^7.8.1" }, - "require-dev": {}, - "minimum-stability": "dev", - "prefer-stable": true, - "config": { - "sort-packages": true + "require-dev": { + "symfony/var-dumper": "^6.4|^7.0" }, "autoload": { "psr-4": { @@ -29,5 +27,10 @@ "Tests\\": "tests" } }, - "scripts": {} + "scripts": {}, + "config": { + "sort-packages": true + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Exceptions/GenericException.php b/src/Exceptions/GenericException.php new file mode 100644 index 0000000..537ac55 --- /dev/null +++ b/src/Exceptions/GenericException.php @@ -0,0 +1,11 @@ +request('GET', $uri); + } + + protected function post(string $uri, array $payload = []) + { + return $this->request('POST', $uri, $payload); + } + + protected function put(string $uri, array $payload = []) + { + return $this->request('PUT', $uri, $payload); + } + + protected function delete(string $uri, array $payload = []) + { + return $this->request('DELETE', $uri, $payload); + } + + protected function request(string $verb, string $uri, array $payload = []) + { + $response = $this->client->request( + $verb, + $uri, + empty($payload) ? [] : ['form_params' => $payload] + ); + + if (! $this->isSuccessful($response)) { + $this->handleRequestError($response); + } + + $responseBody = (string) $response->getBody(); + + return json_decode($responseBody, true) ?: $responseBody; + } + + protected function isSuccessful(ResponseInterface $response): bool + { + return (int) substr((string) $response->getStatusCode(), 0, 1) === 2; + } + + protected function handleRequestError(ResponseInterface $response): void + { + if ($response->getStatusCode() === 404) { + throw new NotFoundException((string) $response->getBody()); + } + + if ($response->getStatusCode() === 401) { + throw new UnauthorizedException((string) $response->getBody()); + } + + throw new GenericException((string) $response->getBody()); + } +} diff --git a/src/RetroAchievements.php b/src/RetroAchievements.php new file mode 100644 index 0000000..b89104c --- /dev/null +++ b/src/RetroAchievements.php @@ -0,0 +1,138 @@ +client = new Client([ + 'base_uri' => $baseUri, + 'http_errors' => false, + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + ]); + } + + public function setClient(Client $client): self + { + $this->client = $client; + + return $this; + } + + public function getTopTenUsers(): mixed + { + return $this->getApiUrl('API_GetTopTenUsers.php'); + } + + public function getGameInfo(int $gameID): mixed + { + return $this->getApiUrl('API_GetGame.php', [ + 'i' => $gameID, + ]); + } + + public function getGameInfoExtended(int $gameID): mixed + { + return $this->getApiUrl('API_GetGameExtended.php', [ + 'i' => $gameID, + ]); + } + + public function getConsoleIDs(): mixed + { + return $this->getApiUrl('API_GetConsoleIDs.php'); + } + + public function getGameList(int $consoleID): mixed + { + return $this->getApiUrl('API_GetGameList.php', [ + 'i' => $consoleID, + ]); + } + + public function getUserRankAndScore(string $user): mixed + { + return $this->getApiUrl('API_GetUserRankAndScore.php', [ + 'u' => $user, + ]); + } + + public function getUserProgress(string $user, string $gameIDCSV): mixed + { + $gameIDCSV = preg_replace('/\s+/', '', $gameIDCSV); // Remove all whitespace + + return $this->getApiUrl('API_GetUserProgress.php', [ + 'u' => $user, + 'i' => $gameIDCSV, + ]); + } + + public function getUserRecentlyPlayedGames(string $user, int $count, int $offset = 0): mixed + { + return $this->getApiUrl('API_GetUserRecentlyPlayedGames.php', [ + 'u' => $user, + 'c' => $count, + 'o' => $offset, + ]); + } + + public function getUserSummary(string $user, int $numRecentGames): mixed + { + return $this->getApiUrl('API_GetUserSummary.php', [ + 'u' => $user, + 'g' => $numRecentGames, + 'a' => 5, + ]); + } + + public function getGameInfoAndUserProgress(string $user, int $gameID): mixed + { + return $this->getApiUrl('API_GetGameInfoAndUserProgress.php', [ + 'u' => $user, + 'g' => $gameID, + ]); + } + + public function getAchievementsEarnedOnDay(string $user, string $date): mixed + { + return $this->getApiUrl('API_GetAchievementsEarnedOnDay.php', [ + 'u' => $user, + 'd' => $date, + ]); + } + + public function getAchievementsEarnedBetween(string $user, string $startDate, string $endDate): mixed + { + return $this->getApiUrl('API_GetAchievementsEarnedBetween.php', [ + 'u' => $user, + 'f' => strtotime($startDate), + 't' => strtotime($endDate), + ]); + } + + protected function getApiUrl(string $endpoint, array $parameters = []): mixed + { + return $this->get( + sprintf('/API/%s?%s', $endpoint, http_build_query([ + 'z' => $this->username, + 'y' => $this->apiKey, + ] + $parameters)) + ); + } +} diff --git a/src/RetroAchievementsApiClient.php b/src/RetroAchievementsApiClient.php deleted file mode 100644 index 4903ac2..0000000 --- a/src/RetroAchievementsApiClient.php +++ /dev/null @@ -1,185 +0,0 @@ -ra_user = $user; - $this->ra_api_key = $api_key; - } - - /** - * @return string - */ - private function AuthQS() - { - return "?z=" . $this->ra_user . "&y=" . $this->ra_api_key; - } - - /** - * @param string $target - * @param string $params - * @return bool|null|string - */ - private function GetRAURL($target, $params = "") - { - try { - return file_get_contents(self::API_URL . $target . self::AuthQS() . "&$params"); - } catch (Exception $e) { - } - return null; - } - - /** - * @return mixed - */ - public function GetTopTenUsers() - { - return json_decode(self::GetRAURL('API_GetTopTenUsers.php')); - } - - /** - * @param int $gameID - * @return mixed - */ - public function GetGameInfo($gameID) - { - return json_decode(self::GetRAURL("API_GetGame.php", "i=$gameID")); - } - - /** - * @param int $gameID - * @return mixed - */ - public function GetGameInfoExtended($gameID) - { - return json_decode(self::GetRAURL("API_GetGameExtended.php", "i=$gameID")); - } - - /** - * @return mixed - */ - public function GetConsoleIDs() - { - return json_decode(self::GetRAURL('API_GetConsoleIDs.php')); - } - - /** - * @param int $consoleID - * @return mixed - */ - public function GetGameList($consoleID) - { - return json_decode(self::GetRAURL("API_GetGameList.php", "i=$consoleID")); - } - - /** - * @param string $user - * @param int $count - * @param int $offset - * @return mixed - */ - public function GetFeedFor($user, $count, $offset = 0) - { - return json_decode(self::GetRAURL("API_GetFeed.php", "u=$user&c=$count&o=$offset")); - } - - /** - * @param string $user - * @return mixed - */ - public function GetUserRankAndScore($user) - { - return json_decode(self::GetRAURL("API_GetUserRankAndScore.php", "u=$user")); - } - - /** - * @param string $user - * @param string $gameIDCSV - * @return mixed - */ - public function GetUserProgress($user, $gameIDCSV) - { - $gameIDCSV = preg_replace('/\s+/', '', $gameIDCSV); // Remove all whitespace - return json_decode(self::GetRAURL("API_GetUserProgress.php", "u=$user&i=$gameIDCSV")); - } - - /** - * @param string $user - * @param int $count - * @param int $offset - * @return mixed - */ - public function GetUserRecentlyPlayedGames($user, $count, $offset = 0) - { - return json_decode(self::GetRAURL("API_GetUserRecentlyPlayedGames.php", "u=$user&c=$count&o=$offset")); - } - - /** - * @param string $user - * @param int $numRecentGames - * @return mixed - */ - public function GetUserSummary($user, $numRecentGames) - { - return json_decode(self::GetRAURL("API_GetUserSummary.php", "u=$user&g=$numRecentGames&a=5")); - } - - /** - * @param string $user - * @param int $gameID - * @return mixed - */ - public function GetGameInfoAndUserProgress($user, $gameID) - { - return json_decode(self::GetRAURL("API_GetGameInfoAndUserProgress.php", "u=$user&g=$gameID")); - } - - /** - * @param string $user - * @param int $dateInput - * @return mixed - */ - public function GetAchievementsEarnedOnDay($user, $dateInput) - { - return json_decode(self::GetRAURL("API_GetAchievementsEarnedOnDay.php", "u=$user&d=$dateInput")); - } - - /** - * @param string $user - * @param int $dateStart - * @param int $dateEnd - * @return mixed - */ - public function GetAchievementsEarnedBetween($user, $dateStart, $dateEnd) - { - $dateFrom = strtotime($dateStart); - $dateTo = strtotime($dateEnd); - return json_decode(self::GetRAURL("API_GetAchievementsEarnedBetween.php", "u=$user&f=$dateFrom&t=$dateTo")); - } -} - -;