Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers #14

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package harvest

import (
"io/ioutil"
"net/http"
"net/url"

"github.com/google/go-querystring/query"
)

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

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

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

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

req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Harvest Slack Bot")
req.Header.Set("Harvest-Account-ID", h.API.AccountID)
req.Header.Set("Authorization", "Bearer "+h.API.AuthToken)
req.Header.Set("Content-Type", "application/json")

resp, err := Client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

return body, err
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
// 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"
)

// Harvest creates the struct for the API, User and Entries
type Harvest struct {
API *structs.API
Expand Down
65 changes: 2 additions & 63 deletions time_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,12 @@ import (
"log"

"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/google/go-querystring/query"
// "github.com/google/go-querystring/query"
"github.com/polarsquad/harvest/structs"
)

func addParamsToURL(baseURL string, opt interface{}) (string, error) { //TODO: Move to helpers.go
url, err := url.Parse(baseURL)
if err != nil {
return baseURL, err
}

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

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

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

// req, _ := http.NewRequest("GET", url, nil)
// req.Header.Set("User-Agent", "Harvest Slack Bot")
// req.Header.Set("Harvest-Account-ID", h.API.AccountID)
// req.Header.Set("Authorization", "Bearer "+h.API.AuthToken)
// req.Header.Set("Content-Type", "application/json")

// resp, err := Client.Do(req)
// body, err := ioutil.ReadAll(resp.Body)
// defer resp.Body.Close()

// // var jsonResponse map[string]interface{}

// // json.Unmarshal(body, &jsonResponse)

// return body, err
// }

func (h *Harvest) getURL(method string, url string) ([]byte, error) { //TODO: Move to heplers.go
Client := &http.Client{}

req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Harvest Slack Bot")
req.Header.Set("Harvest-Account-ID", h.API.AccountID)
req.Header.Set("Authorization", "Bearer "+h.API.AuthToken)
req.Header.Set("Content-Type", "application/json")

resp, err := Client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

// var jsonResponse map[string]interface{}

// json.Unmarshal(body, &jsonResponse)

return body, err
}

// GetEntries fetches all the TimeEntries with the provided timespan.
// If from and/or to dates are not defined, it will fetch all the TimeEntries.
//
Expand All @@ -77,9 +19,6 @@ func (h *Harvest) getURL(method string, url string) ([]byte, error) { //TODO: Mo
// to: time.Time with format "2006-01-02"
// u: User, specifies which users TimeEntries are fetched.
func (h *Harvest) GetEntries(from time.Time, to time.Time, u User) *TimeEntries {
// Start with fetching the entries
url := "https://api.harvestapp.com/v2/time_entries"

// Let's build the URL with parameters.
params := GetTimeEntriesParams{
UserID: int64(u.ID),
Expand All @@ -89,7 +28,7 @@ func (h *Harvest) GetEntries(from time.Time, to time.Time, u User) *TimeEntries
To: to.Format("2006-01-02"),
}

urlWithParams, _ := addParamsToURL(url, &params)
urlWithParams, _ := addParamsToURL(timeEntriesURL, &params)
body, err := h.getURL("GET", urlWithParams)
if err != nil {
log.Fatalln(err.Error())
Expand Down