Skip to main content

Conveniently access data

Project description

What is speedracer?

Speedracer is an asyncio enabled python library for interacting with Autobahn. Speedracer enables users to

  • Subscribe to datasets available in Autobahn
  • Authenticate to Autobahn APIs

Installation

Requires Python3.8+

pip install speedracer

Quickstart

See Important Notes for all SSL/TLS errors.

Subscribe to a dataset

To subscribe to a dataset on the MDB

  1. Modify the string 'PUT-DATASET-HERE' with the dataset slug
  2. Modify the certificate paths to poin to your certificates (about certificates)
  3. Download the CA Bundle from the table here, and update the ca_bundle path to point to it

Put the following code block in a file, and run it.

import asyncio
from speedracer import Connection

async def main():
    conn = Connection('https://mdb-account-server/',
              cert=('/path/to/mycert.crt', '/path/to/mycert.key'),
              ca_bundle='/path/to/ca.cer')
    sub = await conn.subscribe('PUT-DATASET-HERE')
    async for msg in sub:
        print(msg.data.decode())

if __name__ == '__main__':
    asyncio.run(main())

Authenticate to Autobahn APIs

Autobahn uses JWT credentials for authorization. These tokens are put into the Authorization header for all HTTPs requests.

JWTs are obtained by exchanging private keys with the API Gateway server and must be periodically refreshed.

speedracer automates the process of fetching JWTs and refreshing them when they expire.

manager = JWTManager('https://api-gateway/getjwt',
              cert=('/path/to/mycert.crt', '/path/to/mycert.key'),
              verify='/path/to/ca.cer')
auth_headers = manager.get_headers()
requests.get('https://autobahn-service/', headers=auth_headers)

Setup logging for viewing speedracer logs

Logging is handled by Python's standard logging module. By default, speedracer will not log unless the root logger has been configured by the calling code.

To set up the root logger in your code:

import logging

logging.basicConfig()

Advanced Usage

Dataset Subscriptions

Callback

Instead of iterating over a Subscription, you can provide a callback that takes a message as an argument.

def cb(msg):
    print(msg.data.decode())

conn = Connection('https://mdb-account-server.com')
sub = await conn.subscribe('mydataset', callback=cb)
await sub.wait(messages=10) # exit after 10 messages received

Seek

Autobahn maintains a short history of messages (by default 7 days) for each dataset. To navigate to different points in the stream use the seek method. Seek accepts a message sequence number or a datetime object.

# seek to message sequence 1
await sub.seek(1)

# seek to 5 minutes ago
await sub.seek(datetime.datetime.utcnow() - datetime.timedelta(minutes=5))

Offset

By default, subscribe starts at the current time; to start at the beginning of the stream use Offset.BEGIN

sub = await conn.subscribe('mydataset', offset=Offset.BEGIN)

Low Latency

By default, each Subscription fetches messages in batches of 10. If the server has fewer than 10 messages to send, it will wait up to 10 seconds for new messages before returning the messages it does have. For low-latency applications set the batch size to 1.

sub = await conn.subscribe('mydataset', batch=1)

Important Notes

SSL/TLS Errors

SSL/TLS errors occur for 2 reasons

  1. Invalid or missing client certificates
  2. No certificate authority configured

Providing Client Certificates

By default, speedracer uses PEM formatted X.509 certificates for user authentication. By default speedracer will use certificates placed in the users $HOME/.ssh directory. The certificate and key files should be named $HOME/.ssh/cert.crt and $HOME/.ssh/cert.key, respectively.

To specify a different path, pass a tuple containing the path to the certificate and key files to the cert argument.

conn = Connection('https://mdb-account-server.com',
          cert=('./mycert.crt', './mycert.key'))

To create PEM formatted .crt and .key certificate files from a PKCS .p12 signing_cert file, run the following OpenSSL commands.

openssl pkcs12 -in INFILE.p12 -out OUTFILE.key -nodes -nocerts
openssl pkcs12 -in INFILE.p12 -out OUTFILE.crt -nokeys

Providing a Certificate Authority (SSLCertVerificationError)

Certificate Authorities establish a chain of trust. This is usually configured globally as part of the operating system. There are cases where the OS does not have the proper certificate authority configured.

In these instances, users may specify a cabundle as a X.509 PEM file via the argument ca_bundle.

conn = Connection('https://mdb-account-server.com', ca_bundle='ca.cer')

Download the appropiate CA Bundle.

Autobahn Environment Link to CA Bundle

Subscription not starting at specified offset

Subscriptions to datasets are durable. This means that once you have subscribed to a dataset, message delivery restarts where you left off--even if your program restarts.

To change where your subscription starts, use seek.

Subscriptions will expire after 1-week if not used.

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

speedracer-0.0.7.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

speedracer-0.0.7-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file speedracer-0.0.7.tar.gz.

File metadata

  • Download URL: speedracer-0.0.7.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for speedracer-0.0.7.tar.gz
Algorithm Hash digest
SHA256 fb54a579caa32b331768c48e067b9606344ad09d0aeef41c99ab7f74382615f3
MD5 ebc9bda4fbf470cd73bbf6f0b30eafc9
BLAKE2b-256 c60f6813690946b3ed562a53ade97d1b1dbd1463dae30f5634653efc28403a57

See more details on using hashes here.

File details

Details for the file speedracer-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: speedracer-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for speedracer-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a7b26d38d67a442bb26e8d317b665d4aead8f7c36bf1a7b497138d7beba4e12b
MD5 2067b7e5f1d0d5d6f0b665f1853c4015
BLAKE2b-256 dc1e0a7c4a3a7266ac1422b19d20d05ecfd42975126f9f5b96c7b1ebedfb83d9

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