Skip to main content

Crypto WS API connector for ASYNC requests

Project description

Crypto WS API connector for ASYNC requests

Full coverage of all methods provided by the interface

Provides of connection management, keepalive and rate limits control


PyPI version DeepSource DeepSource sonarcloud Downloads


For :heavy_check_mark:Binance,


Features

Lightweight and efficient solution to utilize of all available methods provided through the:

Session management layer for:

  • Credentials
  • Connection
  • Keepalive
  • Error handling
  • Limits control
  • Methods construction
    • Generating session request id by default
    • Creating request on-the-fly from method name and params: {}
    • Generating signature if necessary
    • Response handling
  • logging

User interface layer

  • Start session instance
  • Getting session operational status
  • Send async request
  • Get response or raised exception
  • Stop session instance

Get started

Install use PIP

pip install crypto_ws_api

For upgrade to latest versions use:

pip install -U crypto_ws_api

After first install create environment by run

crypto_ws_api_init

in terminal window.

The config directory will be created. You get path to config:

ubuntu@ubuntu:~$ crypto_ws_api_init

Can't find config file! Creating it...

Before first run set account(s) API key into /home/ubuntu/.config/crypto_ws_api/ws_api.toml

Prepare exchange account

  • For test purpose log in at Binance Spot Test Network
  • Create API Key
  • After install and create environment specify api_key and api_secret to the config file

Start demo

  • Run in terminal window
crypto_ws_api_demo

Useful tips

crypto_ws_api/demo.py - complete and fully functional example

Get credentials and create user session

from crypto_ws_api.ws_session import get_credentials, UserWSSession

# Can be omitted if you have credentials
_exchange, _test_net, api_key, api_secret, ws_api_endpoint = get_credentials(account_name)

session = aiohttp.ClientSession()

user_session = UserWSSession(
    api_key,
    api_secret,
    session=session,
    endpoint=ws_api_endpoint
)

await user_session.start()
print(f"Operational status: {user_session.operational_status}")

Demo method's calling

await account_information(user_session)

Stop user session and close aiohttp session

await user_session.stop()
await session.close()
print(f"Operational status: {user_session.operational_status}")

Method call example

async def account_information(user_session: UserWSSession):
    # https://developers.binance.com/docs/binance-trading-api/websocket_api#account-information-user_data
    try:
        res = await user_session.handle_request(
            "account.status",
            api_key=True,
            signed=True
        )
        if res is None:
            print("Here handling state Out-of-Service")
    except asyncio.CancelledError:
        pass  # Task cancellation should not be logged as an error
    except Exception as _ex:
        print(f"Handling exception: {_ex}")
    else:
        print(f"Account information (USER_DATA) response: {res}")

Logging setup

For configure logging in multi-module project use next snippet for yours main():

import logging.handlers

logger = logging.getLogger(__name__)
formatter = logging.Formatter(fmt="[%(asctime)s: %(levelname)s] %(message)s")
#
sh = logging.StreamHandler()
sh.setFormatter(formatter)
sh.setLevel(logging.DEBUG)
#
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(sh)

Limits control :link:

Upon reaching the limit threshold of each type, the session switches to the Out-of-Service state. Monitor the values of the variables user_session.operational_status and user_session.order_handling

If you send a request in this state, the answer will be None

In any case, you are protected from exceeding limits and blocking for this reason

Donate

BNB, BUSD, USDT (BEP20) 0x5b52c6ba862b11318616ee6cef64388618318b92

USDT (TRC20) TP1Y43dpY7rrRyTSLaSKDZmFirqvRcpopC

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

crypto-ws-api-2.0.1b2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

crypto_ws_api-2.0.1b2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file crypto-ws-api-2.0.1b2.tar.gz.

File metadata

  • Download URL: crypto-ws-api-2.0.1b2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for crypto-ws-api-2.0.1b2.tar.gz
Algorithm Hash digest
SHA256 b5ff9c1e522e730aaf6975f83aed3b92e47143556227c78ebb18974c285f3dd5
MD5 294e32a9f6e158780fe73b6d61fdb08c
BLAKE2b-256 d3e6bd028a261f56bec0790e0a14c4a394ace4da042580409099ea03f7b70b13

See more details on using hashes here.

File details

Details for the file crypto_ws_api-2.0.1b2-py3-none-any.whl.

File metadata

File hashes

Hashes for crypto_ws_api-2.0.1b2-py3-none-any.whl
Algorithm Hash digest
SHA256 8511695e27d6f1b8fd83b64504576ff6ee58f693d22544347b3d3b32a63d0277
MD5 063f0b5fd24dc9ff46478f7f73ae74f7
BLAKE2b-256 123280a68d817f0221927c1f2422d69884d3f59e7a68667c3d98e6ed0db7fdda

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