Skip to main content

DataSource Python SDK

Project description

datasource-python

Python SDK for all DataSource iterations

Installation

Install this package via pip:

pip install bocks_ds

Usage

The Client

Import this package and instantiate a client object.

import bocks_ds

ds_client = bocks_ds.Client("starfinder") # provide the name of the datasource to access

Error Example:

try:
    bad_client = Client("bad_client")
except DSTargetError as e:
    print(e) # The target 'bad_client' provided in Client initialization is not in available target names:\n['starfinder', 'pathfinder']

Fetching Data

There are currently only two options to consider when fetching data: the type and the parameters.

The "type" can be thought of as a data table, though we will discuss some complexities later on.

response = ds_client.armor.get(['name', 'price']) # 'armor' here is the query type
if response.status_code == 200: # optional status check
    data = response.json() # '.json()' gives the API data output we came for

Note that the ds_client will accept any value as an attribute, which it will then use to craft an API request.

Error Example:

try:
    client.bad_name.get(['name']).json()
except DSQueryError as e:
    print(e) # <Response 400> DataSource did not find table/field 'bad_name'.

Refining Your Request

The API allows for active filtering of strings and integers within your request. To do this, you'll need to set arguments immediately prior to making the request.

For data values that return strings, you may refine with the terms <value_name>_like and <value_name>_is. For "like" queries, sequencing is not considered and the search is not case sensitive. For "is" queries, only exact, case-sensitive matches will be returned.

For data values that return integers, you may refine with the terms <value_name>_min, <value_name>_max and <value_name>_equals. These equate to "greater than or eqal to", "less than or equal to", and "equal to", respectively.

Finally, it is often valuable to select an item specifically by its ID, which functions in a more direct manner than the queries for integers (see example below).

All arguments must be presented as a single dictionary, as seen in the examples below.

client.armor.set_arguments({"name_like":"basic"})
response = ds_client.armor.get(['name', 'price'])
client.armor.set_arguments({"price_min":200, "price_max":2000})
response = ds_client.armor.get(['name', 'price'])
client.armor.set_arguments({"id":1})
response = ds_client.armor.get(['name', 'price'])

Note that these requests do not stack, but you can place all terms into a single dictionary to futher refine results. If set_arguments is called multiple times, the final call will overwrite previous calls.

query = {"name_like":"basic", "price_min":200, "price_max":2000}
client.armor.set_arguments(query)
response = ds_client.armor.get(['name', 'price'])

As a final note: all arguments are cleared when get is called.

Error Example

try:
    client.armor.set_arguments({"name_min":200})
    erroneous_armor = client.armor.get(['name', 'price']) # Throws exception due to errors in response.json()
except DSQueryError as e:
    print(e) # <Response 400> ['Unknown argument "name_min" on field "Query.armor". Did you mean "name_is", "name_like", "type_min", "bulk_min", or "level_min"?']

Fetching Nested Data

As mentioned previously, not all types match their data tables precisely. Some types include additional data from relationships with other tables. Documentation for these relationships is automatically generated in the API Documentation (such as at docs.sfdatasource.com).

In order to access nested data, it is required that you present a dictionary describing the data to fetch.

Where we previously provided a list of string field-names to query, we will now include a dictionary in that list, as seen in the examples below.

query = [
    "name",
    {
        "effect_ranges": ["name", "description"],
    }
]
all_spells = ds_client.spells.get(query)

This logic is recursive, so in the event that a relationship target has it's own relationships you may do the following:

query = [
    "name",
    {
        "class_proficiencies": ['name'],
        "class_features": ["name", {
            "class_special_skills": ['name']
        }],
    }
]
spells = client.classes.get(query)

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

bocks_ds-0.2.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

bocks_ds-0.2.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file bocks_ds-0.2.1.tar.gz.

File metadata

  • Download URL: bocks_ds-0.2.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for bocks_ds-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f0be2ede121ec1eddbae1d7348786d4ec663384145d08691bd4da31940f532bd
MD5 7eb6905a63cd35f0377a46a7286c1314
BLAKE2b-256 448b051bf52b3e9d629e5cc36be980309d915cad7e92398111ad7640ae56a1b3

See more details on using hashes here.

File details

Details for the file bocks_ds-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: bocks_ds-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for bocks_ds-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dfdb371539de1ec3580499ca4902ef405cd739c60d871bf696f9e3574b3c44af
MD5 9b16c6a4f6d00231d15368b047bf68ac
BLAKE2b-256 a95e056ebb7e9022337a7010819a5e0652325f5d01178aa3e0007f2a346b45f0

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