@@ -129,19 +129,50 @@ def get_dashboard_data(self, params={}):
129
129
130
130
return json_response
131
131
132
+ def create_time_entry (self , json_data ):
133
+ """
134
+ creates a time entry. data should have
135
+ this structure
136
+ {
137
+ "time_entry": {
138
+ "description":"<description>",
139
+ "tags":[<list-of-tags>],
140
+ "duration":<duration-in-seconds>,
141
+ "start":"<iso8601-format-datetime>",
142
+ "pid":<project-id>,
143
+ "created_with":"<name-of-your-client-app>"
144
+ }
145
+ }
146
+ """
147
+ response = self .query ("/time_entries" , method = "POST" , json_data = json_data )
148
+
149
+ if response .status_code != requests .codes .ok :
150
+ response .raise_for_status ()
151
+
152
+ response = response .json ()
153
+
154
+ return response
155
+
132
156
def query_report (self , url , params = {}, method = "GET" ):
133
157
return self ._query (self .api_report_base_url , url , params , method )
134
158
135
- def query (self , url , params = {}, method = "GET" ):
136
- return self ._query (self .api_base_url , url , params , method )
159
+ def query (self , url , params = {}, method = "GET" , json_data = {} ):
160
+ return self ._query (self .api_base_url , url , params , method , json_data )
137
161
138
- def _query (self , base_url , url , params , method ):
162
+ def _query (self , base_url , url , params , method , json_data = {} ):
139
163
api_endpoint = base_url + url
140
164
toggl_auth = (self .api_token , "api_token" )
141
165
toggl_headers = {"content-type" : "application/json" }
142
166
143
167
if method == "POST" :
144
- return False
168
+ response = self ._do_post_query (
169
+ api_endpoint ,
170
+ headers = toggl_headers ,
171
+ auth = toggl_auth ,
172
+ params = params ,
173
+ timeout = self .timeout ,
174
+ json_data = json_data ,
175
+ )
145
176
elif method == "GET" :
146
177
response = self ._do_get_query (
147
178
api_endpoint ,
@@ -168,3 +199,16 @@ def _do_get_query(url, headers, auth, params, timeout):
168
199
)
169
200
170
201
return response
202
+
203
+ @staticmethod
204
+ def _do_post_query (url , headers , auth , params , timeout , json_data ):
205
+ response = requests .post (
206
+ url ,
207
+ headers = headers ,
208
+ auth = auth ,
209
+ json = json_data ,
210
+ params = params ,
211
+ timeout = timeout ,
212
+ )
213
+
214
+ return response
0 commit comments