From b5fe6cc310b17c1c0fa1fb5db44aff6b54192b20 Mon Sep 17 00:00:00 2001 From: Tobias Schoknecht Date: Thu, 25 Apr 2024 09:35:46 +0200 Subject: [PATCH 1/2] Adjust code to use v9 API --- command/current.go | 12 ++++---- command/projects.go | 2 +- command/start.go | 12 ++++---- command/stop.go | 7 ++--- command/workspaces.go | 2 +- global.go | 4 +-- lib/client.go | 2 +- lib/project.go | 12 +++++--- lib/time_entry.go | 68 +++++++++++++++++++++++++------------------ local.go | 8 ++--- 10 files changed, 72 insertions(+), 57 deletions(-) diff --git a/command/current.go b/command/current.go index a377fba..c36a7b5 100644 --- a/command/current.go +++ b/command/current.go @@ -6,7 +6,7 @@ import ( "time" "github.com/sachaos/toggl/cache" - "github.com/sachaos/toggl/lib" + toggl "github.com/sachaos/toggl/lib" "github.com/urfave/cli" ) @@ -26,12 +26,12 @@ func (app *App) CmdCurrent(c *cli.Context) error { var project toggl.Project var timeEntry toggl.TimeEntry var workspace toggl.Workspace + var err error timeEntry = cache.GetContent().CurrentTimeEntry if !c.GlobalBool("cache") { - current, err := app.client.GetCurrentTimeEntry() - timeEntry = current.Data + timeEntry, err = app.client.GetCurrentTimeEntry() if err != nil { return err } @@ -42,14 +42,14 @@ func (app *App) CmdCurrent(c *cli.Context) error { if err != nil { return err } - workspace, err = workspaces.FindByID(timeEntry.WID) + workspace, err = workspaces.FindByID(timeEntry.WorkspaceID) - if timeEntry.PID != 0 { + if timeEntry.ProjectID != 0 { projects, err := app.getProjects(c) if err != nil { return err } - project, err = projects.FindByID(timeEntry.PID) + project, err = projects.FindByID(timeEntry.ProjectID) if err != nil { return err } diff --git a/command/projects.go b/command/projects.go index 6d6b3fd..8af8053 100644 --- a/command/projects.go +++ b/command/projects.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/sachaos/toggl/cache" - "github.com/sachaos/toggl/lib" + toggl "github.com/sachaos/toggl/lib" "github.com/spf13/viper" "github.com/urfave/cli" ) diff --git a/command/start.go b/command/start.go index f453418..e153495 100644 --- a/command/start.go +++ b/command/start.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/sachaos/toggl/cache" - "github.com/sachaos/toggl/lib" + toggl "github.com/sachaos/toggl/lib" "github.com/spf13/viper" "github.com/urfave/cli" ) @@ -16,19 +16,19 @@ func (app *App) CmdStart(c *cli.Context) error { } timeEntry.Description = c.Args().First() - timeEntry.WID = viper.GetInt("wid") + timeEntry.WorkspaceID = viper.GetInt("wid") if c.IsSet("project-id") { - timeEntry.PID = c.Int("project-id") + timeEntry.ProjectID = c.Int("project-id") } else if viper.GetInt("pid") != 0 { - timeEntry.PID = viper.GetInt("pid") + timeEntry.ProjectID = viper.GetInt("pid") } - response, err := app.client.PostStartTimeEntry(timeEntry) + newTimeEntry, err := app.client.PostStartTimeEntry(timeEntry) if err != nil { return err } - cache.SetCurrentTimeEntry(response.Data) + cache.SetCurrentTimeEntry(newTimeEntry) cache.Write() return nil diff --git a/command/stop.go b/command/stop.go index b632c43..5132781 100644 --- a/command/stop.go +++ b/command/stop.go @@ -2,15 +2,14 @@ package command import ( "github.com/sachaos/toggl/cache" - "github.com/sachaos/toggl/lib" + toggl "github.com/sachaos/toggl/lib" "github.com/urfave/cli" ) func (app *App) CmdStop(c *cli.Context) error { - current, err := app.client.GetCurrentTimeEntry() - current_time_entry := current.Data + currentTimeEntry, err := app.client.GetCurrentTimeEntry() - err = app.client.PutStopTimeEntry(current_time_entry.ID) + err = app.client.PutStopTimeEntry(currentTimeEntry.WorkspaceID, currentTimeEntry.ID) if err != nil { return err diff --git a/command/workspaces.go b/command/workspaces.go index 710f415..a7488ca 100644 --- a/command/workspaces.go +++ b/command/workspaces.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/sachaos/toggl/cache" - "github.com/sachaos/toggl/lib" + toggl "github.com/sachaos/toggl/lib" "github.com/spf13/viper" "github.com/urfave/cli" ) diff --git a/global.go b/global.go index 6800b3f..502576b 100644 --- a/global.go +++ b/global.go @@ -12,11 +12,11 @@ func CmdGlobal(c *cli.Context) error { if !c.Args().Present() { return errors.New("Command Failed") } - wid, err := strconv.Atoi(c.Args().First()) + workspaceID, err := strconv.Atoi(c.Args().First()) if err != nil { return err } - viper.Set("wid", wid) + viper.Set("wid", workspaceID) CreateConfig(RootConfigFilePath(), viper.AllSettings()) diff --git a/lib/client.go b/lib/client.go index 2ae72f7..1057c81 100644 --- a/lib/client.go +++ b/lib/client.go @@ -12,7 +12,7 @@ import ( ) const ( - baseURI = "https://api.track.toggl.com/api/v8" + baseURI = "https://api.track.toggl.com/api/v9" retryCount = 3 ) diff --git a/lib/project.go b/lib/project.go index 9bfa94a..79d5ff5 100644 --- a/lib/project.go +++ b/lib/project.go @@ -3,7 +3,7 @@ package toggl import ( "encoding/json" "errors" - "strconv" + "fmt" ) type Project struct { @@ -19,7 +19,7 @@ type Project struct { IsPrivate bool `json:"is_private"` Name string `json:"name"` Template bool `json:"template"` - Wid int `json:"wid"` + WorkspaceID int `json:"workspace_id"` } type Projects []Project @@ -33,10 +33,14 @@ func (repository Projects) FindByID(id int) (Project, error) { return Project{}, errors.New("Find Failed") } -func (cl *Client) FetchWorkspaceProjects(wid int) (Projects, error) { +func (cl *Client) FetchWorkspaceProjects(workspaceID int) (Projects, error) { var projects Projects - res, err := cl.do("GET", "/workspaces/"+strconv.Itoa(wid)+"/projects", nil) + res, err := cl.do( + "GET", + fmt.Sprintf("/workspaces/%d/projects", workspaceID), + nil, + ) if err != nil { return Projects{}, err } diff --git a/lib/time_entry.go b/lib/time_entry.go index 0e9e08c..9bb3e41 100644 --- a/lib/time_entry.go +++ b/lib/time_entry.go @@ -2,7 +2,8 @@ package toggl import ( "encoding/json" - "strconv" + "fmt" + "time" ) type TimeEntry struct { @@ -14,61 +15,72 @@ type TimeEntry struct { ID int `json:"id"` Start string `json:"start"` Tags []string `json:"tags"` - UID int `json:"uid"` - PID int `json:"pid"` - WID int `json:"wid"` -} - -type CurrentResponse struct { - Data TimeEntry `json:"data"` + UserID int `json:"user_id"` + ProjectID int `json:"project_id"` + WorkspaceID int `json:"workspace_id"` } func (timeEntry TimeEntry) AddParam() interface{} { - param := make(map[string]map[string]interface{}) - param["time_entry"] = make(map[string]interface{}) - if timeEntry.PID != 0 { - param["time_entry"]["pid"] = timeEntry.PID + param := make(map[string]interface{}) + if timeEntry.ProjectID != 0 { + param["project_id"] = timeEntry.ProjectID } - param["time_entry"]["wid"] = timeEntry.WID - param["time_entry"]["description"] = timeEntry.Description - param["time_entry"]["created_with"] = "sachaos/toggl" + param["start"] = time.Now().UTC().Format(time.RFC3339) + param["duration"] = timeEntry.Duration + param["workspace_id"] = timeEntry.WorkspaceID + param["description"] = timeEntry.Description + param["created_with"] = "sachaos/toggl" return param } -func (cl *Client) GetCurrentTimeEntry() (CurrentResponse, error) { - var response CurrentResponse +func (cl *Client) GetCurrentTimeEntry() (TimeEntry, error) { + var response TimeEntry - res, err := cl.do("GET", "time_entries/current", nil) + res, err := cl.do("GET", "me/time_entries/current", nil) if err != nil { - return CurrentResponse{}, err + return TimeEntry{}, err } enc := json.NewDecoder(res.Body) if err := enc.Decode(&response); err != nil { - return CurrentResponse{}, err + return TimeEntry{}, err } return response, nil } -func (cl *Client) PostStartTimeEntry(timeEntry TimeEntry) (response CurrentResponse, err error) { - res, err := cl.do("POST", "time_entries/start", timeEntry.AddParam()) +func (cl *Client) PostStartTimeEntry( + timeEntry TimeEntry, +) (response TimeEntry, err error) { + timeEntry.Duration = -1 + + res, err := cl.do( + "POST", + fmt.Sprintf("/workspaces/%d/time_entries", timeEntry.WorkspaceID), + timeEntry.AddParam(), + ) if err != nil { - return CurrentResponse{}, err + return TimeEntry{}, err } enc := json.NewDecoder(res.Body) if err := enc.Decode(&response); err != nil { - return CurrentResponse{}, err + return TimeEntry{}, err } return response, nil } -func (cl *Client) PutStopTimeEntry(id int) error { - id_string := strconv.Itoa(id) - - _, err := cl.do("PUT", "time_entries/"+id_string+"/stop", nil) +func (cl *Client) PutStopTimeEntry(workspaceID int, timeEntryID int) error { + _, err := cl.do( + "PUT", + fmt.Sprintf( + "/workspaces/%d/time_entries/%d/stop", + workspaceID, + timeEntryID, + ), + nil, + ) return err } diff --git a/local.go b/local.go index e751ee2..b0345cc 100644 --- a/local.go +++ b/local.go @@ -11,18 +11,18 @@ func CmdLocal(c *cli.Context) error { if !c.Args().Present() { return errors.New("Command Failed") } - wid, err := strconv.Atoi(c.Args().First()) + workspaceID, err := strconv.Atoi(c.Args().First()) if err != nil { return err } - var pid int + var projectID int if c.IsSet("project-id") { - pid = c.Int("project-id") + projectID = c.Int("project-id") } - CreateConfig(LocalConfigFilePath(), map[string]int{"wid": wid, "pid": pid}) + CreateConfig(LocalConfigFilePath(), map[string]int{"wid": workspaceID, "pid": projectID}) return nil } From a43fc88b3d9e22e5bc214d27583eeb0c0107284e Mon Sep 17 00:00:00 2001 From: Tobias Schoknecht Date: Mon, 29 Apr 2024 08:03:48 +0200 Subject: [PATCH 2/2] Fix support for stop entry --- lib/time_entry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/time_entry.go b/lib/time_entry.go index 9bb3e41..4bafd0a 100644 --- a/lib/time_entry.go +++ b/lib/time_entry.go @@ -73,7 +73,7 @@ func (cl *Client) PostStartTimeEntry( func (cl *Client) PutStopTimeEntry(workspaceID int, timeEntryID int) error { _, err := cl.do( - "PUT", + "PATCH", fmt.Sprintf( "/workspaces/%d/time_entries/%d/stop", workspaceID,