2021-11-06 19:06:34 +01:00
|
|
|
import requests
|
2021-11-06 20:07:57 +01:00
|
|
|
import json
|
2021-11-06 20:48:30 +01:00
|
|
|
import uuid
|
2021-11-06 19:06:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TodoistAPI:
|
|
|
|
url = "https://api.todoist.com/rest/v1/"
|
|
|
|
|
|
|
|
def __init__(self, token: str):
|
|
|
|
self.token = token
|
|
|
|
|
2021-12-29 11:10:56 +01:00
|
|
|
def get_get_tasks_by_filter(self, todoist_filter: str):
|
2021-11-06 19:06:34 +01:00
|
|
|
"""
|
|
|
|
Returns all tasks from todoist
|
|
|
|
"""
|
|
|
|
response = requests.get(
|
2021-11-06 20:48:30 +01:00
|
|
|
self.url + 'tasks',
|
2021-11-06 19:06:34 +01:00
|
|
|
headers={'Authorization': 'Bearer ' + self.token},
|
2021-12-29 11:10:56 +01:00
|
|
|
params={"filter": todoist_filter})
|
2021-11-06 19:06:34 +01:00
|
|
|
return response.json()
|
2021-11-06 20:07:57 +01:00
|
|
|
|
2021-11-06 20:48:30 +01:00
|
|
|
def create_tasks(self, tasks: list):
|
2021-11-06 20:07:57 +01:00
|
|
|
"""
|
|
|
|
Adds tasks to todoist
|
|
|
|
"""
|
|
|
|
for task in tasks:
|
2021-11-06 20:48:30 +01:00
|
|
|
requests.post(self.url + 'tasks',
|
|
|
|
headers={
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-Request-Id": str(uuid.uuid4()),
|
|
|
|
"Authorization": "Bearer "
|
|
|
|
+ self.token},
|
2021-11-06 20:07:57 +01:00
|
|
|
data=json.dumps({
|
|
|
|
"content": task["content"],
|
2021-11-06 21:38:11 +01:00
|
|
|
"label_ids": task["label_ids"]
|
2021-11-06 20:07:57 +01:00
|
|
|
}))
|