mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-16 23:39:54 +03:00
Created shortcode creation rest endpoint
This commit is contained in:
parent
8a7d5a499e
commit
1fbefbbd15
4 changed files with 135 additions and 2 deletions
|
@ -11,7 +11,7 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^5.5 || ^7.0",
|
||||
"php": "^5.6 || ^7.0",
|
||||
"zendframework/zend-expressive": "^1.0",
|
||||
"zendframework/zend-expressive-helpers": "^2.0",
|
||||
"zendframework/zend-expressive-fastroute": "^1.1",
|
||||
|
@ -24,7 +24,7 @@
|
|||
"symfony/console": "^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^4.8",
|
||||
"phpunit/phpunit": "^5.0",
|
||||
"squizlabs/php_codesniffer": "^2.3",
|
||||
"roave/security-advisories": "dev-master",
|
||||
"filp/whoops": "^2.0",
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
use Acelaya\UrlShortener\Middleware\Routable;
|
||||
use Acelaya\UrlShortener\Middleware\Rest;
|
||||
|
||||
return [
|
||||
|
||||
|
@ -10,6 +11,14 @@ return [
|
|||
'middleware' => Routable\RedirectMiddleware::class,
|
||||
'allowed_methods' => ['GET'],
|
||||
],
|
||||
|
||||
// Rest
|
||||
[
|
||||
'name' => 'rest-create-shortcode',
|
||||
'path' => '/rest/short-code',
|
||||
'middleware' => Rest\CreateShortcodeMiddleware::class,
|
||||
'allowed_methods' => ['POST'],
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
|
98
src/Middleware/Rest/CreateShortcodeMiddleware.php
Normal file
98
src/Middleware/Rest/CreateShortcodeMiddleware.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
namespace Acelaya\UrlShortener\Middleware\Rest;
|
||||
|
||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||
use Acelaya\UrlShortener\Service\UrlShortener;
|
||||
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
|
||||
use Acelaya\UrlShortener\Util\RestUtils;
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Diactoros\Uri;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class CreateShortcodeMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var UrlShortener|UrlShortenerInterface
|
||||
*/
|
||||
private $urlShortener;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $domainConfig;
|
||||
|
||||
/**
|
||||
* GenerateShortcodeMiddleware constructor.
|
||||
*
|
||||
* @param UrlShortenerInterface|UrlShortener $urlShortener
|
||||
* @param array $domainConfig
|
||||
*
|
||||
* @Inject({UrlShortener::class, "config.url_shortener.domain"})
|
||||
*/
|
||||
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
|
||||
{
|
||||
$this->urlShortener = $urlShortener;
|
||||
$this->domainConfig = $domainConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an incoming request and/or response.
|
||||
*
|
||||
* Accepts a server-side request and a response instance, and does
|
||||
* something with them.
|
||||
*
|
||||
* If the response is not complete and/or further processing would not
|
||||
* interfere with the work done in the middleware, or if the middleware
|
||||
* wants to delegate to another process, it can use the `$out` callable
|
||||
* if present.
|
||||
*
|
||||
* If the middleware does not return a value, execution of the current
|
||||
* request is considered complete, and the response instance provided will
|
||||
* be considered the response to return.
|
||||
*
|
||||
* Alternately, the middleware may return a response instance.
|
||||
*
|
||||
* Often, middleware will `return $out();`, with the assumption that a
|
||||
* later middleware will return a response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param null|callable $out
|
||||
* @return null|Response
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
$postData = $request->getParsedBody();
|
||||
if (! isset($postData['longUrl'])) {
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
|
||||
'message' => 'A URL was not provided',
|
||||
], 400);
|
||||
}
|
||||
$longUrl = $postData['longUrl'];
|
||||
|
||||
try {
|
||||
$shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl));
|
||||
$shortUrl = (new Uri())->withPath($shortcode)
|
||||
->withScheme($this->domainConfig['schema'])
|
||||
->withHost($this->domainConfig['hostname']);
|
||||
|
||||
return new JsonResponse([
|
||||
'longUrl' => $longUrl,
|
||||
'shortUrl' => $shortUrl->__toString(),
|
||||
]);
|
||||
} catch (InvalidUrlException $e) {
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::getRestErrorCodeFromException($e),
|
||||
'message' => sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl),
|
||||
], 400);
|
||||
} catch (\Exception $e) {
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::UNKNOWN_ERROR,
|
||||
'message' => sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
26
src/Util/RestUtils.php
Normal file
26
src/Util/RestUtils.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
namespace Acelaya\UrlShortener\Util;
|
||||
|
||||
use Acelaya\UrlShortener\Exception\ExceptionInterface;
|
||||
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||
|
||||
class RestUtils
|
||||
{
|
||||
const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
|
||||
const INVALID_URL_ERROR = 'INVALID_URL';
|
||||
const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMEN';
|
||||
const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
|
||||
|
||||
public static function getRestErrorCodeFromException(ExceptionInterface $e)
|
||||
{
|
||||
switch (true) {
|
||||
case $e instanceof InvalidShortCodeException:
|
||||
return self::INVALID_SHORTCODE_ERROR;
|
||||
case $e instanceof InvalidUrlException:
|
||||
return self::INVALID_URL_ERROR;
|
||||
default:
|
||||
return self::UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue