Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi.py
More file actions
Latest commit
233 lines (199 loc) · 7.99 KB
/
Copy pathapi.py
File metadata and controls
233 lines (199 loc) · 7.99 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright (c) 2010 AtTask, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
importurllib2
importjson
fromurllibimporturlencode
classStreamClient(object):
GET='GET'
POST='POST'
PUT='PUT'
DELETE='DELETE'
PATH_LOGIN="/login"
PATH_LOGOUT="/logout"
def__init__(self,url):
"""
url -- the full url to the attask api
(http://yourdomain.attask-ondemand.com:8080/attask/api)
"""
self.url=urlifnoturl.endswith('/') elseurl[:-1]
self.handle=None
self.session_id=None
self.user_id=None
deflogin(self,username,password):
"""
logs into attask with username and password
"""
params= {'username':username,
'password':password}
data=self.request(StreamClient.PATH_LOGIN, params, StreamClient.GET)
self.session_id=data['sessionID']
self.user_id=data['userID']
deflogout(self):
"logs out of attask"
self.request(StreamClient.PATH_LOGOUT, None, StreamClient.GET)
self.session_id=self.user_id=None
defget_list(self,objcode,ids,fields=None):
"""
Returns each object by id, similar to calling get for each id individually
objcode -- object type ie. ObjCode.PROJECT
ids -- list of ids to lookup
[fields] -- list of field names to return for each object
"""
path='/%s'%objcode
returnself.request(path,{'ids':','.join(ids)},fields)
defput(self,objcode,objid,params,fields=None):
"""
Updates an existing object, returns the updated object
objcode -- object type ie. ObjCode.PROJECT
objid -- id of object to update
params -- fields to update
[fields] -- list of field names to return for the object
"""
path='/%s/%s'% (objcode, objid)
returnself.request(path,params,StreamClient.PUT,fields)
defpost(self,objcode,params,fields=None):
"""
Creates a new object, returns the new object
objcode -- object type ie. ObjCode.PROJECT
params -- values for object fields
[fields] -- list of field names to return for the object
"""
path='/%s'%objcode
returnself.request(path,params,StreamClient.POST,fields)
defget(self,objcode,objid,fields=None):
"""
Lookup an object by id
objcode -- object type ie. ObjCode.PROJECT
objid -- id to lookup
[fields] -- list of field names to return for each object
"""
path='/%s/%s'% (objcode, objid)
returnself.request(path,None,StreamClient.GET,fields)
defdelete(self,objcode,objid,force=False):
"""
Deletes object with id objid
objcode -- object type ie. ObjCode.PROJECT
objid -- id of object to delete
[force=False] -- force delete objects with relationships,
ie. projects with task
"""
path='/%s/%s'% (objcode, objid)
returnself.request(path,{'force':force},StreamClient.DELETE)
defsearch(self,objcode,params,fields=None):
"""
Search for objects
objcode -- object type ie. ObjCode.PROJECT
params -- name value keys to search for
[fields] -- fields to return for each search result
"""
path='/%s/%s'% (objcode, 'search')
returnself.request(path,params,StreamClient.GET,fields)
defrequest(self,path,params,method,fields=None,raw=False):
"""
Basic api request
path -- api url to open
params -- parameters for request
method -- a request method, StreamClient.GET,POST,PUT,DELETE
[fields] -- added to params as fields to return for request
[raw=False] -- returns the full json object, otherwise returns
the contents of the data json data field
"""
ifnotparams:
params= {}
params['sessionID'] =self.session_id
params['method'] =method
iffields:
params['fields'] =','.join(fields)
dest=self.url+path
try:
response=urllib2.urlopen(dest,urlencode(params))
excepturllib2.URLError, e:
raiseStreamAPIException(e)
data=json.load(response)
returndataifrawelsedata['data']
classStreamAPIException(Exception):
"Raised when a request fails"
classStreamNotModifiedException(Exception):
"Raised when saving an object that has not been modified"
classStreamClientNotSet(Exception):
"""Raised when calling an api method on an object without an
attached StreamClient object
"""
# CRUD wrapper for basic modifications
classAtTaskObject(object):
def__init__(self,data,streamclient=None):
self.__dict__['streamclient'] =streamclient
self.__dict__['data'] =data
self.__dict__['_dirty_fields'] = {}
def__getattr__(self, item):
returnself.__dict__['data'][item]
def__setattr__(self, key, value):
self._dirty_fields[key] =True
self.data[key] =value
def__str__(self):
returnjson.dumps(self.data,indent=4)
defis_modified(self):
"Determines if object has been modified after creation"
returnbool(len(m))
defsave(self):
"""
Persists changes to streamclient instance
raises -- StreamClientNotSet if stream client was not passed in constructor
-- StreamNotModifiedException if no fields have changed
-- StreamAPIException if api call fails
"""
ifnotself.streamclient:
raiseStreamClientNotSet()
params=dict([(key,self.data[key])
forkey,valinself._dirty_fields.iteritems() ifval])
ifnotlen(params):
raiseStreamNotModifiedException("No fields were modified.")
ifself.data.has_key('ID'):
self.__dict__['data'] =self.streamclient.put(self.objCode,self.ID,params,self.data.keys())
else:
self.__dict__['data'] =self.streamclient.post(self.objCode,params,self.data.keys())
self.__dict__['_dirty_fields'] = {}
defdelete(self,streamclient,force=False):
"""
Deletes the current object by id
raises -- StreamClientNotSet if stream client was not passed in constructor
"""
ifnotself.streamclient:
raiseStreamClientNotSet()
returnself.streamclient.delete(self.objCode,self.ID,force)
# Supported object codes
classObjCode:
PROJECT='proj'
TASK='task'
ISSUE='optask'
TEAM='team'
HOUR='hour'
TIMESHEET='tshet'
USER='user'
ASSIGNMENT='assgn'
USER_PREF='userpf'
CATEGORY='ctgy'
CATEGORY_PARAMETER='ctgypa'
PARAMETER='param'
PARAMETER_GROUP='pgrp'
PARAMETER_OPTION='popt'
PARAMETER_VALUE='pval'
ROLE='role'
GROUP='group'
NOTE='note'
DOCUMENT='docu'
DOCUMENT_VERSION='docv'
EXPENSE='expns'
CUSTOM_ENUM='custem'