From f460f76b7e9806d301c80c6645daa25b880e4f7a Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Fri, 18 Jan 2019 11:40:26 -0500 Subject: [PATCH] Improve utils.get_client to supply sane defaults This will make it easier to get started. Change-Id: I46a1944701f544db202b80871e01a382770e2148 --- ara/clients/utils.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ara/clients/utils.py b/ara/clients/utils.py index 1da3e1d..819a033 100644 --- a/ara/clients/utils.py +++ b/ara/clients/utils.py @@ -19,10 +19,16 @@ from ara.clients.http import AraHttpClient from ara.clients.offline import AraOfflineClient -def get_client(client=None, **kwargs): - if client is None or client == "offline": - return AraOfflineClient(**kwargs) - elif client == "http": - return AraHttpClient(**kwargs) - else: - raise ValueError("Unsupported client: %s" % client) +def get_client(client="offline", endpoint="http://127.0.0.1:8000", timeout=30): + """ + Returns a specified client configuration or one with sane defaults. + """ + try: + # fmt: off + return { + "offline": AraOfflineClient(), + "http": AraHttpClient(endpoint=endpoint, timeout=timeout) + }[client] + # fmt: on + except KeyError: + raise ValueError(f"Unsupported API client: {client} (use 'http' or 'offline')")