todoist_interface/todoist_interface/todoist.py

37 lines
1021 B
Python
Raw Normal View History

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:
2023-04-06 13:20:34 +02:00
url = "https://api.todoist.com/rest/v2/"
2021-11-06 19:06:34 +01:00
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(
2023-04-06 14:45:33 +02:00
self.url + "tasks",
headers={"Authorization": "Bearer " + self.token},
params={"filter": todoist_filter},
)
2021-11-06 19:06:34 +01:00
return response.json()
2021-11-06 20:07:57 +01:00
2023-04-06 13:40:05 +02:00
def create_tasks(self, tasks: list, labels: list):
2021-11-06 20:07:57 +01:00
"""
Adds tasks to todoist
"""
for task in tasks:
2023-04-06 14:45:33 +02:00
requests.post(
self.url + "tasks",
headers={
"Content-Type": "application/json",
"X-Request-Id": str(uuid.uuid4()),
"Authorization": "Bearer " + self.token,
},
data=json.dumps({"content": task["content"], "labels": labels}),
)