feat: JSON API

This commit is contained in:
Pierre Rudloff 2018-03-20 12:02:21 +01:00
parent 618fb8416a
commit 74505cea57
4 changed files with 84 additions and 1 deletions

View file

@ -179,6 +179,11 @@ The library documentation is available on [alltube.surge.sh](https://alltube.sur
You can also have a look at this [example project](https://github.com/Rudloff/alltube-example-project).
## JSON API
We also provide a JSON API that you can use like this:
[/json?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ](https://alltubedownload.net/json?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ)
## FAQ
Please read the [FAQ](resources/FAQ.md) before reporting any issue.

View file

@ -548,6 +548,36 @@ class FrontController
}
}
/**
* Return the JSON object generated by youtube-dl.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
*
* @return Response HTTP response
*/
public function json(Request $request, Response $response)
{
$params = $request->getQueryParams();
$format = $this->getFormat($request);
if (isset($params['url'])) {
try {
return $response->withJson(
$this->download->getJSON(
$params['url'],
$format
)
);
} catch (Exception $e) {
return $response->withJson(['error' => $e->getMessage()])
->withStatus(500);
}
} else {
return $response->withJson(['error' => 'You need to provide the url parameter'])
->withStatus(400);
}
}
/**
* Generate the canonical URL of the current page.
*

View file

@ -51,6 +51,10 @@ $app->get(
'/redirect',
[$controller, 'redirect']
)->setName('redirect');
$app->get(
'/json',
[$controller, 'json']
)->setName('json');
$app->get(
'/locale/{locale}',
[$controller, 'locale']

View file

@ -147,7 +147,7 @@ class FrontControllerTest extends TestCase
}
/**
* Assert that calling controller function with these parameters returns an HTTP redirect.
* Assert that calling controller function with these parameters returns an HTTP 500 error.
*
* @param string $request Controller function to call
* @param array $params Query parameters
@ -160,6 +160,20 @@ class FrontControllerTest extends TestCase
$this->assertTrue($this->getRequestResult($request, $params, $config)->isServerError());
}
/**
* Assert that calling controller function with these parameters returns an HTTP 400 error.
*
* @param string $request Controller function to call
* @param array $params Query parameters
* @param Config $config Custom config
*
* @return void
*/
private function assertRequestIsClientError($request, array $params = [], Config $config = null)
{
$this->assertTrue($this->getRequestResult($request, $params, $config)->isClientError());
}
/**
* Test the constructor.
*
@ -539,6 +553,36 @@ class FrontControllerTest extends TestCase
);
}
/**
* Test the json() function without the URL parameter.
*
* @return void
*/
public function testJsonWithoutUrl()
{
$this->assertRequestIsClientError('json');
}
/**
* Test the json() function.
*
* @return void
*/
public function testJson()
{
$this->assertRequestIsOk('json', ['url' => 'https://www.youtube.com/watch?v=M7IpKCZ47pU']);
}
/**
* Test the json() function with an error.
*
* @return void
*/
public function testJsonWithError()
{
$this->assertRequestIsServerError('json', ['url' => 'http://example.com/foo']);
}
/**
* Test the locale() function.
*