mem projects

This commit is contained in:
Erik Winter 2024-08-16 13:25:06 +01:00
parent d65ee1eb65
commit 16547ab7b8
9 changed files with 188 additions and 40 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module code.ewintr.nl/hatd-planner
module code.ewintr.nl/planner
go 1.21.5

View File

@ -1,8 +1,6 @@
package main
import "os"
func main() {
td := NewTodoist(os.Getenv("TODOIS_API_TOKEN"))
td.Run()
// td := NewTodoist(os.Getenv("TODOIST_API_TOKEN"), "https://api.todoist.com")
// td.Run()
}

33
service/memory.go Normal file
View File

@ -0,0 +1,33 @@
package service
type Memory struct {
projects map[string]Project
}
func NewMemory() *Memory {
return &Memory{
projects: make(map[string]Project),
}
}
func (m *Memory) StoreProject(project Project) error {
m.projects[project.ID] = project
return nil
}
func (m *Memory) FindProject(id string) (Project, error) {
project, ok := m.projects[id]
if !ok {
return Project{}, ErrNotFound
}
return project, nil
}
func (m *Memory) FindAllProjects() ([]Project, error) {
projects := make([]Project, 0, len(m.projects))
for _, p := range m.projects {
projects = append(projects, p)
}
return projects, nil
}

20
service/memory_test.go Normal file
View File

@ -0,0 +1,20 @@
package service_test
import (
"testing"
"code.ewintr.nl/planner/service"
)
func TestMemoryProjects(t *testing.T) {
t.Parallel()
mem := service.NewMemory()
actProjects, actErr := mem.FindAllProjects()
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if len(actProjects) != 0 {
t.Errorf("exp 0, got %d", len(actProjects))
}
}

6
service/project.go Normal file
View File

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

13
service/storage.go Normal file
View File

@ -0,0 +1,13 @@
package service
import "errors"
var (
ErrNotFound = errors.New("not found")
)
type Repository interface {
FindProject(id string) (Project, error)
FindAllProjects() ([]Project, error)
StoreProject(project Project) error
}

View File

@ -1,4 +1,4 @@
package task
package service
import (
"encoding/json"

112
service/todoist.go Normal file
View File

@ -0,0 +1,112 @@
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,
}
}

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"net/http"
"time"
)
type Todoist struct {
apiKey string
client http.Client
done chan bool
}
func NewTodoist(apiKey string) *Todoist {
td := &Todoist{
apiKey: apiKey,
done: make(chan bool),
}
return td
}
func (td *Todoist) Run() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-td.done:
return
case <-ticker.C:
fmt.Println("hoi")
}
}
}