Extending the API#
With API version 2.0, we introduced a new way to extend endpoints. The new endpoints are significantly easier to create and offer a variety of improvements, including:
- Endpoint versioning
- Built-in type safety
- Descriptions for endpoints and parameters
- Required and optional parameters
- Endpoints can be read via
system.endpoints.read.v2
Strict Processing#
The endpoints of this new structure are significantly stricter than previous endpoints of the old structure. Specifically, this affects the provided parameters:
- Provided parameters must match the defined type
- Parameter validation (if present) must pass successfully
- Providing parameters that are not defined in the endpoint definition will trigger an error
Required Interfaces#
To create your own endpoint in the new structure, you need to create a PHP class that implements the interface \idoit\Api\EndpointInterface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Subsequently, this endpoint must be registered in the API. There are two options for this:
Registration via registerEndpoint#
Within your init.php, you can use the following code to register endpoints:
1 2 3 4 5 6 7 8 9 10 | |
Registration via Tagged Service#
If you define your own services in your add-on that are added to the Dependency Injection Container, you can have your endpoints automatically registered by using the tag 'api.endpoint':
1 2 3 4 5 6 7 8 | |
Structure of an Endpoint#
An API endpoint consists, based on the EndpointInterface interface, of two methods:
1 2 | |
Endpoint Definition#
The endpoint definition contains necessary information such as the name of the endpoint, a description, and available parameters. Optionally, examples for requests and responses can also be added.
Endpoint Request#
This method is executed when the corresponding API endpoint is called. The parameters are located in $request->request and can be read using $request->request->get('parameter-name').
The request method must return an instance of the JsonRpcResponse class.
Parameters#
Parameters are part of the endpoint definition and exist in two variants: RequiredParameter and OptionalParameter. Required parameters MUST be provided. Both parameter types have a name, a type (string, integer, array, boolean), a description, and a validation callback (PHP callback function). Optional parameters can define a default value for the case that they are not explicitly provided.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Parameter validation must be a callable and return a boolean value (true or false). A validation can also throw exceptions.
Legacy Logic (pre API 2.0)#
To extend the API using the legacy approach, your add-on merely needs one (or more) PHP classes with the prefix "isys_api_model_". For example, place the file isys_api_model_example.class.php in your add-on directory and register it in the legacy autoloader -- this is already enough to make the class accessible via the API.
For the i-doit API to work correctly with the class, the following template should be used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
If you now call the API with the following request:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
The following response is sent back to the client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Using this information, we also know what values to expect in the PHP context and how to react to them.
The "method" and "option" values from "method" are typically used for internal routing -- using the example of the method "cmdb.object.read":
The class isys_api_model_cmdb is initialized and within the "route" method, the "object" parameter is handled. An instance of isys_api_model_cmdb_object is created and the read method is called. In code, this might look roughly as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
See also#
- Developing Add-ons -- Guide to add-on development
- Software Development -- Overview of developer documentation
- API Add-on -- Interface for external access