Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit ae806cd

Browse files
authored
Update README.md
1 parent a320eb8 commit ae806cd

File tree

1 file changed

+1
-126
lines changed

1 file changed

+1
-126
lines changed

README.md

+1-126
Original file line numberDiff line numberDiff line change
@@ -1,126 +1 @@
1-
[![Unittests](https://github.com/usox/json-schema-api/actions/workflows/php.yml/badge.svg)](https://github.com/usox/json-schema-api/actions/workflows/php.yml)
2-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/usox/json-schema-api/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/usox/json-schema-api/?branch=master)
3-
[![Code Coverage](https://scrutinizer-ci.com/g/usox/json-schema-api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/usox/json-schema-api/?branch=master)
4-
5-
# JsonSchemaApi
6-
7-
This library provides a simple way to create a json api using [json-schema](http://json-schema.org/) to validate the request.
8-
You can leverage most of the input validation tasks (variables types, length/content-constraints, lists containing just certain items, etc.)
9-
to the json schema validator and work with the data right away.
10-
11-
## Json-Schema
12-
13-
Every method needs a corresponding schema-file which describes, how the request data should look alike.
14-
You can find a simple example in the `example/schema`-folder.
15-
16-
Every request has also to follow a basic schema (see `dist/request.json`) which contains informations about the method.
17-
18-
## Validation
19-
20-
Every request and the generated response will be validated against the provided schema.
21-
22-
## Requirements
23-
24-
This library requires existing psr7 and psr17 implementations.
25-
26-
## Install
27-
28-
```
29-
composer require usox/json-schema-api
30-
```
31-
32-
## Usage
33-
34-
Just use the factory method to create the endpoint. The factory automatically searches for a existing psr17 stream factory implementation,
35-
but you can also provide a factory instance when calling the method.
36-
37-
Get your method-provider ready (see below) and call the `factory`-method on the endpoint to retrieve a working instance:
38-
39-
The serve method requires a psr request and returns a psr response.
40-
```php
41-
$endpoint = \Usox\JsonSchemaApi\Endpoint::factory(
42-
$methodProvider
43-
);
44-
45-
$endpoint->serve(
46-
$psr7Request,
47-
$psr7Response
48-
);
49-
```
50-
51-
#### Optional: PSR15 Middleware
52-
53-
The endpoint class implements the [PSR15](https://www.php-fig.org/psr/psr-15/) `RequestHandlerInterface`.
54-
55-
### MethodProvider
56-
57-
The `MethodProvider` is the source for your api methods - this could be a simple array, a DI-Container, etc. The class has to implement `Usox\JsonSchemApi\Contract\MethodProviderInterface`.
58-
59-
```php
60-
class MyMethodProvider implements \Usox\JsonSchemaApi\Contract\MethodProviderInterface
61-
{
62-
private array $methodList = ['beerlist' => BeerlistMethod::class];
63-
64-
public function lookup(string $methodName) : ?\Usox\JsonSchemaApi\Contract\ApiMethodInterface
65-
{
66-
$handler = $this->methodList[$methodName] ?? null;
67-
if ($handler === null) {
68-
return null;
69-
}
70-
return new $handler;
71-
}
72-
}
73-
```
74-
75-
### API-Method
76-
77-
The `lookup`-method in the MethodProvider must return an instance of `Usox\JsonSchemaApi\Contract\ApiMethodInterface`.
78-
79-
Every api method handler must define at least those two methods:
80-
- The `handle` method which processes the request and returns the result
81-
- The `getSchemaFile` method which returns the path to the schema file which is used to validate the request
82-
83-
```php
84-
class BeerlistMethod implements \Usox\JsonSchemaApi\Contract\ApiMethodInterface
85-
{
86-
public function handle(stdClass $parameter) : array
87-
{
88-
return ['ipa', 'lager', 'weizen'];
89-
}
90-
91-
public function getSchemaFile() : string
92-
{
93-
return '/path/to/schema.json';
94-
}
95-
}
96-
```
97-
98-
## Example
99-
100-
You can find a working example in the `example`-folder.
101-
102-
Just cd to the example-folder and fire up the the php internal webserver `php -S localhost:8888`.
103-
Now you can send `POST`-Requests to the api like this using curl.
104-
105-
```shell script
106-
curl -X POST -d '{"method": "beerlist", "parameter": {"test1": "foobar", "test2": 666}}' "http://localhost:8888"
107-
```
108-
109-
## Error-Handling
110-
111-
Basically there are three types of errors. All of them get logged.
112-
113-
### ApiExceptions
114-
If a handler throws an exception which extends the `ApiException` exception class, the api will
115-
return a `Bad Request (400)` response including an error message (the exception message) and an error code for reference
116-
within a json response.
117-
118-
### InternalException
119-
Internal errors, like non-existing schema files, invalid schemas and such, will return a `Internal Server Error (500)`
120-
response and create a log entry (if a logger is provided).
121-
122-
In addition, optionally available context information within the exception will be logged, too.
123-
124-
### Throwable
125-
126-
Any Throwable which are thrown within an api handler, will be catched, logged and return a `Internal Server Error (500)` response.
1+
Moved to Codeberg: https://codeberg.org/usox/json-schema-api

0 commit comments

Comments
 (0)