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

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,
    ] # 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

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,
    ] # 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.0.tar.gz (8.7 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.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stocra-1.0.0.tar.gz
  • Upload date:
  • Size: 8.7 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.0.tar.gz
Algorithm Hash digest
SHA256 d4339f25bdb8c0cc70f7ff070cb8c52c393f2f765d218234c77471d84700d330
MD5 14fe1a1a9a52be38921fb51f8e97fcf9
BLAKE2b-256 799330f4ad0a0052c6e78c40ab38caa4fdaf8a414b2845543f17359fbda64d27

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stocra-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fe9f95fe4908ebcd866e1c7ffdffead236ed5d1de81799b648b702c5392a3a6
MD5 5fcaa4fd90b81af503f0ccd62ad37040
BLAKE2b-256 4c6dedd40e1b33c49113a7f05dbab29dd894aeeb5fc50b50f8aaf78daa26832d

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