tdapi

package module
v0.0.0-...-e09a7be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

tdapi

tdapi provides a Go interface for the Todoist REST API.

This package and any related application is not created by, affiliated with, or supported by Doist.

This is still a work in progress, but does have some working examples. I use ListTasks as an easy way to extract data from ToDoist.

In order to use this package, you must register an application in the Todoist App Management console. Use https://example.com/redirect as the OAuth redirect URL or adjust the source code to match the URL you entered.

In order to run the examples, you must set the TDCLIENTID and TDCLIENTSECRET environmental variables to the Client ID and Client secret from the app registration.

The current approach assumes the client runs on a host without a browser.

  1. The user is instructed to vist a URL to login and authorize the client.

  2. Once the login is successful, the user must copy the response URL and provide to the client program.

    Note that the default redirect URL https://example.com/redirect uses the special-use example domain and the browser will display a generic message. You must copy the generated URL from the browser address bar into the command line program. The url should look something like https://example.com/redirect?state={characters}&code={characters}. The state and code parameters are used to complete the OAuth access token exchange process in the client program.

By default, a .token.json file is created to store the OAuth2 Access Bearer token.

If you don't want to use OAuth and register an application, you can manually create a .json.token file, which looks like the json file below, with your API token from https://todoist.com/prefs/integrations:

{"access_token":"Replace with your API token","token_type":"Bearer","expiry":"0001-01-01T00:00:00Z"}

DO NOT SHARE YOUR API TOKEN OR THE TOKEN FILE CREATED BY THE APP. ANYONE WITH THE TOKEN HAS ACCESS TO YOUR TODOIST ACCOUNT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ColorToHex = map[string]string{
	"berry_red":   "#b8256f",
	"red":         "#db4035",
	"orange":      "#ff9933",
	"yellow":      "#fad000",
	"olive_green": "#afb83b",
	"lime_green":  "#7ecc49",
	"green":       "#299438",
	"mint_green":  "#6accbc",
	"teal":        "#158fad",
	"sky_blue":    "#14aaf5",
	"light_blue":  "#96c3eb",
	"blue":        "#4073ff",
	"grape":       "#884dff",
	"violet":      "#af38eb",
	"lavendar":    "#eb96eb",
	"magenta":     "#e05194",
	"salmon":      "#ff8d85",
	"charcoal":    "#808080",
	"grey":        "#b8b8b8",
	"taupe":       "#ccac93",
}

ColorToHex maps color ids for a project, label, or filter, to a hex value.

View Source
var PriorityToHexColor = [...]string{
	1: "#000000",
	2: "#246fe0",
	3: "#eb8909",
	4: "#d1453b",
}

PriorityToHexColor maps task priority (1=p4, ..., 4=p1) to a hex value.

Functions

func ChildProjectIDs

func ChildProjectIDs(projects []Project) map[string][]string

ChildProjectIDs returns a map of child IDs for each parent ID. An empty parent ID contains all the top level projects.

func GroupTasksByProjectID

func GroupTasksByProjectID(tasks []Task) map[string][]Task

GroupTasksByProjectID groups Tasks by their Project ID.

func PrettyPrintJson

func PrettyPrintJson(src []byte)

func ProjectByID

func ProjectByID(projects []Project) map[string]Project

ProjectByID returns a map to allow lookup of projects by ID.

func VarToJsonString

func VarToJsonString(v interface{}) string

VarToJsonString converts any varaible (interface{}) to an indented JSON string.

Only public (capitalized) fields will be visible.

func WriteContentToFile

func WriteContentToFile(content []byte, fileName string) (err error)

WriteContent writes the []byte content to a file

Types

type APIErrorResponse

type APIErrorResponse struct {
	Err string
}

APIErrorResponse contains a single property named error.

func (*APIErrorResponse) Error

func (e *APIErrorResponse) Error() string

Error return a string representation of the error

type Comment

type Comment struct {
	// Comment id.
	ID int64 `json:"id"`

	// Comment�s task id (for task comments).
	TaskID int64 `json:"task_id,omitempty"`

	// Comment�s project id (for project comments).
	ProjectID int64 `json:"project_id,omitempty"`

	// Date and time when comment was added, RFC3339 format in UTC.
	Posted time.Time `json:"posted"`

	// Comment content.
	Content string `json:"content"`

	// Attachment file (optional).
	Attachment struct {
		// The name of the file.
		FileName string `json:"file_name,omitempty"`

		// MIME type (i.e. text/plain, image/png).
		FileType string `json:"file_type,omitempty"`

		// The URL where the file is located (a string value
		// representing an HTTP URL). Note that we don�t cache
		// the remote content on our servers and stream or expose
		// files directly from third party resources. In particular
		// this means that you should avoid providing links to
		// non-encrypted (plain HTTP) resources, as exposing this
		// files in Todoist may issue a browser warning.
		FileURL string `json:"file_url,omitempty"`

		FileSize    int64  `json:"file_size,omitempty"`
		UploadState string `json:"upload_state,omitempty"`

		ResourceType string `json:"resource_type"`

		SiteName    string `json:"site_name,omitempty"`
		Description string `json:"description,omitempty"`
		Title       string `json:"title,omitempty"`
		URL         string `json:"url,omitempty"`
		FavIcon     string `json:"favicon,omitempty"`
	} `json:"attachment"`
}

type PersonalLabel

type PersonalLabel struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Color      string `json:"color"`
	Order      int    `json:"order"`
	IsFavorite bool   `json:"is_favorite"`
}

A PersonalLabel represents a Todoist personal label. See https://developer.todoist.com/rest/v2/?shell#labels for more details.

type Project

type Project struct {
	ID             string  `json:"id"`
	Name           string  `json:"name"`
	Color          string  `json:"color"`
	ParentID       *string `json:"parent_id"`
	Order          int     `json:"order"`
	CommentCount   int     `json:"comment_count"`
	IsShared       bool    `json:"is_shared"`
	IsFavorite     bool    `json:"is_favorite"`
	IsInboxProject bool    `json:"is_inbox_project"`
	IsTeamInbox    bool    `json:"is_team_inbox"`
	ViewStyle      string  `json:"view_style"`
	URL            string  `json:"url"`
}

A Project represents a Todoist Project. See https://developer.todoist.com/rest/v2/?shell#projects for more details.

type Task

type Task struct {
	ID           string    `json:"id"`
	ProjectID    string    `json:"project_id"`
	SectionID    string    `json:"section_id,omitempty"`
	Content      string    `json:"content"`
	Description  string    `json:"description,omitempty"`
	IsCompleted  bool      `json:"is_completed"`
	Labels       []string  `json:"labels,omitempty"`
	ParentID     string    `json:"parent,omitempty"`
	Order        int       `json:"order"`
	Priority     int       `json:"priority"`
	Due          *TaskDue  `json:"due,omitempty"`
	URL          string    `json:"url,omitempty"`
	CommentCount int       `json:"comment_count,omitempty"`
	CreatedAt    time.Time `json:"created_at"`
	CreatorID    string    `json:"creator_id"`
	AssigneeID   string    `json:"assignee_id,omitempty"`
	AssignerID   string    `json:"assigner_id,omitempty"`
	Duration     int       `json:"duration"`
	DurationUnit string    `json:"duration_unit"`
}

See https://developer.todoist.com/rest/v2/?shell#tasks

type TaskDue

type TaskDue struct {
	String      string     `json:"string"`
	Date        string     `json:"date"`
	IsRecurring bool       `json:"is_recurring"`
	Datetime    *time.Time `json:"datetime,omitempty"`
	Timezone    string     `json:"timezone,omitempty"`
}

See https://developer.todoist.com/rest/v2/?shell#tasks

type TaskParameters

type TaskParameters struct {
	ProjectID string
	SectionID string
	Label     string
	Filter    string
	Lang      string
	IDs       []int64
}

See https://developer.todoist.com/rest/v2/?shell#get-active-tasks

type TodoistClient

type TodoistClient struct {
	// contains filtered or unexported fields
}

TodoistClient is a client connection to the Todoist REST API. See https://developer.todoist.com/rest/v2/#overview

func New

func New(tokenFileName string, id string, secret string, scopes []string) *TodoistClient

New creates an initialized TodoistClient using the token from tokenFileName.

If tokenFileName doesn't exist, then a token is requested and saved in the file.

The current approach assumes the client runs on a host without a browser. The user is instructed to vist a URL to login and authorize the client. Once the login is successful, the user must copy the response URL and provide to the client program.

The token is requested for offline access, which should include a refresh token to allow access for a long period of time.

clientID and clientSecret must be an application registered with the Todoist App Console

func (*TodoistClient) Get

func (c *TodoistClient) Get(urlString string, query url.Values) (body []byte, err error)

Get executes the Todoist REST API call, returning the response body. Query parmeters can be included to specify and control the amount of data returned in a response. See https://developer.todoist.com/rest/v2/#overview

func (*TodoistClient) GetActiveTask

func (c *TodoistClient) GetActiveTask(id int) (response Task, err error)

GetActiveTask returns an active (non-completed) task by id.

func (*TodoistClient) GetActiveTasks

func (c *TodoistClient) GetActiveTasks(p *TaskParameters) (response []Task, err error)

GetActiveTasks returns an array containing all active tasks.

func (*TodoistClient) GetAllPersonalLabels

func (c *TodoistClient) GetAllPersonalLabels() (response []PersonalLabel, err error)

GetAllLabels returns a JSON-encoded array containing all user labels.

func (*TodoistClient) GetAllProjects

func (c *TodoistClient) GetAllProjects() ([]Project, error)

GetProjects returns all user projects.

func (*TodoistClient) GetAllSharedLabels

func (c *TodoistClient) GetAllSharedLabels() (response []string, err error)

GetAllSharedLabels returns a JSON-encoded array containing all shared labels.

func (*TodoistClient) GetPersonalLabel

func (c *TodoistClient) GetPersonalLabel(id string) (response PersonalLabel, err error)

GetPersonalLabel returns a label by ID.

func (*TodoistClient) GetProject

func (c *TodoistClient) GetProject(project_id string) (Project, error)

GetProject returns the project for the given project_id.

func (*TodoistClient) GetTaskComments

func (c *TodoistClient) GetTaskComments(id int64) (response []Comment, err error)

func (*TodoistClient) Put

func (c *TodoistClient) Put(urlString string, query url.Values, data io.Reader) (body []byte, err error)

Put executes the Todist REST API call, returning the response body.

Query parmeters can be included to specify and control the amount of data returned in a response.

Exact query parameters varies from one API operation to another.

More information can be found at https://docs.microsoft.com/en-us/graph/query-parameters

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL