Skip to main content

Inofficial Marketstack OpenAPI Python client

Project description

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

  1. Install the OpenAPI client generator
    pip install openapi-python-client
    
  2. Regenerate the client
    ./regenerate.sh
    

Run the tests

  1. Setup your marketstack API key in tests/test.env
  2. Run the tests via pytest
    PYTHONPATH="." pytest --cov=marketstack tests/
    

Release update

  1. Update version in setup.py
  2. Package library
    python setup.py sdist
    
  3. Publish library
    twine upload dist/marketstack-[version].tar.gz
    

References

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

marketstack-0.6.tar.gz (30.3 kB view details)

Uploaded Source

File details

Details for the file marketstack-0.6.tar.gz.

File metadata

  • Download URL: marketstack-0.6.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for marketstack-0.6.tar.gz
Algorithm Hash digest
SHA256 44fda8731e2f7d59990a578193031cecac7e241d6cf4036769ee95d78282de47
MD5 8523ba101af8cfb7e5c1e2532b8da4d5
BLAKE2b-256 51cc0f875d2a3d7fe6a0b48d7999e92a8325126bfdbe4a43662da8c9573aaef1

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page