Skip to content

Commit

Permalink
Merge pull request #9 from tobischo/feature/update-to-use-v9-api
Browse files Browse the repository at this point in the history
Adjust code to use v9 API
  • Loading branch information
sachaos authored Apr 29, 2024
2 parents 0a9fe6b + a43fc88 commit d730e48
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 57 deletions.
12 changes: 6 additions & 6 deletions command/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion command/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
12 changes: 6 additions & 6 deletions command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion command/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
baseURI = "https://api.track.toggl.com/api/v8"
baseURI = "https://api.track.toggl.com/api/v9"
retryCount = 3
)

Expand Down
12 changes: 8 additions & 4 deletions lib/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package toggl
import (
"encoding/json"
"errors"
"strconv"
"fmt"
)

type Project struct {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
68 changes: 40 additions & 28 deletions lib/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package toggl

import (
"encoding/json"
"strconv"
"fmt"
"time"
)

type TimeEntry struct {
Expand All @@ -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(
"PATCH",
fmt.Sprintf(
"/workspaces/%d/time_entries/%d/stop",
workspaceID,
timeEntryID,
),
nil,
)

return err
}
8 changes: 4 additions & 4 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit d730e48

Please sign in to comment.