Skip to content

Instantly share code, notes, and snippets.

@jnalley
Last active September 30, 2020 16:52
Show Gist options
  • Save jnalley/ab96ec26feea64e52b7f149b9637a5c0 to your computer and use it in GitHub Desktop.
Save jnalley/ab96ec26feea64e52b7f149b9637a5c0 to your computer and use it in GitHub Desktop.
Use client.py from cdpcli to make direct API calls
import platform
from cdpcli.client import ClientCreator, Context
from cdpcli.endpoint import EndpointCreator, EndpointResolver
from cdpcli.loader import Loader
from cdpcli.parser import ResponseParserFactory
from cdpcli.retryhandler import create_retry_handler
from cdpcli.translate import build_retry_config
def user_agent_header():
return (
f"Python/{platform.python_version()} "
"{platform.system()}/{platform.release()}"
)
def retry_handler(loader):
original_config = loader.load_json("_retry.json")
retry_config = build_retry_config(
original_config["retry"], original_config.get("definitions", {})
)
return create_retry_handler(retry_config)
class Client:
def __init__(self, endpoint_url=None, tls_verification=True):
self.endpoint_url = endpoint_url
self.tls_verification = tls_verification
loader = Loader()
self.client_creator = ClientCreator(
loader,
Context(),
EndpointCreator(EndpointResolver()),
user_agent_header(),
ResponseParserFactory(),
retry_handler(loader),
)
def __getattr__(self, service_model):
# create client
c = self.client_creator.create_client(
service_model,
self.endpoint_url,
self.tls_verification,
self.client_creator.context.get_credentials(),
)
# set property to allow subsequent calls to reuse the client
setattr(self, service_model, c)
return c
if __name__ == "__main__":
# example usage
client = Client()
result = client.environments.list_environments()
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment