todoist_interface/todoist_interface/todoist.py

37 lines
1.1 KiB
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:
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"],
"label_ids": task["label_ids"]
2021-11-06 20:07:57 +01:00
}))