Skip to main content

No project description provided

Project description

SWS API Client

This library provides the user with a set of useful tools to easily interact with the FAO SWS (Statistical Working System) REST APIs.

Deploy

python3 -m build                                                              
twine upload dist/*

Installation

The module is available on Pypi:

python -m pip install sws_api_client

The library requires Python 3.11+.

Usage

To use the package the user needs to create an instance of the SwsApiClient class, provide it with the necessary parameters and execute the methods to query the specific endpoints.

Instantiate the client locally

There are three methods to instantiate the client:

1. Pass the sws_endpoint and the access_token to the constructor

from sws_api_client import SwsApiClient

sws_client = SwsApiClient(sws_endpoint="<sws_endpoint>", access_token="<access_token>")

2. Pass to sws_endpoint and the access_token as named arguments

We need to execute the script from command line passing --sws_endpoint and --access_token as arguments:

python script.py --sws_endpoint <endpoint> --access_token <test_access_token>

And instantiate the client in our script with the class method from_args:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.from_args()

3. Create a conf file where to store the arguments

We need to create a conf file (default name: "conf_sws_api_client.json") with the following structure:

{
    "sws_endpoint": "https://sws.dev.fao.org",
    "sws_token": "XXX",
    "current_task_id": "XXX",
    "current_execution_id": "XXX",
    "authclient": {
        "clientId": "XXX",
        "clientSecret": "XXX",
        "tokenEndpoint": "https://fao-dev.auth.eu-west-1.amazoncognito.com/oauth2/token",
        "scope": "sws/user"
    }
}

And instantiate the client in our script with the class method auto:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.auto()

Behind the scene it will automatically detect the fact that you are in debug mode and it will instanciate the client using the from_conf method

Instantiate the client in a SWS plugin

When working withing a SWS plugin instantiate the client as:

from sws_api_client import SwsApiClient

sws_client = SwsApiClient.auto()

Behind the scene it will automatically detect the fact that you are in a SWS plugin and it will instanciate the client using the from_env method

Perform requests

To perform requests you just need to call the available methods using the SwsApiClient object, as an example:

datasets = Datasets(sws_client)
dataset = datasets.get_dataset_info('aproduction')
logger.info(f"Dataset info: {dataset}")

more complete examples can be found in the example folder:

cp example/conf_sws_api_client.json.example example/conf_sws_api_client.json
# edit the file with your credentials
python3 -m venv example/.venv
source example/.venv/bin/activate
pip install -r example/requirements
python example/dataset_example.py

If you need to perform a test request not in debug mode, you can use the following command:

AWS_PROFILE=fao-dev DEBUG_MODE=FALSE SWS_USER_CREDENTIALS_SECRET_NAME=dev/sws/user_client SWS_TOKEN=YOUR_TOKEN SWS_ENDPOINT=https://sws.dev.fao.org python3 dataset_example.py

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

sws_api_client-1.0.12.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

sws_api_client-1.0.12-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file sws_api_client-1.0.12.tar.gz.

File metadata

  • Download URL: sws_api_client-1.0.12.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.1.79

File hashes

Hashes for sws_api_client-1.0.12.tar.gz
Algorithm Hash digest
SHA256 5ac2c44f66f56e9d63c6089916d4d3b16168474b511734f435d0732c23b66712
MD5 b6f3e913aa17578b48fe1fe65d38b76a
BLAKE2b-256 fa50bd05a79f9db1180d0a02a0ef4eb7890d5f1b498f0cf636eb5a6cf910b0d9

See more details on using hashes here.

File details

Details for the file sws_api_client-1.0.12-py3-none-any.whl.

File metadata

  • Download URL: sws_api_client-1.0.12-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.1.79

File hashes

Hashes for sws_api_client-1.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 1cfd723e17b3b6e36d1d812afc1a9037942bff806710ce29f8cf50d7f65532bf
MD5 c12f0584412d1d0f29f1f8bd541361fa
BLAKE2b-256 73c08187d78a718dab5ffefa253f698d94faabff476d7c106bffc9bf00e50b8e

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