From 895cdc586475b6029a16471a259e627f5dd91cef Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Thu, 17 Oct 2024 07:29:53 +0200 Subject: [PATCH] start sync test --- plan/command/sync.go | 9 +++-- sync/client/client.go | 83 ++----------------------------------------- 2 files changed, 9 insertions(+), 83 deletions(-) diff --git a/plan/command/sync.go b/plan/command/sync.go index 511bb30..1919f1f 100644 --- a/plan/command/sync.go +++ b/plan/command/sync.go @@ -3,6 +3,7 @@ package command import ( "encoding/json" "fmt" + "time" "github.com/urfave/cli/v2" "go-mod.ewintr.nl/planner/item" @@ -22,14 +23,14 @@ var SyncCmd = &cli.Command{ }, } -func NewSyncCmd(client *client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) *cli.Command { +func NewSyncCmd(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) *cli.Command { SyncCmd.Action = func(cCtx *cli.Context) error { return Sync(client, syncRepo, localIDRepo, eventRepo, cCtx.Bool("full")) } return SyncCmd } -func Sync(client *client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event, full bool) error { +func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event, full bool) error { // find local new and updated sendItems, err := syncRepo.FindAll() if err != nil { @@ -45,8 +46,10 @@ func Sync(client *client.Client, syncRepo storage.Sync, localIDRepo storage.Loca return fmt.Errorf("could not clear updated items: %v", err) } + // get last updated time + // get new/updated items - recItems, err := client.Updated(item.KindEvent) + recItems, err := client.Updated([]item.Kind{item.KindEvent}, time.Time{}) if err != nil { return fmt.Errorf("could not receive updates: %v", err) } diff --git a/sync/client/client.go b/sync/client/client.go index cd4cc25..7085971 100644 --- a/sync/client/client.go +++ b/sync/client/client.go @@ -1,89 +1,12 @@ package client import ( - "bytes" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - "strings" "time" "go-mod.ewintr.nl/planner/item" ) -type Client struct { - baseURL string - apiKey string - c *http.Client -} - -func New(url, apiKey string) *Client { - return &Client{ - baseURL: url, - apiKey: apiKey, - c: &http.Client{ - Timeout: 10 * time.Second, - }, - } -} - -func (c *Client) Update(items []item.Item) error { - body, err := json.Marshal(items) - if err != nil { - return fmt.Errorf("could not marhal body: %v", err) - } - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/sync", c.baseURL), bytes.NewReader(body)) - if err != nil { - return fmt.Errorf("could not create request: %v", err) - } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey)) - - res, err := c.c.Do(req) - if err != nil { - return fmt.Errorf("could not make request: %v", err) - } - if res.StatusCode != http.StatusNoContent { - return fmt.Errorf("server returned status %d", res.StatusCode) - } - - return nil -} - -func (c *Client) Updated(ks []item.Kind, ts time.Time) ([]item.Item, error) { - ksStr := make([]string, 0, len(ks)) - for _, k := range ks { - ksStr = append(ksStr, string(k)) - } - u := fmt.Sprintf("%s/sync?ks=%s", c.baseURL, strings.Join(ksStr, ",")) - if !ts.IsZero() { - u = fmt.Sprintf("%s&ts=", url.QueryEscape(ts.Format(time.RFC3339))) - } - req, err := http.NewRequest(http.MethodGet, u, nil) - if err != nil { - return nil, fmt.Errorf("could not create request: %v", err) - } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey)) - - res, err := c.c.Do(req) - if err != nil { - return nil, fmt.Errorf("could not get response: %v", err) - } - if res.StatusCode != http.StatusOK { - return nil, fmt.Errorf("server returned status %d", res.StatusCode) - } - - defer res.Body.Close() - body, err := io.ReadAll(res.Body) - if err != nil { - return nil, fmt.Errorf("could not read response body: %v", err) - } - - var items []item.Item - if err := json.Unmarshal(body, &items); err != nil { - return nil, fmt.Errorf("could not unmarshal response body: %v", err) - } - - return items, nil +type Client interface { + Update(items []item.Item) error + Updated(ks []item.Kind, ts time.Time) ([]item.Item, error) }