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"):
    print(block)
    
# stream new transactions
for block, transaction in stocra_client.stream_new_transactions(blockchain="ethereum"):
    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"):
    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.1.tar.gz (8.9 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.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stocra-1.0.1.tar.gz
  • Upload date:
  • Size: 8.9 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.1.tar.gz
Algorithm Hash digest
SHA256 6cf96f266c28297cf4517a8b86a0db7b2905c7d894a8efdd65d6e295a1c0bd4e
MD5 6f28c31c69200c930ee7577f2423a660
BLAKE2b-256 fcff7039c15a0176fc0e298b5e1a46378c55c931d9af815e7886597d4ef8f221

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stocra-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07889ca324d054d1b8f35bff64f0632b0b41f9b6d990799c24d12992f7f2dc93
MD5 bd95c6b48f5240374355db0c2d3ece5e
BLAKE2b-256 990a5b9bd4cc43fe2be3049022f1d98d09cf750f7f56da7ca6b93cf2c49cacfb

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