|
| 1 | +# APIs development guidelines |
| 2 | + |
| 3 | +# Concept |
| 4 | + |
| 5 | +REST APIs and models (defined as openAPI- or JSON-schemas) are defined in a central location (/apis) to allow for "design-first" development. |
| 6 | + |
| 7 | +# Development guidelines |
| 8 | + |
| 9 | +## Standards |
| 10 | + |
| 11 | +The oSparc platform uses the following standards: |
| 12 | +- REST API: [Open API v3](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md) |
| 13 | +- Models and configuration [JSON Schema](https://json-schema.org/) |
| 14 | + |
| 15 | +## Folder structure |
| 16 | + |
| 17 | +```bash |
| 18 | +/apis/ # base folder |
| 19 | +/apis/director/ # service name folder (e.g. for director service) |
| 20 | +/apis/director/v0/ # service version (v0 for development, then v1, v2... only major) |
| 21 | +/apis/director/v0/openapi.yaml # openapi specifications in YAML |
| 22 | +/apis/director/v0/schemas/ # schemas only used by the director API |
| 23 | +/apis/director/v0/schemas/services.yaml # openapi encoded service only schema |
| 24 | + |
| 25 | +/apis/shared/ # shared apis/schemas base folder |
| 26 | +/apis/shared/schemas/ # sub folder for schemas |
| 27 | +/apis/shared/schemas/health_check.yaml # openapi encoded shared schema |
| 28 | +/apis/shared/schemas/node-meta.json # jsonschema encoded shared schema |
| 29 | +/apis/shared/schemas/v1/error.yaml # openapi encoded shaared schema for version 1 |
| 30 | +/apis/shared/schemas/v2/error.yaml # openapi encoded shaared schema for version 2 |
| 31 | + |
| 32 | +/tests/ # python tests folder to check schemas validity |
| 33 | +/tests/requirements.txt # requirements for python tests |
| 34 | +``` |
| 35 | + |
| 36 | +## Organization |
| 37 | + |
| 38 | +### Openapi specifications file |
| 39 | + |
| 40 | +Each API is defined within a file __openapi.yaml__ in YAML format. The file shall be located in the a subfolder named after the service/package and the major version subfolder. |
| 41 | + |
| 42 | +#### Version subfolder |
| 43 | + |
| 44 | +For initial development, the version shall be 0.1.0 and the subfolder v0 |
| 45 | +For release, the version shall start from 1.0.0 and subfolder v1. |
| 46 | +The subolder digit corresponds to the version major digits. It shall be modified only when changes are not backwards compatible (e.g. changing a return value, removing parameters or resource, ...). |
| 47 | + |
| 48 | +#### Schemas in separate files |
| 49 | + |
| 50 | +Schemas shall always be defined in separate files. |
| 51 | + |
| 52 | +Schemas format shall be either OpenAPI v3 or JSON schema Draft#7. |
| 53 | + |
| 54 | +If these schemas are pertinent only to the current API they shall be contained together with the openapi specifications file inside a __schemas__ subfolder. |
| 55 | +If these schemas are shared with other APIs they should be located in the __/shared/schemas__ subfolder. |
| 56 | + |
| 57 | +#### Versioning shared schemas |
| 58 | + |
| 59 | +NOTE: If shared schemas need backward incompatible changes, then a new major version of this specific shared schema is necessary and all APIs that rely on this specific schema will need to be upgraded. |
| 60 | +In that case, a version subfolder shall be added in the __/shared/__ subfolder and the relevant schemas shall be moved there. |
| 61 | + |
| 62 | +### Schemas defined with JSONSchema format that are used together with OpenAPI |
| 63 | + |
| 64 | +Mixing JSONSchema with OpenAPI schema needs some additional steps: |
| 65 | + |
| 66 | +1. Define the JSONSchema schema. |
| 67 | +2. Convert the JSONSchema formatted file to OpenAPI formatted file using the [converter](../scripts/jsonschema/openapi_converter). |
| 68 | +3. In the openapi specifications file ref the converted OpenAPI schema file. |
| 69 | + |
| 70 | +## Using the openAPI |
| 71 | + |
| 72 | +### Python: Current status (using aiohttp-apiset) |
| 73 | + |
| 74 | +The current python-based packages use the aiohttp-apiset library to create routes from the defined API. The aiohttp-apiset library requires a physical file to create the routes. Therefore one needs to generate that file by following: |
| 75 | + |
| 76 | +1. Generate a 1 file OpenAPI formatted file using [prance](https://pypi.org/project/prance/). By using [openapi-resolver](../scripts/openapi/oas_resolver). |
| 77 | +2. Copy the generated file in a folder in the python-based code and use it. |
| 78 | + |
| 79 | +### Python: in development and should be available soon |
| 80 | + |
| 81 | +Using the library [openapi-core](https://github.com/p1c2u/openapi-core) the library is able to download the api at starting point. |
| 82 | +The [apihub service](../services/apihub) serves the apis and schemas to the internal parts of the oSparc platform. |
| 83 | + |
| 84 | +## references |
| 85 | + |
| 86 | +- [defining reusable components - good practices](https://dev.to/mikeralphson/defining-reusable-components-with-the-openapi-specification-4077) |
| 87 | +- [official guidelines on OAS re-usability](https://github.com/OAI/OpenAPI-Specification/blob/master/guidelines/v2.0/REUSE.md) |
0 commit comments