Skip to content
Snippets Groups Projects
Verified Commit d04832f4 authored by Ciarán Ainsworth's avatar Ciarán Ainsworth
Browse files

Update README

parent 43d1beae
No related branches found
No related tags found
1 merge request!4Update README
Pipeline #23843 passed
# funkwhale-api-client
A client library for accessing Funkwhale API
A client library for accessing the Funkwhale API.
> **Note**: This client is under active development and is not considered production-ready.
## Usage
First, create a client:
### Endpoint structure
The Funkwhale API client follows the same structure for each available endpoint.
1. Each API path/method combination is representsd by a Python module with four methods:
1. `sync`: A blocking request that returns parsed data (if successful) or `None`
2. `sync_detailed`: A blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.
3. `asyncio`: An async request that returns parsed data (if successful) or `None`
4. `asyncio_detailed`: An async request that always returns a `Request`, optionally with `parsed` set if the request was successful.
2. All path/query parameters and bodies are represented by method arguments.
3. If the endpoint contains tags, the first tag is used as a module name for the function
4. Any endpoint which doesn't contain a tag is located in `funkwhale_api_client.api.default`
### Use the library in your project
To get started, create a `Client` in your project.
```python
from funkwhale_api_client import Client
......@@ -10,7 +29,7 @@ from funkwhale_api_client import Client
client = Client(base_url="https://api.example.com")
```
If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead:
If you're interacting with endpoints that require authentication, create an `AuthenticatedClient`.
```python
from funkwhale_api_client import AuthenticatedClient
......@@ -18,7 +37,7 @@ from funkwhale_api_client import AuthenticatedClient
client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken")
```
Now call your endpoint and use your models:
Next, call the endpoint using the data model and endpoint method.
```python
from funkwhale_api_client.models import MyDataModel
......@@ -26,11 +45,11 @@ from funkwhale_api_client.api.my_tag import get_my_data_model
from funkwhale_api_client.types import Response
my_data: MyDataModel = get_my_data_model.sync(client=client)
# or if you need more info (e.g. status_code)
# return more information with the sync_detailed method
response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client)
```
Or do the same thing with an async version:
Call endpoints asynchronously by using the async methods.
```python
from funkwhale_api_client.models import MyDataModel
......@@ -41,7 +60,9 @@ my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
```
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
### Certificate validation
The library attempts to validate TLS on HTTPS endpoints by default. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
```python
client = AuthenticatedClient(
......@@ -51,7 +72,7 @@ client = AuthenticatedClient(
)
```
You can also disable certificate validation altogether, but beware that **this is a security risk**.
You can also disable certificate validation altogether. This is a **security risk** and **is not recommended**.
```python
client = AuthenticatedClient(
......@@ -61,47 +82,58 @@ client = AuthenticatedClient(
)
```
Things to know:
1. Every path/method combo becomes a Python module with four functions:
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.
1. `asyncio`: Like `sync` but async instead of blocking
1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking
1. All path/query params, and bodies become method arguments.
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)
1. Any endpoint which did not have a tag will be in `funkwhale_api_client.api.default`
## Building / publishing this Client
This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Here are the basics:
1. Update the metadata in pyproject.toml (e.g. authors, version)
1. If you're using a private repository, configure it with Poetry
1. `poetry config repositories.<your-repository-name> <url-to-your-repository>`
1. `poetry config http-basic.<your-repository-name> <username> <password>`
1. Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build`
## Contribute to development
The Funkwhale API client is generated from the Funkwhale OpenAPI schema. If you notice an issue with the API itself, consider contributing to [Funkwhale](https://dev.funkwhale.audio/funkwhale/funkwhale).
### Build / publish the client
This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Make sure you have it installed before you start.
To publish a new version of the client:
1. Update the metadata in `pyproject.toml` (e.g. authors, version)
2. If you're using a private repository, configure it with Poetry
```sh
poetry config repositories.<your-repository-name> <url-to-your-repository> # Set up your repository
poetry config http-basic.<your-repository-name> <username> <password> # Configure your credentials
```
3. Publish the client:
1. Publish to PyPI with `poetry publish --build`
2. Publish to a private repository with `poetry publish --build -r <your-repository-name>`.
If you want to install this client into another project without publishing it (e.g. for development) then:
1. If that project **is using Poetry**, you can simply do `poetry add <path-to-this-client>` from that project
1. If that project is not using Poetry:
1. If the project **uses Poetry**, add the client using `poetry add <path-to-this-client>`.
2. If the project doesn't use Poetry:
1. Build a wheel with `poetry build -f wheel`
1. Install that wheel from the other project `pip install <path-to-wheel>`
2. Install that wheel from the target project `pip install <path-to-wheel>`
### Create tests
## Contributing
Tests are split into two types: **integration tests** and **model tests**.
### Run tests
#### Integration tests
To run the tests, run `poetry run pytest`.
You can test methods by calling them with a `Client` or `AuthenticatedClient` and expecting a specific result. Check `tests/integration/test_albums.py` for an example.
### How to write test cases
#### Model tests
There are two things to test: The models and the API functions.
You can test models by asserting equality with a response from a Funkwhale server. For example, to test the `/api/v1/albums` endpoint:
Lets imagine you want to write a test case for the endpoint `/api/v1/albums`
focusing on the models for now. Since this endpoint lists the Albums, the
correct API call is in `api/albums/albums_list.py`. Check the function called
`_parse_response()`. The model used to parse the response is called
`PaginatedAlbumList`, which we will run tests against. Now curl the endpoint you
want to write tests for and put the response into `tests/data/albums.json`. Now
we can load this json file, load it with the model and do some assertions. The
example is available in `tests/unit/test_model_paginated_album_list.py`.
1. Find the API call in `api/albums/albums_list.py`
2. Check which model is used in the `_parse_response()` method. In this case `PaginatedAlbumList`
3. Fetch data from the `/api/v1/albums` endpoint using [cURL](https://curl.se/) and save the output to `tests/data/paginated_album_list.json`
4. Create a test to assert equality between the resulting JSON and the `PaginatedAlbumList` model using the model's `from_dict()` method
An example test can be found in `tests/unit/test_model_paginated_album_list.py`.
### Run tests
You can run the whole suite of tests with the following command:
```sh
poetry run pytest
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment