From 9f7944274f86a2820d6e6f67b855aeec9f009a71 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 23 Aug 2024 08:22:46 +0200 Subject: [PATCH] remove old --- service/project.go | 6 --- service/task.go | 48 ------------------- service/todoist.go | 112 --------------------------------------------- 3 files changed, 166 deletions(-) delete mode 100644 service/project.go delete mode 100644 service/task.go delete mode 100644 service/todoist.go diff --git a/service/project.go b/service/project.go deleted file mode 100644 index 7e3624d..0000000 --- a/service/project.go +++ /dev/null @@ -1,6 +0,0 @@ -package service - -type Project struct { - ID string - Name string -} diff --git a/service/task.go b/service/task.go deleted file mode 100644 index f7bddfe..0000000 --- a/service/task.go +++ /dev/null @@ -1,48 +0,0 @@ -package service - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "os" - "time" -) - -type Recurring struct { - Days int `json:"days"` - Start time.Time `json:"start"` - Name string `json:"name"` - Project string `json:"project"` -} - -type Period struct { - Start time.Time `json:"start"` - End time.Time `json:"end"` - Name string `json:"name"` - Project string `json:"project"` -} - -type All struct { - Recurrings []Recurring `json:"recurrings"` - Periods []Period `json:"periods"` -} - -func LoadAll(path string) (All, error) { - file, err := os.Open(path) - if err != nil { - return All{}, fmt.Errorf("could not open file: %w", err) - } - - defer file.Close() - data, err := ioutil.ReadAll(file) - if err != nil { - return All{}, fmt.Errorf("could not read file: %w", err) - } - - all := All{} - if err := json.Unmarshal(data, &all); err != nil { - return All{}, fmt.Errorf("could not parse file: %w", err) - } - - return all, nil -} diff --git a/service/todoist.go b/service/todoist.go deleted file mode 100644 index d4f055e..0000000 --- a/service/todoist.go +++ /dev/null @@ -1,112 +0,0 @@ -package service - -import ( - "fmt" - "io" - "net/http" - "net/url" - "time" -) - -type Todoist struct { - apiKey string - baseURL string - client *http.Client - syncToken string - done chan bool -} - -func NewTodoist(apiKey, baseURL string) *Todoist { - td := &Todoist{ - apiKey: apiKey, - baseURL: baseURL, - done: make(chan bool), - client: http.DefaultClient, - } - - return td -} - -func (td *Todoist) Run() { - ticker := time.NewTicker(time.Second) - for { - select { - case <-td.done: - return - case <-ticker.C: - fmt.Println("hoi") - if err := td.Sync(); err != nil { - fmt.Println(err) - } - } - } -} - -func (td *Todoist) Sync() error { - if td.syncToken == "" { - return td.FullSync() - } - - return nil -} - -func (td *Todoist) FullSync() error { - res := td.do(http.MethodGet) - if res.Error != nil { - return res.Error - } - - return nil -} - -type tdResp struct { - Status int - Body []byte - Error error -} - -func (td *Todoist) do(method string) tdResp { - u, err := url.Parse(fmt.Sprintf("%s/sync/v9/sync", td.baseURL)) - if err != nil { - return tdResp{ - Error: err, - } - } - formData := url.Values{ - "syncToken": {"*"}, - "resource_types": {`["projects"]`}, - } - u.RawQuery = formData.Encode() - req, err := http.NewRequest(method, u.String(), nil) - if err != nil { - return tdResp{ - Error: err, - } - } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", td.apiKey)) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - res, err := td.client.Do(req) - if err != nil { - return tdResp{ - Error: err, - } - } - - if res.StatusCode != http.StatusOK { - return tdResp{ - Error: fmt.Errorf("status code: %d", res.StatusCode), - } - } - - var body []byte - if body, err = io.ReadAll(res.Body); err != nil { - return tdResp{ - Error: err, - } - } - return tdResp{ - Body: body, - } - -}