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

Add support for OAuth2 Bearer tokens #24

Merged
merged 2 commits into from
Sep 24, 2018
Merged
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
49 changes: 30 additions & 19 deletions tes/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class HTTPClient(object):
password = attrib(default=None,
convert=strconv,
validator=optional(instance_of(str)))
token = attrib(default=None,
convert=strconv,
validator=optional(instance_of(str)))

@url.validator
def __check_url(self, attribute, value):
Expand All @@ -40,11 +43,10 @@ def __check_url(self, attribute, value):
)

def get_service_info(self):
kwargs = self._request_params()
response = requests.get(
"%s/v1/tasks/service-info" % (self.url),
timeout=self.timeout,
auth=(self.user, self.password)
)
**kwargs)
raise_for_status(response)
return unmarshal(response.json(), ServiceInfo)

Expand All @@ -53,35 +55,31 @@ def create_task(self, task):
msg = task.as_json()
else:
raise TypeError("Expected Task instance")

kwargs = self._request_params(data=msg)
response = requests.post(
"%s/v1/tasks" % (self.url),
data=msg,
headers={'Content-Type': 'application/json'},
timeout=self.timeout,
auth=(self.user, self.password)
**kwargs
)
raise_for_status(response)
return unmarshal(response.json(), CreateTaskResponse).id

def get_task(self, task_id, view="BASIC"):
req = GetTaskRequest(task_id, view)
payload = {"view": req.view}
kwargs = self._request_params(params=payload)
response = requests.get(
"%s/v1/tasks/%s" % (self.url, req.id),
params=payload,
timeout=self.timeout,
auth=(self.user, self.password)
)
**kwargs)
raise_for_status(response)
return unmarshal(response.json(), Task)

def cancel_task(self, task_id):
req = CancelTaskRequest(task_id)
kwargs = self._request_params()
response = requests.post(
"%s/v1/tasks/%s:cancel" % (self.url, req.id),
timeout=self.timeout,
auth=(self.user, self.password)
)
**kwargs)
raise_for_status(response)
return

Expand All @@ -95,12 +93,10 @@ def list_tasks(self, view="MINIMAL", page_size=None, page_token=None):
)
msg = req.as_dict()

kwargs = self._request_params(params=msg)
response = requests.get(
"%s/v1/tasks" % (self.url),
params=msg,
timeout=self.timeout,
auth=(self.user, self.password)
)
**kwargs)
raise_for_status(response)
return unmarshal(response.json(), ListTasksResponse)

Expand All @@ -122,5 +118,20 @@ def check_success(data):

if max_time is not None and time.time() >= max_time:
raise TimeoutError("last_response: %s" % (response.as_dict()))

time.sleep(0.5)

def _request_params(self, data=None, params=None):
kwargs = {'timeout': self.timeout}

if data:
kwargs['data'] = data
if params:
kwargs['params'] = params
if self.token:
kwargs['headers'] = {'Content-type': 'application/json',
'Authorization': 'Bearer ' + self.token}
else:
kwargs['headers'] = {'Content-type': 'application/json'}
kwargs['auth'] = (self.user, self.password)

return kwargs