diff --git a/common/components/Mojang/Api.php b/common/components/Mojang/Api.php new file mode 100644 index 0000000..70e253d --- /dev/null +++ b/common/components/Mojang/Api.php @@ -0,0 +1,49 @@ +createClient(); + $request = $client->createRequest('GET', 'https://api.mojang.com/users/profiles/minecraft/' . $username); + $queryParams = []; + if ($atTime !== null) { + $queryParams['atTime'] = $atTime; + } + + $request->setQuery($queryParams); + $response = $client->send($request); + if ($response->getStatusCode() === 204) { + throw new NoContentException('Username not found'); + } elseif ($response->getStatusCode()) { + throw new MojangApiException('Unexpected request result'); + } + + $data = $response->json(['object' => false]); + $responseObj = new UsernameToUUIDResponse(); + $responseObj->id = $data['id']; + $responseObj->name = $data['name']; + $responseObj->legacy = isset($data['legacy']); + $responseObj->demo = isset($data['demo']); + + return $responseObj; + } + + protected function createClient() { + return new GuzzleClient(); + } + +} diff --git a/common/components/Mojang/exceptions/MojangApiException.php b/common/components/Mojang/exceptions/MojangApiException.php new file mode 100644 index 0000000..954f76d --- /dev/null +++ b/common/components/Mojang/exceptions/MojangApiException.php @@ -0,0 +1,8 @@ +=5.6.0", - "yiisoft/yii2": ">=2.0.6", + "yiisoft/yii2": "~2.0.6", "yiisoft/yii2-bootstrap": "*", "yiisoft/yii2-swiftmailer": "*", - "ramsey/uuid": "^3.1", + "ramsey/uuid": "~3.1", "league/oauth2-server": "~4.1.5", "yiisoft/yii2-redis": "~2.0.0", - "damirka/yii2-jwt": "dev-master#be29a5b5d7e7d146c13d4374788e02c54cc04138" + "damirka/yii2-jwt": "dev-master#be29a5b5d7e7d146c13d4374788e02c54cc04138", + "guzzlehttp/guzzle": "~5.3.0" }, "require-dev": { "yiisoft/yii2-codeception": "*",