From 107a384750e882a8d430b10ba510893caee5191f Mon Sep 17 00:00:00 2001 From: Karl Date: Fri, 23 Jun 2023 15:26:53 -0400 Subject: [PATCH] Update local setup docs & apply DRY on http client class --- README.md | 4 ++-- convoy/client/client.py | 30 +++++++++++------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3f0107c..8dbaf98 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,10 @@ convoy = Convoy({"api_key":"your_api_key", "project_id": "your_project_id"}) ``` The SDK also supports authenticating via Basic Auth by defining your username and password. -In the event you're using a self-hosted convoy instance, you can define the `uri` as part of what is passed into the convoy's constructor. +In the event you're using a self-hosted convoy instance, `uri` should contain the `hostname:port` combination (see example below). ```python -convoy = Convoy({ "api_key": 'your_api_key', "uri": 'self-hosted-instance', "project_id": "your_project_id"}) +convoy = Convoy({ "api_key": 'your_api_key', "uri": 'http://localhost:5005', "project_id": "your_project_id"}) ``` ## Usage diff --git a/convoy/client/client.py b/convoy/client/client.py index 6ffb8bd..a325767 100644 --- a/convoy/client/client.py +++ b/convoy/client/client.py @@ -24,37 +24,29 @@ def __init__(self, config: dict): if config.uri == "": self.base_uri = f"https://dashboard.getconvoy.io/api/v1/projects/{config.project_id}" else: - self.base_uri = config.uri + self.base_uri = f"{config.uri}/api/v1/projects/{config.project_id}" self.headers = {"Authorization": self.get_authorization(), "Content-Type": "application/json; charset=utf-8"} - def http_get(self, path, query): + def _http_request(self, verb, path, query, data=None): try: - response = requests.get(self.build_path(path), headers=self.headers, params=query) + method = getattr(requests, verb) + response = method(self.build_path(path), headers=self.headers, params=query, data=data) return response.json(), response.status_code except BaseException as e: - return response_helper(e) + response_helper(e) + + def http_get(self, path, query): + return self._http_request('get', path, query) def http_post(self, path, query, data): - try: - response = requests.post(self.build_path(path), data=json.dumps(data), headers=self.headers, params=query) - return response.json(), response.status_code - except BaseException as e: - return response_helper(e) + return self._http_request('post', path, query, json.dumps(data)) def http_put(self, path, query, data): - try: - response = requests.put(self.build_path(path), data=json.dumps(data), headers=self.headers, params=query) - return response.json(), response.status_code - except BaseException as e: - return response_helper(e) + return self._http_request('put', path, query, json.dumps(data)) def http_delete(self, path, query, data): - try: - response = requests.delete(self.build_path(path), data=json.dumps(data), headers=self.headers, params=query) - return response.json(), response.status_code - except BaseException as e: - return response_helper(e) + return self._http_request('delete', path, query, json.dumps(data)) def get_base_url(self): return self.base_uri