repod/lib/Controller/TopController.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2023-07-02 22:12:40 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use Exception;
use OCA\RePod\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
class TopController extends Controller
{
private IClientService $clientService;
public function __construct(IRequest $request, IClientService $clientService) {
parent::__construct(Application::APP_ID, $request);
$this->clientService = $clientService;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(int $limit = 10): JSONResponse {
if (!in_array($limit, [10, 25, 50])) {
2023-07-02 22:20:18 +00:00
return new JSONResponse(['Invalid limit, can be 10, 25 or 50.'], Http::STATUS_BAD_REQUEST);
2023-07-02 22:12:40 +00:00
}
try {
$client = $this->clientService->newClient();
$response = $client->get("https://rss.applemarketingtools.com/api/v2/fr/podcasts/top/{$limit}/podcasts.json");
2023-07-02 22:20:18 +00:00
/** @var array $json */
$json = json_decode((string) $response->getBody(), flags: JSON_THROW_ON_ERROR);
2023-07-02 22:12:40 +00:00
return new JSONResponse($json, $response->getStatusCode());
} catch (Exception $e) {
2023-07-02 22:20:18 +00:00
return new JSONResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
2023-07-02 22:12:40 +00:00
}
}
}