Asyncio SDK for Azure Cosmos DB

Overview

aio-cosmos

Asyncio SDK for Azure Cosmos DB. This library is intended to be a very thin asyncio wrapper around the Azure Comsos DB Rest API. It is not intended to have feature parity with the Microsoft Azure SDKs but to provide async versions of the most commonly used interfaces.

Feature Support

Databases

List
Create
Delete

Containers

Create
Delete

Documents

Create Single
Create Concurrent Multiple
Delete
Get
Query

Limitations

The library currently only supports Session level consistency, this may change in the future. For concurrent writes the maximum concurrency level is based on a maximum of 100 concurrent connections from the underlying aiohttp library. This may be exposed to the as a client setting in a future version.

Sessions are managed automatically for document operations. The session token is returned in the result so it is possible to manage sessions manually by providing this value in session_token to the appropriate methods. This facilitates sending the token value back to an end client in a session cookie so that writes and reads can maintain consistency across multiple instances of Cosmos.

Installation

pip install aio-cosmos

Usage

Client Setup and Basic Usage

The client can be instantiated using either the context manager as below or directly using the CosmosClient class. If using the CosmosClient class directly the user is responsible for calling the .connect() and .close() methods to ensure the client is boot-strapped and resources released at the appropriate times.

from aio_cosmos.client import get_client

async with get_client(endpoint, key) as client:
    await client.create_database('database-name')
    await client.create_container('database-name', 'container-name', '/partition_key_document_path')
    doc_id = str(uuid4())
    res = await client.create_document(f'database-name', 'container-name',
                                       {'id': doc_id, 'partition_key_document_path': 'Account-1', 'description': 'tax surcharge'}, partition_key="Account-1")

Querying Documents

Documents can be queried using the query_documents method on the client. This method returns an AsyncGenerator and should be used in an async for statement as below. The generator automatically handles paging for large datasets. If you don't wish to iterate through the results use a list comprehension to collate all of them.

async for doc in client.query_documents(f'database-name', 'container-name',
                                        query="select * from r where r.account = 'Account-1'",
                                        partition_key="Account-1"):
    print(f'doc returned by query: {doc}')

Concurrent Writes / Multiple Documents

The client provides the ability to issue concurrent document writes using asyncio/aiohttp. Each document is represented by a tuple of (document, partition key value) as below.

docs = [
    ({'id': str(uuid4()), 'account': 'Account-1', 'description': 'invoice paid'}, 'Account-1'),
    ({'id': str(uuid4()), 'account': 'Account-1', 'description': 'VAT remitted'}, 'Account-1'),
    ({'id': str(uuid4()), 'account': 'Account-1', 'description': 'interest paid'}, 'Account-1'),
    ({'id': str(uuid4()), 'account': 'Account-2', 'description': 'annual fees'}, 'Account-2'),
    ({'id': str(uuid4()), 'account': 'Account-2', 'description': 'commission'}, 'Account-2'),
]

res = await client.create_documents(f'database-name', 'container-name', docs)

Results

Results are returned in a dictionary of the following format:

{
    'status': str,
    'code': int,
    'session_token': Optional[str],
    'error': Optional[str],
    'data': Union[dict,list]
}

status will be either 'ok' or 'failed' code is the integer HTTP response code session_token is the string session code vector returned by Cosmos error is a string error message to provide context to a failed status data is either the data or error return of the operation from Cosmos

Note, to see an error return in the above format you must pass raise_on_failure=False to the client instantiation.

You might also like...
The successor of GeoSnipe, a pythonic Minecraft username sniper based on AsyncIO.
The successor of GeoSnipe, a pythonic Minecraft username sniper based on AsyncIO.

OneSnipe The successor of GeoSnipe, a pythonic Minecraft username sniper based on AsyncIO. Documentation View Documentation Features • Mojang & Micros

An asyncio Python wrapper around the Discord API, forked off of Rapptz's Discord.py.

Novus A modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python. A full fork of Rapptz's Discord.py library, with

Asynchronous and also synchronous non-official QvaPay client for asyncio and Python language.
Asynchronous and also synchronous non-official QvaPay client for asyncio and Python language.

Asynchronous and also synchronous non-official QvaPay client for asyncio and Python language. This library is still under development, the interface could be changed.

asyncio client for Deta Cloud

aiodeta Unofficial client for Deta Clound Install pip install aiodeta Supported functionality Deta Base Deta Drive Decorator for cron tasks Examples i

Spore REST API asyncio client

Spore REST API asyncio client

AWS SDK for Python

Boto3 - The AWS SDK for Python Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to wri

Python SDK for Facebook's Graph API

Facebook Python SDK This client library is designed to support the Facebook Graph API and the official Facebook JavaScript SDK, which is the canonical

Box SDK for Python

Box Python SDK Installing Getting Started Authorization Server-to-Server Auth with JWT Traditional 3-legged OAuth2 Other Auth Options Usage Documentat

The Official Dropbox API V2 SDK for Python
The Official Dropbox API V2 SDK for Python

The offical Dropbox SDK for Python. Documentation can be found on Read The Docs. Installation Create an app via the Developer Console. Install via pip

Comments
  • Upsert flag on create_document causes TypeError

    Upsert flag on create_document causes TypeError

    When using upsert=True on CosmosClient.create_document, a TypeError is thrown:

    >>> await client.create_document(database='my-db', container='my-container', partition_key='...', json={"id": "blah blah"}, upsert=True)
    

    Throws:

    Traceback (most recent call last):
      File "C:\python39\lib\concurrent\futures\_base.py", line 445, in result
        return self.__get_result()
      File "C:\python39\lib\concurrent\futures\_base.py", line 390, in __get_result
        raise self._exception
      File "<console>", line 1, in <module>
      File "[REDACTED]\.venv\lib\site-packages\aio_cosmos\client.py", line 223, in create_document
        async with self.session.post(f'{self._get_writable()}/dbs/{database}/colls/{container}/docs',
      File "[REDACTED]\.venv\lib\site-packages\aiohttp\client.py", line 1138, in __aenter__
        self._resp = await self._coro
      File "[REDACTED]\.venv\lib\site-packages\aiohttp\client.py", line 557, in _request
        resp = await req.send(conn)
      File "[REDACTED]\.venv\lib\site-packages\aiohttp\client_reqrep.py", line 669, in send
        await writer.write_headers(status_line, self.headers)
      File "[REDACTED]\.venv\lib\site-packages\aiohttp\http_writer.py", line 130, in write_headers
        buf = _serialize_headers(status_line, headers)
      File "aiohttp\_http_writer.pyx", line 132, in aiohttp._http_writer._serialize_headers
      File "aiohttp\_http_writer.pyx", line 109, in aiohttp._http_writer.to_str
    TypeError: Cannot serialize non-str key True
    

    We can currently work around this issue by passing in the boolean as a string (i.e. upsert='True') but the IDE raises warnings as the type hints expect a boolean.

    I think we just need to cast the upsert flag to a string in client.py

    opened by danarth 1
Releases(0.2.4)
Owner
Grant McDonald
Grant McDonald
A Powerful Telethon Based Telegram Spam Bot.

Yukki Multi Spam Bot 🚀 Deploy on Heroku You can Use these API ID and API HASH while deploying String Session No Requirement of API ID and API HASH Ge

46 Dec 23, 2022
Trust-minimized Bitcoin wallet

coldcore Trust-minimized, airgapped Bitcoin management This is experimental software. Wait for a formal release before use with real funds. A trust-mi

James O'Beirne 121 Jan 01, 2023
Match-making API for OpenSanctions

OpenSanctions Match-making API This directory contains code and a Docker image for running an API to match data against OpenSanctions. It is intended

OpenSanctions.org 26 Dec 15, 2022
Mventory is an API-driven solution for Makerspaces, Tinkerers, and Hackers.

Mventory is an API-driven inventory solution for Makers, Makerspaces, Hackspaces, and just about anyone else who needs to keep track of "stuff".

Make Monmouth 107 Dec 21, 2022
Huan Xu 1.6k Jan 04, 2023
A modular Telegram Python bot running on python3 with a sqlalchemy, redis, telethon.

GilbertAnimeBot A modular Telegram Python bot running on python3 with a sqlalchemy, redis, telethon. How to setup/deploy. Read these notes carefully b

Kishore 1 Jan 23, 2022
REPO USERBOT YANG DIBUAT DARI BERBAGAI REPO USERBOT GITHUB.

Lord Userbot Userbot Yang Digunakan Untuk Bersenang-Senang Di Telegram Repo Lord Userbot Repo Yang Dibuat Alvin Dari Berbagai Repo Userbot Github CARA

Alvin 70 Jan 02, 2023
This is a telegram bot built using the Oxford Dictionary API

Oxford Dictionaries Telegram Bot This is a telegram bot built using the Oxford Dictionary API Source: Oxford Dictionaries API Documentation Install En

Abhijith N T 2 Mar 18, 2022
A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat.

TgTwitterStreamer A Bot to get RealTime Tweets to a Specific Chats from Desired Persons on Twitter to Telegram Chat. For Getting ENV's Refer this Link

Anonymous 69 Dec 20, 2022
A Telegram bot to extracting text from images. All languages supported.

OCR Bot A Telegram bot to extracting text from images. All languages supported. Deploy to Heroku Local Deploying Clone the repo git clone https://gith

6 Oct 21, 2022
A telegram bot to read RSS feeds

Telegram bot to fetch RSS feeds This is a telegram bot that fetches RSS feeds in regular intervals and send it to you. The feed sources can be added o

Santhosh Thottingal 14 Dec 15, 2022
Simple integrate of API musixmatch.com with python

Python Musixmatch Simple integrate of API musixmatch.com with python Quick start $ pip install pymusixmatch or $ python setup.py install Authenticatio

Hudson Brendon 79 Dec 20, 2022
Disqus API bindings for Python

disqus-python Let's start with installing the API: pip install disqus-python Use the API by instantiating it, and then calling the method through dott

DISQUS 163 Oct 14, 2022
A python library for creating Slack slash commands using AWS Lambda Functions

slashbot Slashbot makes it easy to create slash commands using AWS Lambda functions. These can be handy for creating a secure way to execute automated

Eric Brassell 17 Oct 21, 2022
Telegram Google Translater Bot

Google-Translater-Bot Hey Mo Tech, I am simple Google Translater Bot. I can translate any language to you selected language Team Mo Tech Deploy To Her

21 Dec 01, 2022
Automate and Manage Telegram Channels

Channel Automation Bot @ChannelAutomateBot A star ⭐ from you means a lot to us! Telegram bot to automate and manage channels. Usage Deploy to Heroku T

Stark Bots 61 Dec 29, 2022
Telegram Bot for everyday raffles

SpinEverydayBot v2 Telegram bot for everyday raffles. HIGHLY EXPERIMENTAL! WORK IN PROGRESS! Setting up Requirements Python 3.9+ PostgreSQL 13+ Older

evgfilim1 18 Dec 20, 2022
Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot

- [ MAYBE UPDATE & ADD MORE MODULE ] Bagas Mirror&Leech Bot Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring file

4 Nov 23, 2021
The most versatile torrent leecher and youtube-dl bot for telegram

TorToolkit Telegram So basically Tortoolkit is aimed to be the most versatile torrent leecher and youtube-dl bot for telegram. This bot is highly cust

αвιנтн 1 Nov 11, 2021
Alcarin Tengwar - a Tengwar typeface designed to pair well with the Brill typeface

Alcarin Tengwar Alcarin Tengwar is a Tengwar typeface designed to pair well with

Toshi Omagari 23 Nov 02, 2022