Skip to content

Commit

Permalink
fix: adds pytest for env variable connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 21, 2023
1 parent 8803a31 commit 03b0a1c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 🧰 pymsx - Mosaics AI MSX Client for Python

[![PyPI](https://img.shields.io/pypi/v/pymsx?style=flat-square)](https://pypi.org/project/pymsx/)
[![Interrogate](assets/interrogate_badge.svg)](https://github.com/Mosaics-AI/pymsx)

This repository contains the source code for Mosaics AI's official python client. This client is currently in pre-alpha version, so feedback is always welcome!

Expand Down Expand Up @@ -40,7 +41,6 @@ Example usage:
import os
import pandas as pd
from pymsx.client import MsxClient
from pymsx.exceptions import ApiResponseError

# If no credentials are supplied, then environment variables are required.
email = "help@mosaics.ai"
Expand Down Expand Up @@ -84,7 +84,8 @@ else

Exception handling:
```python

from pymsx.client import MsxClient
from pymsx.exceptions import ApiResponseError, InvalidTokenError

try:
try:
Expand Down
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
boto3==1.26.75 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \
--hash=sha256:0bbc318e8c4a6006de0069b99810780962f53fed591830fee9ab670aa4ec56ef \
--hash=sha256:702efaf333ddd9a1520283a22bd74b6c3a890ab38df5afcf4e821a2f3d707688
botocore==1.29.75 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \
--hash=sha256:b6e50fc7aacdcc7fa345cc2c55f53e61db8165bdca4d8ca57323e22cac2671c6 \
--hash=sha256:eef47ca90d02dbc92296208e24ac5e02bdf5cfa45f10d160fdc19612e141bce2
boto3==1.26.76 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \
--hash=sha256:30c7d967ed1c6b5a05643e42cae9d4d36c3f1cb6782637ddc7007a104cfd9027 \
--hash=sha256:b4c2969b7677762914394b8273cc1905dfe5b71f250741c1a575487ae357e729
botocore==1.29.76 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \
--hash=sha256:70735b00cd529f152992231ca6757e458e5ec25db43767b3526e9a35b2f143b7 \
--hash=sha256:c2f67b6b3f8acf2968eafca06526f07b9fb0d27bac4c68a635d51abb675134a7
certifi==2022.12.7 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \
--hash=sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \
--hash=sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18
Expand Down
18 changes: 13 additions & 5 deletions src/pymsx/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MsxClient:
"""Main class for access to remote msx functionality."""

validated: bool = False
base_url: str = app_config.base_url
base_url: str
org_id: str

def __init__(
Expand All @@ -29,9 +29,12 @@ def __init__(
token: Optional[str] = None,
):
"""Create a connection to org's remote msx instance."""
self.token = token or app_config.token
self.email = email or app_config.email
self.password = password or app_config.password
config = self.config

self.base_url = config.base_url
self.token = token or config.token
self.email = email or config.email
self.password = password or config.password

if self.token is None and self.email is None and self.password is None:
raise ValueError("Either a token or email/password is required.")
Expand All @@ -53,6 +56,11 @@ def __init__(
else:
raise e

@property
def config(self):
"""Get runtime application config."""
return app_config()

# Internal helpers

def __create_http_pool(self):
Expand Down Expand Up @@ -111,7 +119,7 @@ def __get_token(self) -> TokenResponse:

def add_org_header(self, headers: Optional[dict[str, str]]) -> dict[str, str]:
"""Add custom org header to requests."""
org_header = app_config.org_header
org_header = self.config.org_header
return {**(headers or {}), org_header: self.org_id}

def validate_token(self) -> ApiMessage:
Expand Down
15 changes: 12 additions & 3 deletions src/pymsx/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Application wide configuration."""
import os
import tempfile
from dataclasses import dataclass
from typing import Optional
Expand Down Expand Up @@ -28,8 +29,16 @@ class Configuration:
"temp_dir": tempfile.gettempdir(),
"base_url": "https://api.mosaics.ai",
"org_header": "X-Org-Id",
"email": os.getenv("MSX_EMAIL"),
"password": os.getenv("MSX_PASSWORD"),
}

app_config = (
dataconf.multi.dict(configuration_defaults).env(env_prefix).on(Configuration)
)

def app_config(overrides: Optional[dict] = None):
"""Runtime application config."""
return (
dataconf.multi.dict(configuration_defaults)
.env(env_prefix)
.dict(overrides or {})
.on(Configuration)
)
29 changes: 25 additions & 4 deletions tests/test_pymsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
Test package level functionality.
"""

import pytest

from pymsx.client import MsxClient
from pymsx.exceptions import InvalidTokenError

email = "help@mosaics.ai"
password = "$mosaics123"

Expand All @@ -31,6 +27,8 @@

def test_connect_with_credentials():
"""Test client connection with creds."""
from pymsx.client import MsxClient

msx = MsxClient(email=email, password=password)

print("Token: ", msx.token)
Expand All @@ -40,14 +38,37 @@ def test_connect_with_credentials():
assert msx.validated is True


def test_connect_with_env(monkeypatch):
"""Test client connection with env variables."""
with monkeypatch.context() as m:
"""Test client connection use env vars."""
m.setenv("MSX_EMAIL", email)
m.setenv("MSX_PASSWORD", password)

from pymsx.client import MsxClient

msx = MsxClient()

print("Token: ", msx.token)

assert msx.token is not None and len(msx.token) > 0
assert msx.org_id is not None
assert msx.validated is True


def test_incorrect_token():
"""Test incorrect token."""
from pymsx.client import MsxClient
from pymsx.exceptions import InvalidTokenError

with pytest.raises(InvalidTokenError):
_ = MsxClient(token=invalid_token)


def test_health_with_token():
"""Test client health check."""
from pymsx.client import MsxClient

msx = MsxClient(email=email, password=password)

assert msx.org_id is not None
Expand Down

0 comments on commit 03b0a1c

Please sign in to comment.