Skip to content

Instantly share code, notes, and snippets.

@jleclanche
Created June 8, 2021 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jleclanche/31bb2369b56b0be14cf83d36d414926d to your computer and use it in GitHub Desktop.
Save jleclanche/31bb2369b56b0be14cf83d36d414926d to your computer and use it in GitHub Desktop.
class APIKeyMixin:
@classmethod
def from_env(cls: Type[T], livemode: bool = False) -> T:
key_id = os.environ.get("API_KEY_ID", "")
secret_key = os.environ.get("API_SECRET_KEY", "")
if not key_id or not secret_key:
raise RuntimeError(
"Environment variables `API_KEY_ID` and `API_SECRET_KEY` need to be defined."
)
return cls(key_id, secret_key, livemode=livemode)
def __init__(self, key_id: str, secret_key: str, *, livemode: bool = False):
self.key_id = key_id
self.secret_key = secret_key
self.livemode = livemode
# run with `APCA_API_SECRET_KEY="123" APCA_API_KEY_ID="ABC" PYTHONPATH=. poetry run pytest -vv --run-api-tests`
import pytest
from example import APIClient
def pytest_addoption(parser):
parser.addoption("--run-api-tests", action="store_true", help="run live API tests")
@pytest.fixture(scope="session")
def api_client():
return APIClient.from_env(livemode=False)
def pytest_generate_tests(metafunc):
if "api_client" in metafunc.fixturenames:
if not metafunc.config.getoption("run_api_tests"):
pytest.skip("Skipping API tests")
# run with `API_SECRET_KEY="123" API_KEY_ID="ABC" PYTHONPATH=. poetry run pytest -vv --run-api-tests`
def test_list_accounts(api_client):
resp = api_client.accounts.list()
assert isinstance(resp, list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment