Skip to content

Commit adf4e0c

Browse files
authored
Start consolidation of documentation (#391)
* Start making documentation * Use snippets extension to insert readme from other directories * Skip tests when changing only markdown files * Add a "Using the API" documentation page * Add more documentation on hosting * Expand "Hosting" documentation * Import existing resources * Fix some broken links * fix typo in default network name * Finish CURL section * Add alt text and use smaller images
1 parent 5eefa4d commit adf4e0c

19 files changed

+307
-7
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ repos:
3434
entry: pytest src/tests
3535
language: system
3636
pass_filenames: false
37-
always_run: true
37+
exclude: ".*.md"

alembic/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ docker build -f alembic/Dockerfile . -t aiod-migration
1515
With the sqlserver container running, you can migrate to the latest schema with:
1616

1717
```commandline
18-
docker run -v $(pwd)/alembic:/alembic:ro -v $(pwd)/src:/app -it --network aiod_default aiod-migration
18+
docker run -v $(pwd)/alembic:/alembic:ro -v $(pwd)/src:/app -it --network aiod-rest-api_default aiod-migration
1919
```
2020
Make sure that the specified `--network` is the docker network that has the `sqlserver` container.
2121
The alembic directory is mounted to ensure the latest migrations are available,

docs/Contributing.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Contributing

docs/Hosting.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Hosting the Metadata Catalogue
2+
This page has information on how to host your own metadata catalogue.
3+
If you plan to locally develop the REST API, please follow the installation procedure in ["Contributing"](../contributing) instead.
4+
5+
## Prerequisites
6+
The platform is tested on Linux, but should also work on Windows and MacOS.
7+
Additionally, it needs [Docker](https://docs.docker.com/get-docker/) and
8+
[Docker Compose](https://docs.docker.com/compose/install/) (version 2.21.0 or higher).
9+
10+
## Installation
11+
Starting the metadata catalogue is as simple as spinning up the docker containers through docker compose.
12+
This means that other than the prerequisites, no installation steps are necessary.
13+
However, we do need to fetch files the latest release of the repository:
14+
15+
=== "CLI (git)"
16+
```commandline
17+
git clone https://github.com/aiondemand/AIOD-rest-api.git
18+
```
19+
20+
=== "UI (browser)"
21+
22+
* Navigate to the project page [aiondemand/AIOD-rest-api](https://github.com/aiondemand/AIOD-rest-api).
23+
* Click the green `<> Code` button and download the `ZIP` file.
24+
* Find the downloaded file on disk, and extract the content.
25+
26+
## Starting the Metadata Catalogue
27+
From the root of the project directory (i.e., the directory with the `docker-compose.yaml` file), run:
28+
29+
=== "Shorthand"
30+
We provide the following script as a convenience.
31+
This is especially useful when running with a non-default or development configuration,
32+
more on that later.
33+
```commandline
34+
./scripts/up.sh
35+
```
36+
=== "Docker Compose"
37+
```commandline
38+
docker compose up -d
39+
```
40+
41+
This will start a number of services running within one docker network:
42+
43+
* Database: a [MySQL](https://dev.mysql.com) database that contains the metadata.
44+
* Keycloak: an authentication service, provides login functionality.
45+
* Metadata Catalogue REST API:
46+
* Elastic Search: indexes metadata catalogue data for faster keyword searches.
47+
* Logstash: Loads data into Elastic Search.
48+
* Deletion: Takes care of cleaning up deleted data.
49+
* nginx: Redirects network traffic within the docker network.
50+
* es_logstash_setup: Generates scripts for Logstash and creates Elastic Search indices.
51+
52+
[//]: # (TODO: Make list items link to dedicated pages.)
53+
These services are described in more detail in their dedicated pages.
54+
After the previous command was executed successfully, you can navigate to [localhost](http://localhost.com)
55+
and see the REST API documentation. This should look similar to the [api.aiod.eu](https://api.aiod.eu) page,
56+
but is connected to your local database and services.
57+
58+
### Starting Connector Services
59+
To start connector services that automatically index data from external platforms into the metadata catalogue,
60+
you must specify their docker-compose profiles (as defined in the `docker-compose.yaml` file).
61+
For example, you can use the following commands when starting the connectors for OpenML and Zenodo.
62+
63+
=== "Shorthand"
64+
```commandline
65+
./scripts/up.sh openml zenodo-datasets
66+
```
67+
=== "Docker Compose"
68+
```commandline
69+
docker compose --profile openml --profile zenodo-datasets up -d
70+
```
71+
72+
The full list of connector profiles are:
73+
74+
- openml: indexes datasets and models from OpenML.
75+
- zenodo-datasets: indexes datasets from Zenodo.
76+
- huggingface-datasets: indexes datasets from Hugging Face.
77+
- examples: fills the database with some example data. Do not use in production.
78+
79+
[//]: # (TODO: Link to docs or consolidate in dedicated page.)
80+
81+
## Configuration
82+
There are two main places to configure the metadata catalogue services:
83+
environment variables configured in `.env` files, and REST API configuration in a `.toml` file.
84+
The default files are `./.env` and `./src/config.default.toml` shown below.
85+
86+
If you want to use non-default values, we strongly encourage you not to overwrite the contents of these files.
87+
Instead, you can create `./override.env` and `./config.override.toml` files to override those files.
88+
When using the `./scripts/up.sh` script to launch your services, these overrides are automatically taken into account.
89+
90+
=== "`./src/config/default.toml`"
91+
```toml
92+
--8<-- "./src/config.default.toml"
93+
```
94+
95+
=== "`./.env`"
96+
```.env
97+
--8<-- ".env"
98+
```
99+
100+
Overwriting these files directly will likely complicate updating to newer releases due to merge conflicts.
101+
102+
## Updating to New Releases
103+
104+
[//]: # (TODO: Publish to docker hub and have the default docker-compose.yaml pull from docker hub instead.)
105+
106+
First, stop running services:
107+
```commandline
108+
./scripts/down.sh
109+
```
110+
Then get the new release:
111+
```commandline
112+
git fetch origin
113+
git checkout vX.Y.Z
114+
```
115+
A new release might come with a database migration.
116+
If that is the case, follow the instructions in ["Database Schema Migration"](#database-schema-migration) below.
117+
The database schema migration must be performed before resuming operations.
118+
119+
Then run the startup commands again (either `up.sh` or `docker compose`).
120+
121+
### Database Schema Migration
122+
123+
We use [Alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html#running-our-first-migration) to automate database schema migrations
124+
(e.g., adding a table, altering a column, and so on).
125+
Please refer to the Alembic documentation for more information.
126+
Commands below assume that the root directory of the project is your current working directory.
127+
128+
!!! warning
129+
130+
Database migrations may be irreversible. Always make sure there is a backup of the old database.
131+
132+
Build the database schema migration docker image with:
133+
```commandline
134+
docker build -f alembic/Dockerfile . -t aiod-migration
135+
```
136+
137+
With the sqlserver container running, you can migrate to the latest schema with
138+
139+
```commandline
140+
docker run -v $(pwd)/alembic:/alembic:ro -v $(pwd)/src:/app -it --network aiod-rest-api_default aiod-migration
141+
```
142+
143+
since the default entrypoint of the container specifies to upgrade the database to the latest schema.
144+
145+
Make sure that the specified `--network` is the docker network that has the `sqlserver` container.
146+
The alembic directory is mounted to ensure the latest migrations are available,
147+
the src directory is mounted so the migration scripts can use defined classes and variable from the project.
148+
149+
[//]: # (TODO: Write documentation for when some of the migrations are not applicable. E.g., when a database was created in a new release.)

README.md docs/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Checkin is strict - as it should be. On our development keycloak, any redirectio
209209
accepted, so that it works on local host or wherever you deploy. This should never be the case
210210
for a production instance.
211211

212-
See [authentication README](authentication/README.md) for more information.
212+
See [authentication README](developer/auth.md) for more information.
213213

214214
### Creating the Database
215215

@@ -243,14 +243,14 @@ start-up work (e.g., populating the database).
243243

244244
#### Database Structure
245245

246-
The Python classes that define the database tables are found in [src/database/model/](src/database/model/).
246+
The Python classes that define the database tables are found in [src/database/model/](../src/database/model/).
247247
The structure is based on the
248-
[metadata schema](https://docs.google.com/spreadsheets/d/1n2DdSmzyljvTFzQzTLMAmuo3IVNx8yposdPLItBta68/edit?usp=sharing).
248+
[metadata schema](https://github.com/aiondemand/metadata-schema).
249249

250250

251251
## Adding resources
252252

253-
See [src/README.md](src/README.md).
253+
See [src/README.md](developer/code.md).
254254

255255
## Backups and Restoration
256256

@@ -313,5 +313,5 @@ To create a new release,
313313
- Check which services currently work (before the update). It's a sanity check for if a service _doesn't_ work later.
314314
- Update the code on the server by checking out the release
315315
- Merge configurations as necessary
316-
- Make sure the latest database migrations are applied: see ["Schema Migrations"](alembic/readme.md#update-the-database)
316+
- Make sure the latest database migrations are applied: see ["Schema Migrations"](developer/migration.md#update-the-database)
317317
9. Notify everyone (e.g., in the API channel in Slack).

docs/Using.md

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Using the REST API
2+
3+
The REST API allows you to retrieve, update, or remove asset metadata in the metadata catalogue.
4+
The assets are indexed from many different platforms, such as educational resources from [AIDA](https://www.i-aida.org),
5+
datasets from [HuggingFace](https://huggingface.co), models from [OpenML](https://openml.org), and many more.
6+
7+
The REST API is available at [`https://api.aiod.eu`](https://api.aiod.eu) and documentation on endpoints
8+
is available on complementary [Swagger](https://api.aiod.eu/docs) and [ReDoc](https://api.aiod.eu/redoc) pages.
9+
10+
To use the REST API, simply make HTTP requests to the different endpoints.
11+
Generally, these are `GET` requests when retrieving data, `PUT` requests when modifying data, `POST` requests when adding data, and `DEL` requests when deleting data.
12+
Here are some examples on how to list datasets in different environments:
13+
14+
=== "Python (requests)"
15+
16+
This example uses the [`requests`](https://requests.readthedocs.io/en/latest/) library to list datasets.
17+
18+
``` python
19+
import requests
20+
response = requests.get("https://api.aiod.eu/datasets/v1?schema=aiod&offset=0&limit=10")
21+
print(response.json())
22+
```
23+
24+
=== "CLI (curl)"
25+
26+
This example uses [curl](https://curl.se/) to retrieve data from the command line.
27+
28+
``` commandline
29+
curl -X 'GET' \
30+
'https://api.aiod.eu/datasets/v1?schema=aiod&offset=0&limit=10' \
31+
-H 'accept: application/json'
32+
```
33+
34+
Additionally, we also provide an [`aiondemand` package](https://aiondemand.github.io/aiondemand/) for Python
35+
to simplify access to the REST API. You can see an example of that below, and we refer to their dedicated
36+
documentation pages for full installation and usage instructions.
37+
38+
```python
39+
import aiod
40+
aiod.datasets.get_list()
41+
```
42+
43+
44+
## Exploring REST API Endpoints
45+
By navigating to the [Swagger documentation](https://api.aiod.eu/docs), you can find information and examples on how to access the different endpoints.
46+
47+
### Retrieving Information
48+
For example, if we navigate to the [`GET /datasets/v1`](https://api.aiod.eu/docs#/datasets/List_datasets_datasets_v1_get)
49+
endpoint and expand the documentation by clicking on the down chevron (`v`), we can see the different query parameters
50+
and can execute a call directly on the API:
51+
52+
![The Swagger documentation allows you to directly query the REST API from your browser.](media/swagger.webp)
53+
54+
Click the `Try it out` button to be able to modify the parameter values and then click the `execute` button to make the request directly from the documentation page.
55+
Under `response` you will also see an example on how to make the request through the command line using `curl`, e.g.:
56+
57+
```bash
58+
curl -X 'GET' \
59+
'https://api.aiod.eu/datasets/v1?schema=aiod&offset=0&limit=10' \
60+
-H 'accept: application/json'
61+
```
62+
63+
Below the example, you will find a section `Server Response` which displays the actual response from the service (if you clicked `execute`).
64+
Normally, this should look similar to the image below; a [HTTP Status Code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status),
65+
and data (in JSON).
66+
67+
![After executing a query, Swagger shows the JSON response.](media/response.webp)
68+
69+
Below the actual server response is a `response` section which lists information about the possible responses, including
70+
for example different error codes.
71+
72+
### Modifying Information
73+
74+
!!! tip
75+
76+
When exploring these endpoints, prefer to connect to the test server instead to avoid editing production data.
77+
You can find the test API at [https://aiod-dev.i3a.es](https://aiod-dev.i3a.es).
78+
79+
The `POST` and `PUT` endpoints allow the addition or modification of assets on the platform.
80+
You can explore them in a similar way as with the `GET` endpoints, with two important differences.
81+
82+
The first is that they require authentication.
83+
To authenticate within the Swagger pages, navigate to the top and click `Authorize` and log in.
84+
Scroll up to `OpenIdConnect (OAuth2, authorization_code with PKCE)` and click `Authorize` to be taken to
85+
the keycloak login page. Log in with your preferred identity provider through `EGI Check-in`.
86+
87+
The second important distinction as that you will provide data through a JSON body instead of individual parameters.
88+
The documentation page will prepopulate example data to help you know what information to provide under
89+
the `Example Value` tab of the `Request Body` section. To know what values are accepted, you can click the
90+
`Schema` tab instead.
91+
92+
![The "schema" tab in Swagger shows allowed types](media/post.webp)
93+
94+
95+
### Alternative Documentation (ReDoc)
96+
The [ReDoc documentation](https://api.aiod.eu/redoc) provides pretty similar functionality to the Swagger documentation.
97+
The main difference is that the Swagger page allows you to run queries against the REST API, whereas the ReDoc documentation does not.
98+
However, some people prefer the organisation of ReDoc,
99+
especially with respect to documentation of the expected responses and the schema documentation.
100+
101+
## REST API using CURL
102+
The Swagger documentation gives examples on how to use CURL for the various endpoints.
103+
To see examples, simply expand the endpoint's documentation and click `Try it out`, fill in any parameters, and click `Execute`.
104+
The query will be executed, but it will also generate a `curl` command which matches the query.
105+
106+
For example, listing the first 10 datasets:
107+
108+
```bash
109+
curl -X 'GET' \
110+
'http://api.aiod.eu/datasets/v1?schema=aiod&offset=0&limit=10' \
111+
-H 'accept: application/json'
112+
```

docs/developer/auth.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Authentication
2+
3+
--8<-- "./authentication/README.md"

docs/developer/code.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code/Architecture
2+
3+
--8<-- "./src/README.md"

docs/developer/migration.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Database Schema Migrations
2+
3+
--8<-- "./alembic/README.md"

docs/developer/scripts.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Scripts
2+
3+
--8<-- "scripts/README.md"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/media/post.webp

14.3 KB
Binary file not shown.

docs/media/response.webp

14.8 KB
Binary file not shown.

docs/media/swagger.webp

18.7 KB
Binary file not shown.

mkdocs.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
site_name: AI-on-Demand REST API
2+
site_url: https://api.aiod.eu/docs
3+
theme:
4+
name: material
5+
features:
6+
- content.code.copy
7+
8+
nav:
9+
- Using the API: Using.md
10+
- Hosting the API: Hosting.md
11+
- 'Developer Resources': README.md
12+
- 'Unorganized Docs':
13+
- 'Code Advice': developer/code.md
14+
- 'Keycloak': developer/auth.md
15+
- 'DB Schema Migration': developer/migration.md
16+
- 'Scripts': developer/scripts.md
17+
18+
markdown_extensions:
19+
- pymdownx.snippets:
20+
check_paths: true
21+
- admonition
22+
- pymdownx.details
23+
- pymdownx.superfences
24+
- pymdownx.tabbed:
25+
alternate_style: true

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ authors = [
1414
{ name = "Taniya Das", email = "[email protected]" }
1515
]
1616
dependencies = [
17+
"mkdocs-material",
1718
"urllib3== 2.1.0",
1819
"bibtexparser==1.4.1",
1920
"huggingface_hub==0.23.4",

0 commit comments

Comments
 (0)