mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
update v.3.2 WIP
This commit is contained in:
parent
215337867f
commit
aac140f99b
@ -8,6 +8,7 @@ use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Jikan\Exception\BadResponseException;
|
||||
use Jikan\Exception\ParserException;
|
||||
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
|
||||
use Symfony\Component\Debug\Exception\FlattenException;
|
||||
@ -51,20 +52,14 @@ class Handler extends ExceptionHandler
|
||||
{
|
||||
|
||||
if ($e instanceof ParserException) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
return response()->json(['error' => $e->getMessage()], $e->getCode());
|
||||
}
|
||||
if ($e instanceof ClientException) {
|
||||
return response()->json(['error' => $e->getMessage()], $e->getCode());
|
||||
}
|
||||
|
||||
// Bugsnag::notifyException($e);
|
||||
// if ($e instanceof HttpResponseException) {
|
||||
// } elseif ($e instanceof ModelNotFoundException) {
|
||||
// $e = new NotFoundHttpException($e->getMessage(), $e);
|
||||
// } elseif ($e instanceof AuthorizationException) {
|
||||
// $e = new HttpException(403, $e->getMessage());
|
||||
// } elseif ($e instanceof ValidationException && $e->getResponse()) {
|
||||
// }
|
||||
if ($e instanceof BadResponseException) {
|
||||
return response()->json(['error' => $e->getMessage()], $e->getCode());
|
||||
}
|
||||
|
||||
$fe = FlattenException::create($e);
|
||||
|
||||
|
@ -8,7 +8,10 @@ use Jikan\Request\Anime\AnimeForumRequest;
|
||||
use Jikan\Request\Anime\AnimeMoreInfoRequest;
|
||||
use Jikan\Request\Anime\AnimeNewsRequest;
|
||||
use Jikan\Request\Anime\AnimePicturesRequest;
|
||||
use Jikan\Request\Anime\AnimeRecentlyUpdatedByUsersRequest;
|
||||
use Jikan\Request\Anime\AnimeRecommendationsRequest;
|
||||
use Jikan\Request\Anime\AnimeRequest;
|
||||
use Jikan\Request\Anime\AnimeReviewsRequest;
|
||||
use Jikan\Request\Anime\AnimeStatsRequest;
|
||||
use Jikan\Request\Anime\AnimeVideosRequest;
|
||||
|
||||
@ -67,4 +70,22 @@ class AnimeController extends Controller
|
||||
$anime = ['moreinfo' => $this->jikan->getAnimeMoreInfo(new AnimeMoreInfoRequest($id))];
|
||||
return response(json_encode($anime));
|
||||
}
|
||||
|
||||
public function recommendations(int $id)
|
||||
{
|
||||
$anime = ['recommendations' => $this->jikan->getAnimeRecommendations(new AnimeRecommendationsRequest($id))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
|
||||
public function userupdates(int $id, int $page = 1)
|
||||
{
|
||||
$anime = ['users' => $this->jikan->getAnimeRecentlyUpdatedByUsers(new AnimeRecentlyUpdatedByUsersRequest($id, $page))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
|
||||
public function reviews(int $id, int $page = 1)
|
||||
{
|
||||
$anime = ['reviews' => $this->jikan->getAnimeReviews(new AnimeReviewsRequest($id, $page))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
}
|
||||
|
22
app/Http/Controllers/V3/ClubController.php
Normal file
22
app/Http/Controllers/V3/ClubController.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\V3;
|
||||
|
||||
|
||||
use Jikan\Request\Club\ClubRequest;
|
||||
use Jikan\Request\Club\UserListRequest;
|
||||
|
||||
class ClubController extends Controller
|
||||
{
|
||||
public function main(int $id)
|
||||
{
|
||||
$club = $this->jikan->getClub(new ClubRequest($id));
|
||||
return response($this->serializer->serialize($club, 'json'));
|
||||
}
|
||||
|
||||
public function members(int $id, int $page = 1)
|
||||
{
|
||||
$club = ['members' => $this->jikan->getClubUsers(new UserListRequest($id, $page))];
|
||||
return response($this->serializer->serialize($club, 'json'));
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ namespace App\Http\Controllers\V3;
|
||||
use Jikan\Request\Genre\AnimeGenreRequest;
|
||||
use Jikan\Request\Genre\MangaGenreRequest;
|
||||
|
||||
|
||||
class GenreController extends Controller
|
||||
{
|
||||
public function anime(int $id, int $page = 1)
|
||||
|
@ -7,7 +7,10 @@ use Jikan\Request\Manga\MangaForumRequest;
|
||||
use Jikan\Request\Manga\MangaMoreInfoRequest;
|
||||
use Jikan\Request\Manga\MangaNewsRequest;
|
||||
use Jikan\Request\Manga\MangaPicturesRequest;
|
||||
use Jikan\Request\Manga\MangaRecentlyUpdatedByUsersRequest;
|
||||
use Jikan\Request\Manga\MangaRecommendationsRequest;
|
||||
use Jikan\Request\Manga\MangaRequest;
|
||||
use Jikan\Request\Manga\MangaReviewsRequest;
|
||||
use Jikan\Request\Manga\MangaStatsRequest;
|
||||
|
||||
class MangaController extends Controller
|
||||
@ -53,4 +56,22 @@ class MangaController extends Controller
|
||||
$manga = ['moreinfo' => $this->jikan->getMangaMoreInfo(new MangaMoreInfoRequest($id))];
|
||||
return response(json_encode($manga));
|
||||
}
|
||||
|
||||
public function recommendations(int $id)
|
||||
{
|
||||
$anime = ['recommendations' => $this->jikan->getMangaRecommendations(new MangaRecommendationsRequest($id))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
|
||||
public function userupdates(int $id, int $page = 1)
|
||||
{
|
||||
$anime = ['users' => $this->jikan->getMangaRecentlyUpdatedByUsers(new MangaRecentlyUpdatedByUsersRequest($id, $page))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
|
||||
public function reviews(int $id, int $page = 1)
|
||||
{
|
||||
$anime = ['reviews' => $this->jikan->getMangaReviews(new MangaReviewsRequest($id, $page))];
|
||||
return response($this->serializer->serialize($anime, 'json'));
|
||||
}
|
||||
}
|
||||
|
@ -38,4 +38,10 @@ class SeasonController extends Controller
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function later()
|
||||
{
|
||||
$season = $this->jikan->getSeasonal(new SeasonalRequest(null, null, true));
|
||||
return response($this->serializer->serialize($season, 'json'));
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ class Meta
|
||||
$this->updateMeta("requests:monthly", $requestUri, 2629746);
|
||||
|
||||
|
||||
return $next($request);
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function updateMeta($key, $req, $expire) {
|
||||
|
@ -9,6 +9,10 @@ class RedisCache
|
||||
{
|
||||
protected const CACHE_EXPIRY = 43200; // 6 hours
|
||||
|
||||
private $fingerprint;
|
||||
private $cached = false;
|
||||
private $response;
|
||||
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
//debug
|
||||
@ -29,41 +33,50 @@ class RedisCache
|
||||
$requestType = $request->segments()[0];
|
||||
}
|
||||
|
||||
$hashKey = "request:{$requestType}:" . sha1($key);
|
||||
$cached = true;
|
||||
$this->fingerprint = "request:{$requestType}:" . sha1($key);
|
||||
$this->cached = (bool) app('redis')->exists($this->fingerprint);
|
||||
|
||||
if (!app('redis')->exists($hashKey)) {
|
||||
// ETag
|
||||
if (
|
||||
$request->hasHeader('If-None-Match')
|
||||
&& app('redis')->exists($this->fingerprint)
|
||||
&& md5(app('redis')->get($this->fingerprint)) === $request->header('If-None-Match')
|
||||
) {
|
||||
return response('', 304);
|
||||
}
|
||||
|
||||
|
||||
if (!app('redis')->exists($this->fingerprint)) {
|
||||
|
||||
$response = $next($request);
|
||||
$cached = false;
|
||||
|
||||
if ($this->isError($response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
app('redis')->set(
|
||||
$hashKey,
|
||||
$this->fingerprint,
|
||||
$response->original
|
||||
);
|
||||
app('redis')->expire($hashKey, self::CACHE_EXPIRY);
|
||||
app('redis')->expire($this->fingerprint, self::CACHE_EXPIRY);
|
||||
}
|
||||
|
||||
$data = app('redis')->get($this->fingerprint);
|
||||
$ttl = app('redis')->ttl($this->fingerprint);
|
||||
|
||||
return response()->json(
|
||||
array_merge(
|
||||
[
|
||||
'request_hash' => $hashKey,
|
||||
'request_cached' => $cached,
|
||||
'request_cache_expiry' => app('redis')->ttl($hashKey),
|
||||
],
|
||||
json_decode(
|
||||
app('redis')->get($hashKey),
|
||||
true
|
||||
)
|
||||
)
|
||||
);
|
||||
[
|
||||
'request_hash' => $this->fingerprint,
|
||||
'request_cached' => $this->cached,
|
||||
'request_cache_expiry' => $ttl,
|
||||
]
|
||||
+
|
||||
json_decode($data, true)
|
||||
)
|
||||
->setEtag(md5($data));
|
||||
}
|
||||
|
||||
private function isError($response) {
|
||||
private function isError($response) : bool {
|
||||
return isset($response->original['error']);
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ try {
|
||||
*/
|
||||
define('BLACKLIST_PATH', __DIR__.'/../storage/app/blacklist.json');
|
||||
|
||||
define('REST_VERSION', '3.1');
|
||||
define('SOURCE_VERSION', '2.0.0');
|
||||
define('REST_VERSION', '3.2');
|
||||
define('SOURCE_VERSION', '2.4.0');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@ -88,7 +88,7 @@ $app->routeMiddleware([
|
||||
'redis-cache' => App\Http\Middleware\RedisCache::class,
|
||||
'throttle' => App\Http\Middleware\Throttle::class,
|
||||
'slave-auth' => App\Http\Middleware\SlaveAuthentication::class,
|
||||
'cachet' => App\Http\Middleware\Cachet::class
|
||||
// 'cachet' => App\Http\Middleware\Cachet::class
|
||||
]);
|
||||
|
||||
/*
|
||||
|
@ -21,8 +21,8 @@
|
||||
"divineomega/cachetphp": "^0.2.0",
|
||||
"jms/serializer": "^1.13",
|
||||
"symfony/yaml": "^4.1",
|
||||
"fabpot/goutte": "dev-http-exceptions as 3.2.x-dev",
|
||||
"jikan-me/jikan": "dev-master#7c88915"
|
||||
"fabpot/goutte": "3.2.3",
|
||||
"jikan-me/jikan": "^2.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fzaninotto/faker": "~1.4",
|
||||
|
516
composer.lock
generated
516
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -24,4 +24,4 @@
|
||||
<ifmodule mod_deflate.c>
|
||||
AddOutputFilterByType DEFLATE text/text text/json text/html text/plain text/xml text/css application/x-javascript application/javascript
|
||||
</ifmodule>
|
||||
# END GZIP
|
||||
# END GZIP
|
||||
|
@ -6,8 +6,8 @@ $router->get('/', function () use ($router) {
|
||||
return response()->json([
|
||||
'Author' => '@irfanDahir',
|
||||
'Contact' => 'irfan@jikan.moe',
|
||||
'JikanREST' => '2.0',
|
||||
'JikanPHP' => '2.0.0-rc.2',
|
||||
'JikanREST' => '2.2',
|
||||
'JikanPHP' => '2.0.0',
|
||||
'Website' => 'https://jikan.moe',
|
||||
'Docs' => 'https://jikan.docs.apiary.io',
|
||||
'GitHub' => 'https://github.com/jikan-me/jikan',
|
||||
|
@ -6,9 +6,9 @@ $router->get('/', function () use ($router) {
|
||||
|
||||
return response()->json([
|
||||
'Author' => '@irfanDahir',
|
||||
'Contact' => 'irfan@jikan.moe',
|
||||
'JikanREST' => '3.0',
|
||||
'JikanPHP' => '2.0.0-rc.2',
|
||||
'Contact' => 'irfan@dahir.co',
|
||||
'JikanREST' => '3.2',
|
||||
'JikanPHP' => '2.6.0',
|
||||
'Website' => 'https://jikan.moe',
|
||||
'Docs' => 'https://jikan.docs.apiary.io',
|
||||
'GitHub' => 'https://github.com/jikan-me/jikan',
|
||||
@ -80,6 +80,18 @@ $router->group(
|
||||
$router->get('/moreinfo', [
|
||||
'uses' => 'AnimeController@moreInfo'
|
||||
]);
|
||||
|
||||
$router->get('/recommendations', [
|
||||
'uses' => 'AnimeController@recommendations'
|
||||
]);
|
||||
|
||||
$router->get('/userupdates[/{page:[0-9]+}]', [
|
||||
'uses' => 'AnimeController@userupdates'
|
||||
]);
|
||||
|
||||
$router->get('/reviews[/{page:[0-9]+}]', [
|
||||
'uses' => 'AnimeController@reviews'
|
||||
]);
|
||||
}
|
||||
);
|
||||
|
||||
@ -115,6 +127,18 @@ $router->group(
|
||||
$router->get('/moreinfo', [
|
||||
'uses' => 'MangaController@moreInfo'
|
||||
]);
|
||||
|
||||
$router->get('/recommendations', [
|
||||
'uses' => 'MangaController@recommendations'
|
||||
]);
|
||||
|
||||
$router->get('/userupdates[/{page:[0-9]+}]', [
|
||||
'uses' => 'MangaController@userupdates'
|
||||
]);
|
||||
|
||||
$router->get('/reviews[/{page:[0-9]+}]', [
|
||||
'uses' => 'MangaController@reviews'
|
||||
]);
|
||||
}
|
||||
);
|
||||
|
||||
@ -149,7 +173,11 @@ $router->group(
|
||||
);
|
||||
|
||||
$router->get('season/archive', [
|
||||
'uses' => 'SeasonController@archive'
|
||||
'uses' => 'SeasonController@archive'
|
||||
]);
|
||||
|
||||
$router->get('season/later', [
|
||||
'uses' => 'SeasonController@later'
|
||||
]);
|
||||
|
||||
$router->get('season[/{year:[0-9]{4}}/{season:[A-Za-z]+}]', [
|
||||
@ -265,4 +293,19 @@ $router->group(
|
||||
]);
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
$router->group(
|
||||
[
|
||||
'prefix' => 'club/{id:[0-9]+}'
|
||||
],
|
||||
function() use ($router) {
|
||||
$router->get('/', [
|
||||
'uses' => 'ClubController@main'
|
||||
]);
|
||||
|
||||
$router->get('/members[/{page:[0-9]+}]', [
|
||||
'uses' => 'ClubController@members'
|
||||
]);
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user