Created
June 8, 2021 18:59
-
-
Save jleclanche/31bb2369b56b0be14cf83d36d414926d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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