remove old

This commit is contained in:
Erik Winter 2024-08-23 08:22:46 +02:00
parent b667cb1616
commit 9f7944274f
3 changed files with 0 additions and 166 deletions

View File

@ -1,6 +0,0 @@
package service
type Project struct {
ID string
Name string
}

View File

@ -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
}

View File

@ -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,
}
}