2016-12-11 21:21:59 +00:00
```yaml
module: api
version: 2.2
requires:
2017-05-12 12:15:39 +00:00
- http: 1.0
2016-12-11 21:21:59 +00:00
- error: 2.0
```
Links
====
2017-09-10 16:37:11 +00:00
[**I.** Overview ](#I.-overview )
- [**1** Introduction & features ](#1-introduction--features )
- [**2** Basic knowledge ](#2-basic-knowledge )
[**II.** Usage ](#2---usage )
- [**1** Setup ](#1-setup )
- [**2** Php requirements ](#2-php-requirements )
- [**3** From php internally ](#3-from-php-internally )
- [**4** From HTTP requests ](#4-from-http-requests )
[**III.** Configuration ](#3---configuration )
- [**1** Basic usage ](#1-basic-usage )
- [**2** Advanced usage ](#2-advanced-usage )
[**IV.** Implementation ](#4---implementation )
- [**1** Permissions : AuthSystem ](#1-permissions--authsystem )
- [**2** Modules & methods ](#2-modules--methods )
- [**3** Automatic type check ](#3-automatic-type-check )
[**V.** Class documentation ](#5---class-documentation )
- [**1** Request ](#1-request )
- [**2** Response ](#2-response )
- [**3** AuthSystem ](#4-authsystem )
- [**4** Checker ](#4-checker )
- [**5** ModuleFactory ](#4-modulefactory )
2016-12-11 21:21:59 +00:00
2017-09-10 16:37:11 +00:00
> # **I.** Overview
> ## **1** Introduction & features
2017-05-12 12:15:39 +00:00
The `api` package (v2.2) allows you to easily create and manage an API. It could be used for an HTTP API (REST, or other kind), or you can use it as an internal core for your system.
2016-12-11 21:21:59 +00:00
2017-05-12 12:15:39 +00:00
The aim of this package is to make your life easier working with APIs or internal delegation. The only things you have to do is to implement your processes and edit the configuration, the package will do the rest.
2016-12-11 21:21:59 +00:00
Things you have to do :
- implement your processes (obviously)
2016-12-12 00:16:06 +00:00
- implement your authentication system (cf. [AuthSystem ](#3-authsysten ))
- edit the configuration file (cf. [configuration ](#3---configuration ))
2016-12-11 21:21:59 +00:00
Things you **don't have** to do :
2016-12-12 00:16:06 +00:00
- input type check (cf. [Checker ](#4-checker ))
2016-12-11 21:21:59 +00:00
- API multiple permission management
- optional or required inputs
- before and after scripts
2016-12-12 13:13:07 +00:00
- catch both in-URL and `multipart/form-data` input
2016-12-11 21:21:59 +00:00
2017-09-10 16:37:11 +00:00
> ## **2** Basic knowledge
2016-12-11 21:21:59 +00:00
The API is based over a 2-level delegation structure :
1. `module` which is a set of methods
2. `method` which have input, output, permissions, and is bound to a function
2017-05-12 12:15:39 +00:00
So each of your functionalities must have a `method` name and be inside a `module` .
2016-12-11 11:05:48 +00:00
2017-05-12 12:15:39 +00:00
< u > Example:< / u >
2017-09-07 18:17:30 +00:00
* the module `article` contains methods:
* `read` with argument `article_id` (to identify the wanted article)
* `write` with arguments `title` and `body` (data to write into the new created article)
* `edit` with arguments `article_id` and `body` (to identify and replace the body)
* `delete` with argument `article_id` (to identify which article to delete)
2017-05-12 12:15:39 +00:00
2017-09-07 18:17:30 +00:00
If you want to delete the article of id `52` , you must request `article/delete` passing `article_id` =`52`.
2016-12-11 11:05:48 +00:00
2017-09-10 16:37:11 +00:00
[2] - Usage
====
> ## **1** Setup
2016-12-11 21:21:59 +00:00
In order to make the API work, you have to :
2016-12-12 00:16:06 +00:00
1. Edit the configuration file according to your needs (cf. [configuration ](#3---configuration ))
2. Implement the Authentication System to manage permissions (cf. [AuthSystem ](#3-authsystem ))
2017-05-12 12:15:39 +00:00
3. Implement the code of the methods according to the configuration
2016-12-11 11:05:48 +00:00
2017-09-10 16:37:11 +00:00
> ## **2** Php requirements
2016-12-11 11:05:48 +00:00
2017-05-12 12:15:39 +00:00
> ### 1) include the `autoloader` file
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
< ?php require_once '../autoloader.php';
```
2016-12-11 21:21:59 +00:00
2017-05-12 12:15:39 +00:00
> ### 2) load useful classes
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
// for API use
use \api\core\Request;
use \api\core\Response;
2016-12-11 21:21:59 +00:00
2017-05-12 12:15:39 +00:00
// for error handling
use \error\core\Err;
2016-12-11 21:21:59 +00:00
```
2016-12-11 11:05:48 +00:00
2016-12-12 13:13:07 +00:00
2017-09-10 16:37:11 +00:00
> ## **3** From php internally
2016-12-12 13:13:07 +00:00
2017-05-12 12:15:39 +00:00
> ### 1) create a request
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
// creates a request for the module {module} and its method {method} with params
$request = new Request('{module}/{method}', [
'param1' => 10,
'param2' => 'somevalue'
]);
2016-12-11 21:21:59 +00:00
```
2017-05-12 12:15:39 +00:00
> ### 2) catch possible errors (optional)
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
// if error is not Err::Success
if( $request->error->get() !== Err::Success )
'do something';
2016-12-11 21:21:59 +00:00
```
2017-05-12 12:15:39 +00:00
> ### 3) execute the request and catch response
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
$response = $request->dispatch();
2016-12-11 21:21:59 +00:00
```
2017-05-12 12:15:39 +00:00
> ### 4) catch response errors (optional)
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
// if error is not Err::Success
if( $response->error->get() !== Err::Success )
'do something';
2016-12-11 21:21:59 +00:00
```
2017-05-12 12:15:39 +00:00
> ### 5) catch response output
2016-12-12 13:13:07 +00:00
2016-12-11 21:21:59 +00:00
```php
2017-05-12 12:15:39 +00:00
// fetch all outputs
$output = $response->getAll();
2016-12-11 21:21:59 +00:00
2017-05-12 12:15:39 +00:00
// fetch specific output
$specific = $response->get('someOutputName');
2016-12-11 21:21:59 +00:00
```
2017-09-10 16:37:11 +00:00
> ## **4** From HTTP requests
2016-12-12 13:13:07 +00:00
In order to setup an automatic bound from HTTP requests to API directly, you must use a **router** .
2017-05-12 12:15:39 +00:00
> ### 1) Format url so it must begin with `/{module}/{method}`
2016-12-12 13:13:07 +00:00
```php
2017-05-12 12:15:39 +00:00
// let's suppose the url is `/api/{module}/{method}`
$url = '/api/somemodule/somemethod/1/2/';
2017-09-07 18:17:30 +00:00
$uri = substr($url, strlen('/api'));
// $uri = /somemodule/somemethod/1/2/
2016-12-12 13:13:07 +00:00
```
2017-05-12 12:15:39 +00:00
> ### 2) give the url to the HTTP manager
2016-12-12 13:13:07 +00:00
```php
2017-05-12 12:15:39 +00:00
// create request from HTTP data
$request = Request::remote($url);
2016-12-12 13:13:07 +00:00
2017-05-12 12:15:39 +00:00
// execute request and catch response
// note that request errors will propagate through response
$response = $request->dispatch();
2016-12-12 13:13:07 +00:00
2017-05-12 12:15:39 +00:00
// return response as HTTP body
die( $response->serialize() );
```
2016-12-12 13:16:58 +00:00
2016-12-12 13:13:07 +00:00
2017-05-12 12:15:39 +00:00
Then can handle various kinds of URL :
2016-12-11 14:22:39 +00:00
2017-05-12 12:15:39 +00:00
- request and parameters can be in URL (separated by `/` )
- request and parameters can be in `multipart/form-data` or `x-www-form-urlencoded`
- request and parameters of both URL, post data, and form-data are caught
2016-12-11 14:22:39 +00:00
2017-05-12 12:15:39 +00:00
### The following examples can work :
> 1. `http://www.host.com/{module}/{method}/`
```json
"post-data": {
"param1": "{value1}",
"param2": "{value2}"
}
```
> 2. `http://www.host.com/{module}/{method}/{param1}/{param2}`
```json
"post-data": {}
```
> 3. `http://www.host.com/apiOrParentUrl/`
```json
"post-data": {
"module": "{module}",
"method": "{method}",
"param1": "{value1}",
"param2": "{value2}"
}
```
> 4. `http://www.host.com/apiOrParentUrl/{value1}/{value2}`
```json
"post-data": {
"module": "{module}",
"method": "{method}",
}
```
2016-12-11 14:22:39 +00:00
2016-12-12 00:16:06 +00:00
2017-09-10 16:37:11 +00:00
[3] - Configuration
====
2016-12-11 14:22:39 +00:00
```json
{
"{module_name}": {
"{http_method}::{method_name}": {
"description": "{method_description}",
"permissions": ["{method_perm}"],
"options": { "download": "{is_downloadable}" },
"parameters": {
"{name_param}": { "description": "{desc_param}", "type": "{type_param}", "optional": "{is_optional}" }
},
"output": {
"{name_output}": { "description": "{desc_output}", "type": "{type_output}" }
}
}
}
}
```
|variable|description|exemple|
|-------|-------|------|
|`{module_name}`|alphanumeric module name|"publications"|
|`{http_method}`|uppercase HTTP method|"POST"|
|`{method_name}`|alphanumeric method name|"article"|
|`{method_description}`|textual description|"Returns a specific article"|
|`{method_perm}`|permission array|`["poster", "admin", "user"]`|
|`{is_downloadable}`|If you want this method to return a file|`true`, `false` |
2017-09-07 18:17:30 +00:00
|`{name_param}`|Your param's name _*_ |"id_article"|
2016-12-11 14:22:39 +00:00
|`{desc_param}`|Your param's description|"Wanted article's id"|
|`{type_param}`|Your param's type (cf. Checker)|"Wanted article's type"|
|`{is_optional}`|Whether to make your param _required_ |`true`, `false` |
|`{name_output}`|Your output's name|"article"|
|`{desc_output}`|Your output's description|"Article content"|
2017-09-07 18:17:30 +00:00
_*_ If you want URL (GET) parameters, the {param_name} must be `URL_0` , `URL_1` and so on according to the index wanted in the URL.
> `api/module/method/URL_0/URL_1/URL_2/`
2016-12-11 14:22:39 +00:00
2017-05-12 12:15:39 +00:00
2017-09-10 16:37:11 +00:00
[4] - Implementation
====
> ## **1** Permissions : AuthSystem
2017-05-12 12:15:39 +00:00
In order to implement your _Authentification System_ you have to implement the **interface** `AuthSystem` located in `/build/api/core/AuthSystem` .
You must register your custom authentification system before each api call with :
```php
// let's suppose your auth system class is "AuthSystemDefault"
\api\core\Request::setAuthSystem(new AuthSystemDefault);
```
2017-09-10 16:37:11 +00:00
> ## **2** Modules & methods
2017-05-12 12:15:39 +00:00
### Module implementation
Each module's implementation is represented as a **file** so as a **class** located in `/build/api/module/` . In order for the autoloader to work, you must name the **file** the same name as the **class** .
### Method implementation
Each method is represented as a method in its module's class.
### Input arguments
Arguments are passed to the method as a single argument which an associative array according to the documentation.
_Notes_:
* Optional parameters if not given are set to `null`
* parameters of type `FILE` are given by reference but the use is the same as normal parameters
2017-09-07 18:17:30 +00:00
* URL parameters are called `URL_0` , `URL_1` and so on according to their order.
2017-05-12 12:15:39 +00:00
### Ouput required
2017-09-07 18:17:30 +00:00
You must return an associative array containing at least the field `error` containing an instance of `/api/core/Error` , then you can add whatever you want to return in the array.
2016-12-11 17:41:37 +00:00
2017-05-12 12:15:39 +00:00
If you don't return the 'error' field, by default it is set to `new Error(Err::Success)` .
2016-12-12 00:16:06 +00:00
2017-09-10 16:37:11 +00:00
[5] - Class documentation
====
> ## **4** Checker
2016-12-12 00:16:06 +00:00
`Checker` checks the input values according to the type given in the configuration.
The default types below are available in the default package.
To add a new type, just open the file `/build/api/Checker.php` and add an entry in the `switch` statement.
2017-05-12 12:15:39 +00:00
### Default types
2016-12-12 00:16:06 +00:00
|Type|Example|Description|
|---|---|---|
|`mixed`|`[9,"a"]`, `"a"` |Any content (can be simple or complex)|
|`id`|`10`, `"23"` |Positive integer number between `0` and `2147483647` |
|`numeric`|`-10.2`, `"23"` |Any number, `null` and the string `"null"` |
|`text`|`"Hello!"`|String that can be of any length (even empty)|
|`hash`|`"4612473aa81f93a878674f9ebffa8d63a1b51ea28dcdcdb1e89eb512aae9b77e"`|String with a length of 40 or 64, containing only hexadecimal characters|
|`alphanumeric`|`"abc029.-sd9"`|String containing only alphanumeric, ___, _-_ , and _._ characters|
|`letters`|`"abc -sd"`|String containing only letters, _-_ , and space characters|
|`mail`|`"a.b@c.def"`|Valid email address|
|`number`|`0102030405`|Phone number, following formats allowed : `06` , `+336` , `+33 6` |
|`array`|`[1, 3]`|Non-empty array|
|`object`|_works only within php_|Non-empty object|
|`boolean`|`true`, `false` |Boolean|
|`varchar(a,b)`|`"Hello!"`|String with a length between `a` and `b` (included)|
|`varchar(a,b,c)`|`"abc"`|String with a length between `a` and `b` (included) and matching the `c` type|
2017-05-12 12:15:39 +00:00
### Complex type : chainable array
2016-12-12 00:16:06 +00:00
|Type|Sub-Type|Description|
|---|---|---|
|`array< a > `|`a`|Array containing only entries matching the type `a` |
2017-05-12 12:15:39 +00:00
> **Note:** It is possible to chain `array` type as many as needed.
2016-12-12 00:16:06 +00:00
**Ex.:** `array<array<id>>` - Will match array only containing arrays that only contains `id` entries.
2017-09-07 18:17:30 +00:00
2017-09-10 16:37:11 +00:00
> ## **5** Advanced
2017-09-07 18:17:30 +00:00
### Before and After scripts
Each time a **method** is called, the api **creates an instance** from the class, and after the execution, the class is **destroyed** . So you can implement the methods `__construct` and `__destruct` to add before and after scripts.