-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.py
192 lines (151 loc) · 4.96 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The GCI API Client. A thin wrapper around the GCI API.
Exmple usage:
client = gciclient.GCIAPIClient(
auth_token='xxxxxxxxxxxxxx',
url_prefix='https://codein.withgoogle.com',
debug=False)
client.NewTask(myTaskDict)
"""
import json
import logging
try:
import urlparse as up # Python 2.7
except:
import urllib.parse as up # Python 3
import requests
class GCIAPIClient(object):
"""GCIAPIClient provides a thin wrapper around the GCI Task API.
A GCIAPIClient simplifies working with tasks by forming the HTTP requests on
behalf of the caller.
Attributes:
url_prefix: A string prefix for the codin URL
headers: A dictionary of HTTP headers
"""
def __init__(self, auth_token=None,
url_prefix='https://codein.withgoogle.com/',
debug=False):
self.url_prefix = up.urljoin(url_prefix, 'api/program/current/')
self.headers = {
'Authorization': 'Bearer %s' % auth_token,
'Content-Type': 'application/json',
}
if debug:
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger('requests.packages.urllib3')
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def _Url(self, path):
return up.urljoin(self.url_prefix, path) + '/'
def ListTasks(self, page=1):
"""Fetches a list of tasks.
Args:
page: Which page of results to return.
Returns:
A JSON encoded list of tasks.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = requests.get(self._Url('tasks'), headers=self.headers,
params={'page': page})
r.raise_for_status()
return r.json()
def GetTask(self, task_id):
"""Fetches a single task.
Args:
task_id: An integer id for the task.
Returns:
A JSON encoded task.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = requests.get(self._Url('tasks/%d' % task_id), headers=self.headers)
r.raise_for_status()
return r.json()
def NewTask(self, task):
"""Creates a single new task.
Args:
task: A task object.
Returns:
A JSON encoded response.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = requests.post(self._Url('tasks'), headers=self.headers,
data=json.dumps(task))
r.raise_for_status()
return r.json()
def UpdateTask(self, task_id, task):
"""Modifies a single task.
Args:
task_id: An integer id for the task.
task: A task object.
Returns:
A JSON encoded response.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = (
requests.put(
self._Url('tasks/%d' % task_id), data=json.dumps(task),
headers=self.headers))
r.raise_for_status()
return r.json()
def DeleteTask(self, task_id):
"""Deletes a single task.
Args:
task_id: An integer id for the task.
Returns:
A JSON encoded response, if there is content in the response.
Otherwise None.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = (
requests.delete(
self._Url('tasks/%d' % task_id),
headers=self.headers))
r.raise_for_status()
# DELETE returns nothing on success, don't try and parse it.
if r.content:
return r.json()
return
def ListTaskInstances(self, page=1):
"""Fetches a list of tasks.
Args:
page: Which page of results to return.
Returns:
A JSON encoded list of task instances.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = requests.get(self._Url('instances'), headers=self.headers,
params={'page': page})
r.raise_for_status()
return r.json()
def GetTaskInstance(self, task_instance_id):
"""Fetches a single task.
Args:
task_instance_id: An integer id for the task instance.
Returns:
A JSON encoded task instance.
Raises:
HTTPError: a 4XX client error or 5XX server error response was returned.
"""
r = requests.get(self._Url('instances/%d' % task_instance_id),
headers=self.headers)
r.raise_for_status()
return r.json()