Skip to content

Commit a7106a3

Browse files
authored
Merge pull request #4 from swappsco/feature/create-entry
adds method to create a time entry
2 parents aee224c + 2dcf798 commit a7106a3

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

toggl/api_client.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,50 @@ def get_dashboard_data(self, params={}):
129129

130130
return json_response
131131

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+
132156
def query_report(self, url, params={}, method="GET"):
133157
return self._query(self.api_report_base_url, url, params, method)
134158

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)
137161

138-
def _query(self, base_url, url, params, method):
162+
def _query(self, base_url, url, params, method, json_data={}):
139163
api_endpoint = base_url + url
140164
toggl_auth = (self.api_token, "api_token")
141165
toggl_headers = {"content-type": "application/json"}
142166

143167
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+
)
145176
elif method == "GET":
146177
response = self._do_get_query(
147178
api_endpoint,
@@ -168,3 +199,16 @@ def _do_get_query(url, headers, auth, params, timeout):
168199
)
169200

170201
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

Comments
 (0)