Skip to content

Commit

Permalink
chore: remove dead and redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoheikkila committed May 13, 2024
1 parent 95c964b commit c009110
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 118 deletions.
5 changes: 0 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"log"
"os"

"encoding/json"
Expand Down Expand Up @@ -34,14 +33,12 @@ func LoadConfig(file string) (config *Config, err error) {

configFile, err := os.Open(file)
if err != nil {
log.Printf("OPEN FILE ERROR: %v\n", err.Error())
return config, err
}

defer func() {
closeErr := configFile.Close()
if closeErr != nil {
log.Printf("CLOSE FILE ERROR: %v\n", closeErr.Error())
if err == nil {
err = closeErr
}
Expand All @@ -50,9 +47,7 @@ func LoadConfig(file string) (config *Config, err error) {

confJSONParser := json.NewDecoder(configFile)
err = confJSONParser.Decode(&config)

if err != nil {
log.Printf("JSON DECODE ERROR: %v\n", err.Error())
return config, err
}

Expand Down
10 changes: 5 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
)

func addParamsToURL(baseURL string, opt interface{}) (string, error) {
url, err := url.Parse(baseURL)
parsedUrl, err := url.Parse(baseURL)
if err != nil {
return baseURL, err
}

vs, err := query.Values(opt)
values, err := query.Values(opt)
if err != nil {
return baseURL, err
}

url.RawQuery = vs.Encode()
return url.String(), nil
parsedUrl.RawQuery = values.Encode()
return parsedUrl.String(), nil
}

func (h *Harvest) getURL(method string, url string) ([]byte, error) {
func (h *Harvest) getURL(url string) ([]byte, error) {
Client := &http.Client{}

req, _ := http.NewRequest("GET", url, nil)
Expand Down
4 changes: 2 additions & 2 deletions hours.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (e *TimeEntries) TotalHours() float64 {
}

// Total counts total logged hours from the TimeEntries struct
func (e TimeEntries) Total() float64 {
func (e *TimeEntries) Total() float64 {
var hours float64
for _, v := range e.Entries {
hours = hours + v.Hours
Expand All @@ -46,7 +46,7 @@ func (e TimeEntries) Total() float64 {
// Will output hours, saldo and overtime.
// Hours is logged hours excluding spent flexitime.
// Saldo is spent flexitime for that day.
// Overtime is overtime for that day.
// Overtime is extra time worked for that day.
func (e *TimeEntries) DailyTotals(daySelector time.Time) (hours float64, saldo float64, overtime float64) {
// var selection Entries

Expand Down
98 changes: 8 additions & 90 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@ import (
"github.com/polarsquad/harvest/structs"
)

// VERSION v0.0.2
const VERSION = "v0.0.2"

// Harvest API URLs
const (
timeEntriesURL string = "https://api.harvestapp.com/v2/time_entries"
timeEntryByIDURL string = "https://api.harvestapp.com/v2/time_entries/"
userMeURL string = "https://api.harvestapp.com/v2/users/me"
usersURL string = "https://api.harvestapp.com/v2/users"
userByIDURL string = "https://api.harvestapp.com/v2/users/"
projectsURL string = "https://api.harvestapp.com/v2/projects"
tasksURL string = "https://api.harvestapp.com/v2/tasks"
timeEntriesURL string = "https://api.harvestapp.com/v2/time_entries"
usersURL string = "https://api.harvestapp.com/v2/users"
)

var (
Expand All @@ -27,72 +19,32 @@ var (

// Harvest creates the struct for the API, User and Entries
type Harvest struct {
// API *structs.API
API *API
Env *Env
User *User
// Users *Users
// Project string
// TimeEntries *TimeEntries
}

// HarvestOLD creates the struct for the API, User and Entries
type HarvestOLD struct {
API *structs.API
User *User
// Users *Users
// Project string
// TimeEntries *TimeEntries
}

// type api structs.API

// TimeEntries ...
type TimeEntries structs.TimeEntries

// Entries ...
type Entries []structs.Entries

// type entries structs.Entries

// User ...
type User structs.User

// type Users structs.UserList
// Users ...
type Users struct {
Users []User `json:"users"`
}

// type Users []User

// type Users []structs.User

// Links ...
type Links structs.Links

// GetTimeEntriesParams ...
type GetTimeEntriesParams struct {
From string `url:"from"`
To string `url:"to"`
UserID int64 `url:"user_id"`
// ClientID string `url:"client_id"`
// ProjectID string `url:"project_id"`
// IsBilled bool `url:"is_billed"`
// Page int16 `url:"page"`
PerPage int8 `url:"per_page"`
From string `url:"from"`
To string `url:"to"`
UserID int64 `url:"user_id"`
PerPage int8 `url:"per_page"`
}

// API is something...
type API structs.API

// Env is something...
type Env structs.Env

// Config is ...
type Config config.Config

// InitHarvest methot initializes the data structure needed for Harvest
// InitHarvest method initializes the data structure needed for Harvest
func InitHarvest(conf *Config) *Harvest {
a := &API{
AuthToken: conf.API.AuthToken,
Expand All @@ -103,38 +55,18 @@ func InitHarvest(conf *Config) *Harvest {
DateFormatter: conf.Env.DateFormatter,
FlexitimeIDs: conf.Env.FlexitimeIDs,
}
// a := &structs.API{
// AuthToken: conf.API.AuthToken,
// AccountID: conf.API.AccountID,
// }

// e := &TimeEntries{}
// u := &Users{}

H := &Harvest{
API: a,
User: &User{},
Env: e,
// Users: u,
// Project: "",
// TimeEntries: e,
}

// API.AccountID = conf.API.AccountID
// API.AuthToken = conf.API.AuthToken
// API.BaseURL = conf.API.BaseURL
// h := &Harvest{
// User: "Mika",
// Projects: "Client",
// Entries: []TimeEntries{},
// }

return H
}

// Init methot initializes the data structure needed for Harvest
// Init method initializes the data structure needed for Harvest
func Init(conf *config.Config) *Harvest {
// a := &structs.API{
a := &API{
AuthToken: conf.API.AuthToken,
AccountID: conf.API.AccountID,
Expand All @@ -144,28 +76,14 @@ func Init(conf *config.Config) *Harvest {
DateFormatter: conf.Env.DateFormatter,
FlexitimeIDs: conf.Env.FlexitimeIDs,
}
// e := &TimeEntries{}
// u := &Users{}

H := &Harvest{
API: a,
Env: e,
User: &User{},
// Users: u,
// Project: "",
// TimeEntries: e,
}

flexitimeIDs = H.Env.FlexitimeIDs

// API.AccountID = conf.API.AccountID
// API.AuthToken = conf.API.AuthToken
// API.BaseURL = conf.API.BaseURL
// h := &Harvest{
// User: "Mika",
// Projects: "Client",
// Entries: []TimeEntries{},
// }

return H
}
7 changes: 3 additions & 4 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type UserList struct {
Users []User `json:"users"`
}

// User is a object of a user fetched from REST API
// User is an object of a user fetched from REST API
type User struct {
ID int64 `json:"id"`
Email string `json:"email"`
Expand All @@ -31,7 +31,7 @@ type User struct {
CreatedAt string `json:"created_at"`
}

// List of entries fetched from API
// TimeEntries – lists of entries fetched from API
type TimeEntries struct {
TotalEntries int64 `json:"total_entries"`
TotalPages int64 `json:"total_pages"`
Expand All @@ -40,15 +40,14 @@ type TimeEntries struct {
DailyTotal []dailyHours
}

// Time Entries paging links
// Links – paging links
type Links struct {
First string `json:"first"`
Last string `json:"last"`
Next string `json:"next"`
Previous string `json:"previous"`
}

// type Entries map[string]interface{}
type Entries struct {
Billable bool `json:"billable"`
Client EntriesClient `json:"client"`
Expand Down
13 changes: 4 additions & 9 deletions time_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ import (
func (h *Harvest) GetEntries(from time.Time, to time.Time, u *User) *TimeEntries {
var times TimeEntries

// Let's build the URL with parameters.
params := GetTimeEntriesParams{
UserID: int64(u.ID),
UserID: u.ID,
From: from.Format("2006-01-02"),
PerPage: 10,
// IsBilled: true,
To: to.Format("2006-01-02"),
To: to.Format("2006-01-02"),
}

urlWithParams, _ := addParamsToURL(timeEntriesURL, &params)
body, err := h.getURL("GET", urlWithParams)
body, err := h.getURL(urlWithParams)
if err != nil {
log.Fatalln(err.Error())
}
Expand All @@ -40,10 +38,7 @@ func (h *Harvest) GetEntries(from time.Time, to time.Time, u *User) *TimeEntries

log.Printf("TotalEntries: %v, Pages: %v\n", times.TotalEntries, times.TotalPages)

// IF: returned entries have Next link, then we fetch the additional pages
if times.Links.Next != "" {
// Then call getAllEntries
// time.Sleep(1 * time.Second) // This might be needed, if Harvest API starts throttling
entries := h.getAllEntries(times.Links)
times.Entries = append(times.Entries, entries...)
}
Expand All @@ -55,7 +50,7 @@ func (h *Harvest) GetEntries(from time.Time, to time.Time, u *User) *TimeEntries
func (h *Harvest) getAllEntries(l structs.Links) Entries {
var times TimeEntries
// getURL to fetch additional Entries from Links.Next URL
body, _ := h.getURL("GET", l.Next)
body, _ := h.getURL(l.Next)

err := json.Unmarshal(body, &times)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
)

// GetUser Fetches the information of the logged in user
// GetUser Fetches the information of the authenticated user
func (h *Harvest) GetUser() (*User, error) {
url := "https://api.harvestapp.com/v2/users/me"
Client := &http.Client{}
Expand Down Expand Up @@ -48,12 +48,11 @@ func (h *Harvest) GetUser() (*User, error) {
return &user, nil
}

// GetUserByEmail is ...
func (h *Harvest) GetUserByEmail(email string) (*User, error) {
var user User
var usersList Users

body, err := h.getURL("GET", usersURL)
body, err := h.getURL(usersURL)
if err != nil {
log.Fatalf("[ERROR] Could not get users.")
return &user, err
Expand Down

0 comments on commit c009110

Please sign in to comment.