Added versioning to API endpoints, allowing not to pass the version which will default to v1

This commit is contained in:
Alejandro Celaya 2016-10-22 18:46:53 +02:00
parent 850ce152cd
commit 42f86a4a24
5 changed files with 73 additions and 11 deletions

View File

@ -14,7 +14,7 @@
],
"paths": {
"/authenticate": {
"/v1/authenticate": {
"post": {
"description": "Performs an authentication",
"parameters": [
@ -60,7 +60,7 @@
}
}
},
"/short-codes": {
"/v1/short-codes": {
"get": {
"description": "Returns the list of short codes",
"parameters": [
@ -185,7 +185,7 @@
}
}
},
"/short-codes/{shortCode}": {
"/v1/short-codes/{shortCode}": {
"get": {
"description": "Get the long URL behind a short code.",
"parameters": [
@ -234,7 +234,7 @@
}
}
},
"/short-codes/{shortCode}/visits": {
"/v1/short-codes/{shortCode}/visits": {
"get": {
"description": "Get the list of visits on provided short code.",
"parameters": [
@ -284,7 +284,7 @@
}
}
},
"/short-codes/{shortCode}/tags": {
"/v1/short-codes/{shortCode}/tags": {
"put": {
"description": "Edit the tags on provided short code.",
"parameters": [

View File

@ -22,6 +22,7 @@ return [
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\PathVersionMiddleware::class => InvokableFactory::class,
Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class,
],
],

View File

@ -4,6 +4,13 @@ use Shlinkio\Shlink\Rest\Middleware;
return [
'middleware_pipeline' => [
'pre-routing' => [
'middleware' => [
Middleware\PathVersionMiddleware::class,
],
'priority' => 11,
],
'rest' => [
'path' => '/rest',
'middleware' => [

View File

@ -6,37 +6,37 @@ return [
'routes' => [
[
'name' => 'rest-authenticate',
'path' => '/rest/authenticate',
'path' => '/rest/v{version:1}/authenticate',
'middleware' => Action\AuthenticateAction::class,
'allowed_methods' => ['POST', 'OPTIONS'],
],
[
'name' => 'rest-create-shortcode',
'path' => '/rest/short-codes',
'path' => '/rest/v{version:1}/short-codes',
'middleware' => Action\CreateShortcodeAction::class,
'allowed_methods' => ['POST', 'OPTIONS'],
],
[
'name' => 'rest-resolve-url',
'path' => '/rest/short-codes/{shortCode}',
'path' => '/rest/v{version:1}/short-codes/{shortCode}',
'middleware' => Action\ResolveUrlAction::class,
'allowed_methods' => ['GET', 'OPTIONS'],
],
[
'name' => 'rest-list-shortened-url',
'path' => '/rest/short-codes',
'path' => '/rest/v{version:1}/short-codes',
'middleware' => Action\ListShortcodesAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'rest-get-visits',
'path' => '/rest/short-codes/{shortCode}/visits',
'path' => '/rest/v{version:1}/short-codes/{shortCode}/visits',
'middleware' => Action\GetVisitsAction::class,
'allowed_methods' => ['GET', 'OPTIONS'],
],
[
'name' => 'rest-edit-tags',
'path' => '/rest/short-codes/{shortCode}/tags',
'path' => '/rest/v{version:1}/short-codes/{shortCode}/tags',
'middleware' => Action\EditTagsAction::class,
'allowed_methods' => ['PUT', 'OPTIONS'],
],

View File

@ -0,0 +1,54 @@
<?php
namespace Shlinkio\Shlink\Rest\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class PathVersionMiddleware implements MiddlewareInterface
{
/**
* 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)
{
$uri = $request->getUri();
$path = $uri->getPath();
// If the path does not begin with the version number, prepend v1 by default for retrocompatibility purposes
if (strpos($path, '/rest/v') !== 0) {
$parts = explode('/', $path);
// Remove the first empty part and the "/rest" prefix
array_shift($parts);
array_shift($parts);
// Prepend the prefix with version
array_unshift($parts, '/rest/v1');
$request = $request->withUri($uri->withPath(implode('/', $parts)));
}
return $out($request, $response);
}
}