Skip to main content

Stocra.com python sdk

Project description

Stocra Python SDK

Synchronous client

Install

pip install stocra[synchronous]
# or
poetry add stocra --extras synchronous

Usage

from concurrent.futures import ThreadPoolExecutor
from decimal import Decimal

from requests import Session
from requests.adapters import HTTPAdapter
from stocra.synchronous.client import Stocra
from stocra.synchronous.error_handlers import (
  retry_on_too_many_requests, 
  retry_on_service_unavailable,
  retry_on_bad_gateway,
  retry_on_timeout_error,
)

adapter = HTTPAdapter(pool_connections=100, pool_maxsize=100)
session = Session()
session.mount('https://', adapter)
stocra_client = Stocra(
    api_key="<api-key>", # optional
    session=session, # optional
    executor=ThreadPoolExecutor(), # optional
    error_handlers=[ 
        retry_on_service_unavailable,
        retry_on_too_many_requests,
        retry_on_bad_gateway,
        retry_on_timeout_error,
    ] # optional
)

# stream new blocks
for block in stocra_client.stream_new_blocks(blockchain="ethereum"):
    print(block)

# stream new blocks, load new blocks in the background for faster processing. 
# Works only if executor was provided during instantiation.
for block in stocra_client.stream_new_blocks_ahead(blockchain="ethereum", n_blocks_ahead=5):
    print(block)
    
# stream new transactions
for block, transaction in stocra_client.stream_new_transactions(
    blockchain="ethereum", 
    load_n_blocks_ahead=5,  # works only with executor 
):
    print(block.height, transaction.hash)
    
# get one block
block = stocra_client.get_block(blockchain="bitcoin", hash_or_height=57043)

# get one transaction
transaction = stocra_client.get_transaction(
    blockchain="bitcoin", 
    transaction_hash="a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d"
)

# get all transactions in block
transactions = stocra_client.get_all_transactions_of_block(blockchain="bitcoin", block=block) 
for transaction in transactions:
    print(transaction)
    
# scale token value
value = stocra_client.scale_token_value(
    "ethereum", 
    "0xa49ded8b4607f958003e0d87d7f2d2f69bcadd41",  # USDT
    Decimal("34500000000000000000000000") # raw value in token transfer
)

Asynchronous client

Install

pip install stocra[asynchronous]
# or
poetry add stocra --extras asynchronous

Usage

from asyncio import Semaphore
from decimal import Decimal

from aiohttp import ClientSession
from stocra.asynchronous.client import Stocra
from stocra.asynchronous.error_handlers import (
  retry_on_too_many_requests, 
  retry_on_service_unavailable,
  retry_on_bad_gateway,
  retry_on_timeout_error,
)

session = ClientSession()
stocra_client = Stocra(
    api_key="<api-key>", # optional
    session=session, # optional
    semaphore=Semaphore(50), # optional
    error_handlers=[
        retry_on_service_unavailable,
        retry_on_too_many_requests,
        retry_on_bad_gateway,
        retry_on_timeout_error,
    ] # optional
)
# stream new transactions
async for block, transaction in stocra_client.stream_new_transactions(
    blockchain="ethereum", 
    load_n_blocks_ahead=5
):
    print(block.height, transaction.hash)

# stream new blocks and always load next 5 blocks in the background.
# useful when you need to parse multiple blocks in short time span
async for block in stocra_client.stream_new_blocks(blockchain="ethereum", n_blocks_ahead=5):
    print(block)

# get one block
block = await stocra_client.get_block(
    blockchain="bitcoin",
    hash_or_height="00000000152340ca42227603908689183edc47355204e7aca59383b0aaac1fd8"
)

# get one transaction
transaction = await stocra_client.get_transaction(
    blockchain="bitcoin",
    transaction_hash="a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 
)

# get all transactions in block
transactions = stocra_client.get_all_transactions_of_block(blockchain="bitcoin", block=block)
async for transaction in transactions:
    print(transaction)

# scale token value
value = await stocra_client.scale_token_value(
    "ethereum", 
    "0xa49ded8b4607f958003e0d87d7f2d2f69bcadd41",  # USDT
    Decimal("34500000000000000000000000") # raw value in token transfer
)

Error handlers

Error handlers are functions that are called after a request fails. They receive single argument, StocraHTTPError and return boolean indicating whether to retry request (True) or raise (False).

Error handler signature: ErrorHandler = Callable[[StocraHTTPError], Union[bool, Awaitable[bool]]]

No errors handlers are used by default although there are two already defined for both sync and async version:

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

stocra-1.0.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

stocra-1.0.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file stocra-1.0.2.tar.gz.

File metadata

  • Download URL: stocra-1.0.2.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

File hashes

Hashes for stocra-1.0.2.tar.gz
Algorithm Hash digest
SHA256 82cce67349e9052e8d5d7c4e88348d2dcad4093c65ddffaf0109a0d5d714da37
MD5 7bddd6042162fbed38558baf0c1ecedb
BLAKE2b-256 aa9d880c6d0d1b43abaa56f0bb3368f7f236d9a1e4556d538da3ce793115a274

See more details on using hashes here.

File details

Details for the file stocra-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: stocra-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

File hashes

Hashes for stocra-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a47d292037e9ee216eb433c10306180eb767d3ddd9dcd3098b82eeea6bb941
MD5 981062035f02fbaa871f8659a0a669c5
BLAKE2b-256 6da75b4cb96b82f81bda9cbe3d03a7fa0e9834b4efdacc876a3eced651d0033a

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