marketstack-python
Inofficial Python OpenAPI implementation of the marketstack API based on the marketstack OpenAPI spec.
Usage
Configuration (recommended)
Set up your API key and TLS support as ENV variables.
MARKETSTACK_API_KEY="xyz"
MARKETSTACK_TLS_SUPPORT="1"
Create your client
from marketstack.client import Client
import os
def create_client() -> tuple[Client, str]:
access_key = os.getenv("MARKETSTACK_API_KEY")
assert access_key is not None and len(access_key) > 0, "Environment variable MARKETSTACK_API_KEY is not defined"
tls_support = os.getenv("MARKETSTACK_TLS_SUPPORT")
protocol = "https" if tls_support == "1" else "http"
client = Client(base_url=f"{protocol}://api.marketstack.com/v1")
return client, access_key
Call operations
from marketstack.api.eod import eod
client, access_key = create_client()
response = eod.sync(
client=client,
access_key=access_key,
symbols="AAPL,AMZN",
limit=10,
)
All endpoint features are implemented and tested. For examples see the repository's tests/ directory.
Multiple asynchronous calls
import asyncio
from typing import List
from marketstack.api.intraday import intraday_latest
from marketstack.api.exchanges import exchanges
from marketstack.models import PagedResponseListmodelsIntervalPrice, PagedResponseListmodelsExchange
async def load_async(symbols: List[str]):
client, access_key = create_client()
prices_call = intraday_latest.asyncio(access_key=access_key, client=client, symbols=",".join(symbols))
exchanges_call = exchanges.asyncio(access_key=access_key, client=client)
# Type hints for future results
prices_response: PagedResponseListmodelsIntervalPrice
exchanges_response: PagedResponseListmodelsExchange
prices_response, exchanges_response = await asyncio.gather(prices_call, exchanges_call)
Error handling
from marketstack.models import ErrorCode, ErrorResponse
if isinstance(response, ErrorResponse):
assert response.error.code == ErrorCode.FUNCTION_ACCESS_RESTRICTED
Map to Pandas dataframe
import pandas as pd
df = pd.DataFrame(map(lambda x: x.__dict__, response.data))
Developers area
Generate the client
- Install the OpenAPI client generator
pip install openapi-python-client
- Regenerate the client
./regenerate.sh
Run the tests
- Setup your marketstack API key in
tests/test.env - Run the tests via pytest
PYTHONPATH="." pytest --cov=marketstack tests/
Release update
- Update version in
setup.py - Package library
python setup.py sdist
- Publish library
twine upload dist/marketstack-[version].tar.gz
References
Metadata
Release files for marketstack 0.6.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| marketstack-0.6.2.tar.gz | 30.5 kB | Details |
Release files / marketstack-0.6.2.tar.gz
| Download URL | marketstack-0.6.2.tar.gz |
|---|---|
| Size | 30.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
93c97516a9f7e89d9979014f24f49f4b5c2b0e74ae7472283dc6f820a821593d
|
|
BLAKE2b-256 checksum How to use checksums |
d7feba48600d8dcd3d54b74b970e7f604875e3b1addb5ff64a551d407f9877a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.8
|