Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make local setup docs more explicit & apply DRY on http client class #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 11 additions & 19 deletions convoy/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down