Skip to main content

Client SDK for Fetchable

Project description

This is the Fetchable client-side Software Development Kit (SDK) for Python. It allows developers of robots, smart speakers, voice assistants, etc. to quickly develop programs that make calls to the Fetchable API. The library has only been tested on Ubuntu systems so far.

Table of Contents

Installation

Pre-requisites

  • Python 2.7 or 3.6

  • An active Fetchable account.

Install Package

The recommended way to install this package is through pip (or pip3) as follows:

$ pip install fetchable

Set Up Keys

  1. Log into your account and on the console page, download the authentication keys and note the location it downloaded to. The file should be named ‘fetchable_auth_keys.json’. Note: be sure to keep this file secure and share it or commit it to any source control.

2. By default, when the library runs it will attempt to read the file containing your keys from an environment variable which specifies the path to the file (you can also override the environment variable by explicitly setting the path in the constructor). Set the environment variable as so:

$ export FETCHABLE_AUTH_FILE=/path/to/fetchable_auth_keys.json

Usage

Once you have completed the installation, you can start fetching data from the index. The main class used to interact with the API is the FetchableClient object.

Constructing the Client Object

There are three optional parameters which can be passed to the constructor of the FetchableClient object.

  1. API version

The verison of the API to be used can be specified with the api_version parameter. The parameter is an enum defined in the configuration file which needs to be imported. The FetchableClient object will default to the latest version in the absence of this parameter.

from fetchable import FetchableClient
from fetchable import configuration

client = FetchableClient(api_version=configuration.api_version.v0_1)
  1. User agent

The FetchableClient object allows you to set your own user-agent header, this can be done through the user_agent parameter in the format <custom-user-agent>/<version> where version takes the form v0.1, v0.2, v1.0, etc. By setting this to something relevant to only your application (e.g. amazing-chatbot/0.1) and by whitelisting only that value through the console you can enhance the security of your account and distinguish between different applications. The default is value is fetchable-python-client/v?.?.

from fetchable import FetchableClient

client = FetchableClient(user_agent='amazing-chatbot/0.1')
  1. Authentication credentials

By default, the libary will attempt to read your authentication keys from the file specified by the FETCHABLE_AUTH_FILE environment variable. You can also specify this through the constructor. If both are set, the value passed through to the constructor will override the environment variable. If neither are set, an exception is thrown.

from fetchable import FetchableClient

client = FetchableClient(auth_file='/path/to/file/here.json')

Fetching from endpoints

Once the installation has been completed and the object has been constructed, you can begin making calls against the API endpoints. Note: these endpoints only cover version v0.1 of the API, visit the endpoint documentation for more details on these.

  1. API status endpoint

This endpoint is useful to test the connection and authentication of the client as well as receive the current status of the API.

from fetchable import FetchableClient

client = FetchableClient()

status_response = client.status()

if(status_response['status_code']==200):
    print("The Fetchable API is up - all systems are go")
elif(status_response['status_code']==1001):
    print("I can't connect to the internet right now...")
else:
    print(status_response)
  1. Entity-attribute endpoint

This endpoint is used to fetch the attributes of entities in our index.

from fetchable import FetchableClient

client = FetchableClient()

entity_response = client.fetch_entity_atrribute("mount_everest", "elevation")

if(entity_response['status_code']==200):
    print("The height of mount_everest is {} {}.".format(entity_response['value'], entity_response['unit']))
elif(entity_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(entity_response)
  1. Dictionary endpoint

Used to fetch the definitions of words.

from fetchable import FetchableClient

client = FetchableClient()

definition_response = client.fetch_word_definition("ameliorate")

if(definition_response['status_code']==200):
    print("The definition of {}: is {}".format("ameliorate", definition_response['meanings'][0]))
elif(definition_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(definition_response)
  1. Joke endpoint

from fetchable import FetchableClient

client = FetchableClient()

joke_response = client.fetch_joke()

if(joke_response['status_code']==200):
    print("{} - {}".format(joke_response['setup'], joke_response['punchline']))
elif(joke_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(joke_response)
  1. Inspirational quote endpoint

from fetchable import FetchableClient

client = FetchableClient()

quote_response = client.fetch_quote()

if(quote_response['status_code']==200):
    print("{} by {}".format(quote_response['quote'], quote_response['author']))
elif(quote_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(quote_response)
  1. Fun fact endpoint

from fetchable import FetchableClient

client = FetchableClient()

fun_fact_response = client.fetch_fun_fact()

if(fun_fact_response['status_code']==200):
    print(fun_fact_response['fun_fact'])
elif(fun_fact_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(fun_fact_response)
  1. Exact endpoint

This endpoint allows you to specify an exact endpoint to fetch.

from fetchable import FetchableClient

client = FetchableClient()

endpoint_response = client.fetch_endpoint("/v0.1/mount_everest/height")

if(endpoint_response['status_code']==200):
    print("The height of mount_everest is {} {}s.".format(endpoint_response['value'], endpoint_response['unit']))
elif(endpoint_response['status_code']==1001):
    print("Can't connect to the internet right now...")
else:
    print(endpoint_response)

Errors

  1. Exceptions

The FetchableClient object will throw exceptions when:

  • The path to the authentication file is not set through an environment variable or constructor argument.

  • The path to the authentication file is not a valid file path.

  • The authentication file is not formatted properly.

  • Functions taking string parameters are passed arguments which are not strings.

  1. Error codes

There are two types of error codes you can receive back from the client object. Those in the 1xxx range are errors thrown by the client object itself and other error codes in the 2xx, 3xx, 4xx and 5xx ranges are the standard http error codes received from the server. For more information on the server error codes visit the documentation.

Code

Description

Reason

1001

Connection error

The client cannot make a connection to the API server.

1002

Timeout error

The request timed out.

1003

Proxy error

A proxy error occurred.

1004

Unknown error

An unknown error occurred.

Contributing

Contributions are welcome and encouraged! See the Contributing Guide for information on how to contribute.

License

Licensed under Apache Version 2.0.

See the LICENSE file for more information.

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

fetchable-0.1.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

fetchable-0.1.3-py2-none-any.whl (10.8 kB view details)

Uploaded Python 2

File details

Details for the file fetchable-0.1.3.tar.gz.

File metadata

  • Download URL: fetchable-0.1.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.15+

File hashes

Hashes for fetchable-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e5c8c4fa1974ad7195522e0d9751c4eaf08d25f617a80eba9f367cc1bdfc417b
MD5 3cfcf40ec3a96ae17370d318fdda66e0
BLAKE2b-256 4253c12bbc8cca35f414a5b149c9c3d216cc0eb7aee43d0c52cf2587fc2ba392

See more details on using hashes here.

File details

Details for the file fetchable-0.1.3-py2-none-any.whl.

File metadata

  • Download URL: fetchable-0.1.3-py2-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.15+

File hashes

Hashes for fetchable-0.1.3-py2-none-any.whl
Algorithm Hash digest
SHA256 d4c681b0afcd728b5ff4ae14b67a51f0cf56e931f92ddf7adc425119b4d234f5
MD5 7d04c370cee8a5381ce2d853511f4250
BLAKE2b-256 2cb997c1b1cdc19e39def17980d030fb6e0ae778432726cf7d549a09bef309ef

See more details on using hashes here.

Supported by

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