Skip to main content

A python library for ReCodEx API calls.

Project description

ReCodEx Client Library

A client API library for the ReCodEx system. This library can be used in custom scripts and command-line interfaces for fine-grained interactions with the system.

Installation

The recommended way to install the library is via pip. Python 3.11 is recommended, but other versions may also work:

pip install recodex-pylib

Installation from Source

For developers or those who prefer to install directly from the source code, follow these steps. First, you need to generate the API code from the OpenAPI Specification (OAS) file. This is done by a script that requires wget and Java to be installed on your system (automatically downloads the Swagger Codegen CLI as jar from Maven.org). Assuming all commands are executed from the repository root.

./bin/init.sh

Then, you can either use your system's Python installation:

python3 install -r requirements.txt
python3 install -e .

Or, you can set up a local venv environment (recommended):

python3 -m venv ./venv
./venv/bin/pip install -r requirements.txt
./venv/bin/pip install -e .

Which can be activated in your shell with:

source ./venv/bin/activate

This will install the library in interactive mode, meaning that changes made to the source afterwards will be automatically reflected in the installation.

Usage

Creating Client Instance

The Client class is the primary interface with the library and can be created using an existing API token or ReCodEx credentials.

By using the get_client_from_token and get_client_from_credentials functions shown below, a local session file will be created that holds the host URL and API token. In case a session already exists, the functions will remove it and create a new one. Note that the get_client_from_credentials function will always communicate with the server to create a new API token, please use the function below if there is a session available.

The get_client_from_session function can be used to create a Client instance directly from the session file without communicating with the server.

It is not recommended to instantiate the Client directly (without the client_factory), because doing so will not create a session.

from recodex import client_factory
from recodex.client import Client

# URL of the API server
api_url = "http://localhost:4000"

# JWT token used for authentication
api_token = "eyJhbGciOi..."

username = "user"
password = "pwd"

# creating a client with an API token (also creates a session file that stores the API token)
client = client_factory.get_client_from_token(api_url, api_token, verbose=True)

# creating a client with ReCodEx credentials (also creates a session file that stores a newly created API token)
client = client_factory.get_client_from_credentials(api_url, username, password, verbose=True)

# creating a client from the session
client = client_factory.get_client_from_session()

# removing the session file
client_factory.remove_session()

Calling Endpoints

There are two methods for calling an endpoint that differ on how the it is specified.

  • send_request accepts string names of the presenter and action.
  • send_request_by_callback accepts a generated callback.

Request parameters are passed with the path_params, query_params, body, and files function parameters as name-value pairs. Generated model instances can also be passed to the body parameter.

# DefaultApi can be used as an enumeration of all endpoint callbacks
from recodex.generated.swagger_client import DefaultApi
# generated models are imported one by one
from recodex.generated.swagger_client.models.id_organizational_body import IdOrganizationalBody

# specify endpoint with string identifiers
response = client.send_request("groups", "set_organizational", path_params={"id": "154b..."}, body={"value": True})

# specify endpoint with a callback
response = client.send_request_by_callback(
  DefaultApi.groups_presenter_action_set_organizational, 
  path_params={"id": "154b..."},
  # body can also be specified with a generated model class
  body=IdOrganizationalBody(value=True)
)

The methods return a ClientResponse object that contains the status, headers, and the actual data. The data can be retrieved in multiple ways.

# binary response data
binary_data = response.data_binary

# stringified response data
utf8_string = response.data

# data parsed into a dictionary
dictionary_data = response.get_parsed_data()
if dictionary_data is None:
  raise Exception("Data is not in JSON format.")

# formatted data (useful for printing in the CLI)
formatted_json_string = response.get_json_string()
formatted_yaml_string = response.get_json_string()

Utility Functions

In case you want to manually create api tokens, the Client contains methods for this purpose.

new_token = client.get_login_token(username, password)
refresh_token = client.get_refresh_token()

To upload a file, you can use the upload utility function that automatically sends the file in chunks.

from recodex.helpers.file_upload_helper import upload
file_id = upload(client, "file.txt", verbose=True)

Development

Commands

The commands folder contains four utility commands:

  • init.sh is used for initial setup of the repository after download; it is described in the installation section.
  • update-generated-api.sh generates code from a new OAS and replaces the old one. It also generates a diff summary in api-changes.md
  • run-tests-locally.sh installs the library in interactive mode and runs all tests in the tests folder.

Repository Structure

Library Code

The ./src/recodex contains all code of the library.

The client.py contains the main Client class that links all parts together. It uses the SwaggerValidator (client_components/swagger_validator.py) class to validate requests against their schema and the EndpointResolver (client_components/endpoint_resolver.py) to translate endpoint identifiers to the generated API functions.

It uses the generated ApiClient and DefaultApi classes to interface the generated part of the library, which is contained in the generated folder. The folder is not part of the repository and needs to be manually generated.

The aliases.yaml file contains all aliases for endpoints. These aliases can be used instead of the default presenter and action identifiers. The aliases are parsed and managed by the AliasContainer (client_components/alias_container.py) class.

Repository Utilities

During code regeneration, the ./src/swagger-diffchecker.py script is used to find differences between the old and new OAS and writes a summary to this README file.

Testing

Testing relies on a mock ReCodEx API server implemented in flask that exposes a few endpoints, which are implemented in the ./tests/mockEndpoints folder. The files are then linked to the server in the ./tests/mock_server.py script.

The actual tests are implemented in dedicated classes in the ./tests/testClasses folder. They derive from the test_class_base.py which uses the full login process to connect to the mock server.

The tests are automatically run in GitHub CI/CD, where code is generated from the ./tests/swagger.yaml file. This file should be updated regularly to make sure the tests reflect the latest state.

Latest API Endpoint Changes

You can find a summary of the latest API changes here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recodex_pylib-0.2.0.tar.gz (166.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recodex_pylib-0.2.0-py3-none-any.whl (318.2 kB view details)

Uploaded Python 3

File details

Details for the file recodex_pylib-0.2.0.tar.gz.

File metadata

  • Download URL: recodex_pylib-0.2.0.tar.gz
  • Upload date:
  • Size: 166.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for recodex_pylib-0.2.0.tar.gz
Algorithm Hash digest
SHA256 45d3a5e16516f46e4a0a479891d0c57697f81dd18222b81dc584b9fb0c6049b0
MD5 0c31269d1afc2adbccaf119e22e38344
BLAKE2b-256 6c1351372f7b4331dbbd51f224728101cfe52fdbf899b4ce704ca007f7ce1175

See more details on using hashes here.

File details

Details for the file recodex_pylib-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: recodex_pylib-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 318.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for recodex_pylib-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33b07aaa8a90a76f288275b5b036492b72a2ffffbbc6f34e8ba24659a83c4410
MD5 f54b1f716ca219535e97b9911a8fbf20
BLAKE2b-256 72ceb0c5857a394c96359614a80fa10e1f436d248ad8264f8f47b8600ac6945e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page