Skip to content

Commit

Permalink
Don't make assumptions on how versioning should work
Browse files Browse the repository at this point in the history
Api-Method versioning should completly get handled in userspace. The
library itself should be version-agnostic
  • Loading branch information
usox committed Nov 5, 2021
1 parent 16b90e3 commit f58a521
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ to the json schema validator and work with the data right away.
Every method needs a corresponding schema-file which describes, how the request data should like alike.
You can find a simple example in the `example/schema`-folder.

Every request has also to follow a basic schema (see `dist/request.json`) which contains informations about the method and the methods version.
Every request has also to follow a basic schema (see `dist/request.json`) which contains informations about the method.

## Requirements

Expand Down Expand Up @@ -99,5 +99,5 @@ Just cd to the example-folder and fire up the the php internal webserver `php -S
Now you can send `POST`-Requests to the api like this curl-request.

```shell script
curl -X POST -d '{"method": "beerlist", "version": null, "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888"
curl -X POST -d '{"method": "beerlist", "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888"
```
11 changes: 5 additions & 6 deletions dist/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
"type": "object",
"properties": {
"method": {
"type": "string"
"type": "string",
"description": "The method name"
},
"parameter": {
"type": "object"
},
"version": {
"type": ["integer", "null"]
"type": "object",
"description": "The methods parameter"
}
}
}
},
"required": ["method", "parameter", "version"]
"required": ["method", "parameter"]
}
4 changes: 1 addition & 3 deletions src/Contract/MethodProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace Usox\JsonSchemaApi\Contract;

/**
* A method provider should perform a lookup an api method by its name.
* If api versions are used, the version number gets appended to the method name.
* e.g. someNamespace.someMethod, someNamespace.someMethod.1
* Lookup a method name and return the api method handler (e.g. by using a dict<methodName, methodHandler>
*
* If the method name does not exist, lookup is expected to return null.
*/
Expand Down
7 changes: 1 addition & 6 deletions src/Dispatch/MethodDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ public function dispatch(
ServerRequestInterface $request,
stdClass $input
): array {
// Get the method and version from the request
// Get the method from the request and perform lookup
$methodName = $input->method;
$version = $input->version;

if ($version !== null) {
$methodName = sprintf('%s.%d', $methodName, $version);
}

$handler = $this->methodProvider->lookup($methodName);
if (!$handler instanceof ApiMethodInterface) {
Expand Down
8 changes: 3 additions & 5 deletions tests/Dispatch/MethodDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public function testDispatchThrowsExceptionIfMethodDoesNotExist(): void
$this->expectExceptionCode(StatusCode::BAD_REQUEST);

$method = 'some-method';
$version = null;

$input = ['method' => $method, 'version' => $version];
$input = ['method' => $method];

$this->methodProvider->shouldReceive('lookup')
->with($method)
Expand All @@ -66,13 +65,12 @@ public function testDispatchThrowsExceptionIfMethodDoesNotExist(): void
public function testDispatchReturnsHandler(): void
{
$method = 'some-method';
$version = 666;
$result = ['some-result'];
$parameter = (object) ['some-parameter'];
$schemaContent = (object) ['some' => 'schema-content'];
$schemaFilePath = 'some-path';

$input = (object) ['method' => $method, 'version' => $version, 'parameter' => $parameter];
$input = (object) ['method' => $method, 'parameter' => $parameter];

$request = Mockery::mock(ServerRequestInterface::class);
$handler = Mockery::mock(ApiMethodInterface::class);
Expand All @@ -88,7 +86,7 @@ public function testDispatchReturnsHandler(): void
->andReturn($schemaContent);

$this->methodProvider->shouldReceive('lookup')
->with(sprintf('%s.%d', $method, $version))
->with($method)
->once()
->andReturn($handler);

Expand Down

0 comments on commit f58a521

Please sign in to comment.