mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added versioning to API endpoints, allowing not to pass the version which will default to v1
This commit is contained in:
parent
850ce152cd
commit
42f86a4a24
docs
module/Rest
@ -14,7 +14,7 @@
|
|||||||
],
|
],
|
||||||
|
|
||||||
"paths": {
|
"paths": {
|
||||||
"/authenticate": {
|
"/v1/authenticate": {
|
||||||
"post": {
|
"post": {
|
||||||
"description": "Performs an authentication",
|
"description": "Performs an authentication",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
@ -60,7 +60,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/short-codes": {
|
"/v1/short-codes": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Returns the list of short codes",
|
"description": "Returns the list of short codes",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
@ -185,7 +185,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/short-codes/{shortCode}": {
|
"/v1/short-codes/{shortCode}": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Get the long URL behind a short code.",
|
"description": "Get the long URL behind a short code.",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
@ -234,7 +234,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/short-codes/{shortCode}/visits": {
|
"/v1/short-codes/{shortCode}/visits": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Get the list of visits on provided short code.",
|
"description": "Get the list of visits on provided short code.",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
@ -284,7 +284,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/short-codes/{shortCode}/tags": {
|
"/v1/short-codes/{shortCode}/tags": {
|
||||||
"put": {
|
"put": {
|
||||||
"description": "Edit the tags on provided short code.",
|
"description": "Edit the tags on provided short code.",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
|
@ -22,6 +22,7 @@ return [
|
|||||||
|
|
||||||
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
|
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
|
||||||
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
||||||
|
Middleware\PathVersionMiddleware::class => InvokableFactory::class,
|
||||||
Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class,
|
Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
@ -4,6 +4,13 @@ use Shlinkio\Shlink\Rest\Middleware;
|
|||||||
return [
|
return [
|
||||||
|
|
||||||
'middleware_pipeline' => [
|
'middleware_pipeline' => [
|
||||||
|
'pre-routing' => [
|
||||||
|
'middleware' => [
|
||||||
|
Middleware\PathVersionMiddleware::class,
|
||||||
|
],
|
||||||
|
'priority' => 11,
|
||||||
|
],
|
||||||
|
|
||||||
'rest' => [
|
'rest' => [
|
||||||
'path' => '/rest',
|
'path' => '/rest',
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
|
@ -6,37 +6,37 @@ return [
|
|||||||
'routes' => [
|
'routes' => [
|
||||||
[
|
[
|
||||||
'name' => 'rest-authenticate',
|
'name' => 'rest-authenticate',
|
||||||
'path' => '/rest/authenticate',
|
'path' => '/rest/v{version:1}/authenticate',
|
||||||
'middleware' => Action\AuthenticateAction::class,
|
'middleware' => Action\AuthenticateAction::class,
|
||||||
'allowed_methods' => ['POST', 'OPTIONS'],
|
'allowed_methods' => ['POST', 'OPTIONS'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'rest-create-shortcode',
|
'name' => 'rest-create-shortcode',
|
||||||
'path' => '/rest/short-codes',
|
'path' => '/rest/v{version:1}/short-codes',
|
||||||
'middleware' => Action\CreateShortcodeAction::class,
|
'middleware' => Action\CreateShortcodeAction::class,
|
||||||
'allowed_methods' => ['POST', 'OPTIONS'],
|
'allowed_methods' => ['POST', 'OPTIONS'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'rest-resolve-url',
|
'name' => 'rest-resolve-url',
|
||||||
'path' => '/rest/short-codes/{shortCode}',
|
'path' => '/rest/v{version:1}/short-codes/{shortCode}',
|
||||||
'middleware' => Action\ResolveUrlAction::class,
|
'middleware' => Action\ResolveUrlAction::class,
|
||||||
'allowed_methods' => ['GET', 'OPTIONS'],
|
'allowed_methods' => ['GET', 'OPTIONS'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'rest-list-shortened-url',
|
'name' => 'rest-list-shortened-url',
|
||||||
'path' => '/rest/short-codes',
|
'path' => '/rest/v{version:1}/short-codes',
|
||||||
'middleware' => Action\ListShortcodesAction::class,
|
'middleware' => Action\ListShortcodesAction::class,
|
||||||
'allowed_methods' => ['GET'],
|
'allowed_methods' => ['GET'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'rest-get-visits',
|
'name' => 'rest-get-visits',
|
||||||
'path' => '/rest/short-codes/{shortCode}/visits',
|
'path' => '/rest/v{version:1}/short-codes/{shortCode}/visits',
|
||||||
'middleware' => Action\GetVisitsAction::class,
|
'middleware' => Action\GetVisitsAction::class,
|
||||||
'allowed_methods' => ['GET', 'OPTIONS'],
|
'allowed_methods' => ['GET', 'OPTIONS'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'rest-edit-tags',
|
'name' => 'rest-edit-tags',
|
||||||
'path' => '/rest/short-codes/{shortCode}/tags',
|
'path' => '/rest/v{version:1}/short-codes/{shortCode}/tags',
|
||||||
'middleware' => Action\EditTagsAction::class,
|
'middleware' => Action\EditTagsAction::class,
|
||||||
'allowed_methods' => ['PUT', 'OPTIONS'],
|
'allowed_methods' => ['PUT', 'OPTIONS'],
|
||||||
],
|
],
|
||||||
|
54
module/Rest/src/Middleware/PathVersionMiddleware.php
Normal file
54
module/Rest/src/Middleware/PathVersionMiddleware.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user