I have a simple SDK in Python for internal use. It's basically 150~200 lines of code of simple API calls wrapped around nicely that is comfortable for us to use.
It holds a few clients with methods (and each method wraps some API calls).
So a proper usage would be:
from datatube.datatube import Datatube
client = Datatube()
client.do_stuff(...)
Currently it expects 3 environment variables - two for authentication purposes (these are the secret ones) and one for the environment (dev/stg/prod). I want to change the environment variable to be passed on as an argument (seems better). So:
from datatube.datatube import Datatube
client = Datatube('stg')
client.do_stuff(...)
Inside Datatube
I'll take the correct config based on the environment.
Currently I have:
class BaseConfig:
pass
class DevelopmentConfig(BaseConfig):
DATATUBE_URL = 'https://..'
class StagingConfig(BaseConfig):
DATATUBE_URL = 'https://..'
class ProductionConfig(BaseConfig):
DATATUBE_URL = 'https://..'
def get_config(env: str):
env = env.lower()
if env == 'dev':
return DevelopmentConfig()
if env == 'stg':
return StagingConfig()
elif env == 'prod':
return ProductionConfig()
else:
raise KeyError(f"unsupported env: {env}. supported environments: dev/stg/prod")
If I'll use type annotation on get_config
like:
def get_config(env: str) -> BaseConfig
Then the IDE won't like it (I guess because BaseConfig
is empty, but I don't know how to do it nicely - isn't an abstract class kind of an overkill?)
Any recommendations would be helpful.
Using Python 3.9
1 Answer 1
Class inheritance is overkill here. Just use dataclass instances:
from dataclasses import dataclass
@dataclass
class Config:
DATATUBE_URL: str
ENVIRONMENTS = {
'dev': Config(
DATATUBE_URL='https://..',
),
'stg': Config(
DATATUBE_URL='https://..',
),
'prd': Config(
DATATUBE_URL='https://..',
),
}
def get_config(env_name: str) -> Config:
env = ENVIRONMENTS.get(env_name.lower())
if env is None:
raise KeyError(
f"unsupported env: {env_name}. supported environments: " +
', '.join(ENVIRONMENTS.keys()))
return env