Skip to main content

AstraPy is a Pythonic SDK for DataStax Astra

Project description

AstraPy

Actions Status

AstraPy is a Pythonic SDK for DataStax Astra and Stargate

Resources

Getting Started

Install AstraPy

pip install astrapy

Using the HTTP Client

You can use the HTTP client to work with any Astra/Stargate endpoint. API Reference

from astrapy.rest import create_client, http_methods
import uuid

# get Astra connection information from environment variables
ASTRA_DB_ID = os.environ.get('ASTRA_DB_ID')
ASTRA_DB_REGION = os.environ.get('ASTRA_DB_REGION')
ASTRA_DB_APPLICATION_TOKEN = os.environ.get('ASTRA_DB_APPLICATION_TOKEN')
ASTRA_DB_KEYSPACE = os.environ.get('ASTRA_DB_KEYSPACE')
ASTRA_DB_COLLECTION = "test"

# setup an Astra Client
astra_http_client = create_client(astra_database_id=ASTRA_DB_ID,
                         astra_database_region=ASTRA_DB_REGION,
                         astra_application_token=ASTRA_DB_APPLICATION_TOKEN)

# create a document on Astra using the Document API
doc_uuid = uuid.uuid4()
astra_http_client.request(
    method=http_methods.PUT,
    path=f"/api/rest/v2/namespaces/{ASTRA_DB_KEYSPACE}/collections/{ASTRA_DB_COLLECTION}/{doc_uuid}",
    json_data={
        "first_name": "Cliff",
        "last_name": "Wicklow",
        "emails": ["cliff.wicklow@example.com"],
    })

Using the Collections module

You can use the collections module to work with Documents in a simple way. API Reference

Refer to this file for comprehensive examples.

from astrapy.collections import create_client, AstraCollection
import uuid

# get Astra connection information from environment variables
ASTRA_DB_ID = os.environ.get('ASTRA_DB_ID')
ASTRA_DB_REGION = os.environ.get('ASTRA_DB_REGION')
ASTRA_DB_APPLICATION_TOKEN = os.environ.get('ASTRA_DB_APPLICATION_TOKEN')
ASTRA_DB_KEYSPACE = os.environ.get('ASTRA_DB_KEYSPACE')
TEST_COLLECTION_NAME = "test"

# setup an Astra Client and create a shortcut to our test colllection
astra_client = create_client(astra_database_id=ASTRA_DB_ID,
                                astra_database_region=ASTRA_DB_REGION,
                                astra_application_token=ASTRA_DB_APPLICATION_TOKEN)
test_collection = astra_client.namespace(ASTRA_DB_KEYSPACE).collection(TEST_COLLECTION_NAME)

# create a new document
cliff_uuid = str(uuid.uuid4())
test_collection.create(path=cliff_uuid, document={
    "first_name": "Cliff",
    "last_name": "Wicklow",
})

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

astrapy-0.2.0.tar.gz (8.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: astrapy-0.2.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for astrapy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 77fa529900661e920d21ab4790e7d76dfd7e159fd10c376c33e694eb2c23c531
MD5 9a0e1cac1a288b375df3b36bb7c87c18
BLAKE2b-256 4cb0f31ed30be8fe1e033d61b67b743e62e2aed5ff8838835e64c62a4008bdde

See more details on using hashes here.

Supported by

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