-
Notifications
You must be signed in to change notification settings - Fork 1
Add config options to python client #5
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
Changes from all commits
aa8e608
d43dedf
d2fda7a
05084cf
afd9d75
8c59aeb
f657497
5421503
26c64f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| 1.1.0 October 4, 2016 | ||
| - Added config options: hostname, port, secure, timeout | ||
| 1.0.2 August 11, 2016 | ||
| - Initial Release |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| from ..error import ButtonClientError | ||
| from ..version import VERSION | ||
| from ..request import request | ||
| from ..request import request_url | ||
| from ..request import HTTPError | ||
|
|
||
| USER_AGENT = 'pybutton/{0} python/{1}'.format(VERSION, python_version()) | ||
|
|
@@ -26,15 +27,23 @@ class Resource(object): | |
| api_key (string): Your organization's API key. Do find yours at | ||
| https://app.usebutton.com/settings/organization. | ||
|
|
||
| config (dict): Configuration options for the client. Options include: | ||
| hostname: Defaults to api.usebutton.com. | ||
| port: Defaults to 443 if config.secure, else defaults to 80. | ||
| secure: Whether or not to use HTTPS. Defaults to True. | ||
| timeout: The time in seconds for network requests to abort. | ||
| Defaults to None. | ||
| (N.B: Button's API is only exposed through HTTPS. This option is | ||
| provided purely as a convenience for testing and development.) | ||
|
|
||
| Raises: | ||
| pybutton.ButtonClientError | ||
|
|
||
| ''' | ||
|
|
||
| API_BASE = 'https://api.usebutton.com' | ||
|
|
||
| def __init__(self, api_key): | ||
| def __init__(self, api_key, config): | ||
| self.api_key = api_key | ||
| self.config = config | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even though you're going to end up repeating it a lot, I think a docstring here explaining
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally. Added config to the docstring above. |
||
|
|
||
| def api_get(self, path): | ||
| '''Make an HTTP GET request | ||
|
|
@@ -91,7 +100,12 @@ def _api_request(self, path, method, data=None): | |
|
|
||
| ''' | ||
|
|
||
| url = '{0}{1}'.format(self.API_BASE, path) | ||
| url = request_url( | ||
| self.config['secure'], | ||
| self.config['hostname'], | ||
| self.config['port'], | ||
| path | ||
| ) | ||
| api_key_bytes = '{0}:'.format(self.api_key).encode() | ||
| authorization = b64encode(api_key_bytes).decode() | ||
|
|
||
|
|
@@ -101,7 +115,14 @@ def _api_request(self, path, method, data=None): | |
| } | ||
|
|
||
| try: | ||
| resp = request(url, method, headers, data).get('object', {}) | ||
| resp = request( | ||
| url, | ||
| method, | ||
| headers, | ||
| data, | ||
| self.config['timeout'] | ||
| ).get('object', {}) | ||
|
|
||
| return Response(resp) | ||
| except HTTPError as e: | ||
| response = e.read() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| VERSION = '1.0.2' | ||
| VERSION = '1.1.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you include
from pybutton import Client? I'm trying to keep all code snippets atomically copy+pasteable