Compare commits
No commits in common. "c6d8fee7156881b220817dcbced745800dccaef5" and "163e7abf47f20ff5a9ba6c4c0b011d4ad8268894" have entirely different histories.
c6d8fee715
...
163e7abf47
65
mite/mite.go
65
mite/mite.go
@ -3,7 +3,6 @@ package mite
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@ -57,7 +56,7 @@ func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")"
|
|||||||
func (a APIClient) GetProjects() (Projects, error) {
|
func (a APIClient) GetProjects() (Projects, error) {
|
||||||
// GET /projects.json
|
// GET /projects.json
|
||||||
p := APIProjects{}
|
p := APIProjects{}
|
||||||
err := a.get("/projects.json", &p)
|
err := get(a.domain, a.apiKey, "/projects.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -99,7 +98,7 @@ func (c Customer) GetName() string { return c.Name }
|
|||||||
func (a APIClient) GetCustomers() (Customers, error) {
|
func (a APIClient) GetCustomers() (Customers, error) {
|
||||||
// GET /customers.json
|
// GET /customers.json
|
||||||
p := apiCustomers{}
|
p := apiCustomers{}
|
||||||
err := a.get("/customers.json", &p)
|
err := get(a.domain, a.apiKey, "/customers.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -143,7 +142,7 @@ func (s Service) GetName() string {
|
|||||||
func (a APIClient) GetServices() (Services, error) {
|
func (a APIClient) GetServices() (Services, error) {
|
||||||
// GET /services.json
|
// GET /services.json
|
||||||
p := apiServices{}
|
p := apiServices{}
|
||||||
err := a.get("/services.json", &p)
|
err := get(a.domain, a.apiKey, "/services.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -216,7 +215,7 @@ func (a APIClient) GetTimeEntries(from, to time.Time) (TimeEntries, error) {
|
|||||||
p := apiTimeEntry{}
|
p := apiTimeEntry{}
|
||||||
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
||||||
|
|
||||||
err := a.get(u, &p)
|
err := get(a.domain, a.apiKey, u, &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -254,62 +253,23 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
|
|||||||
req.RequestTimeEntryHolder.ProjectID = projectId
|
req.RequestTimeEntryHolder.ProjectID = projectId
|
||||||
req.RequestTimeEntryHolder.ServiceID = serviceId
|
req.RequestTimeEntryHolder.ServiceID = serviceId
|
||||||
|
|
||||||
err := a.post("/time_entries.json", req)
|
err := post(a.domain, a.apiKey, "/time_entries.json", req)
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiTimeTrackerEntry struct {
|
func post(domain, apiKey, path string, data any) error {
|
||||||
TimeTrackerHolder timeTrackerHolder `json:"tracker"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type timeTrackerHolder struct {
|
|
||||||
TrackingTimeEntry *TrackingTimeEntry `json:"tracking_time_entry"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TrackingTimeEntry struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Minutes int `json:"minutes"`
|
|
||||||
Since time.Time `json:"since"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// {
|
|
||||||
// "tracker": {
|
|
||||||
// "tracking_time_entry": {
|
|
||||||
// "id": 36135321,
|
|
||||||
// "minutes": 247,
|
|
||||||
// "since": "2015-10-15T17:05:04+02:00"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
var ErrNoTracker = errors.New("no time tracker running")
|
|
||||||
|
|
||||||
// GetTimeTracker gets the current running time tracker. If no tracker is
|
|
||||||
// running, the error returned will be ErrNoTracker
|
|
||||||
func (a APIClient) GetTimeTracker() (TrackingTimeEntry, error) {
|
|
||||||
r := apiTimeTrackerEntry{}
|
|
||||||
err := a.get("/tracker.json", &r)
|
|
||||||
if err != nil {
|
|
||||||
return TrackingTimeEntry{}, err
|
|
||||||
}
|
|
||||||
if r.TimeTrackerHolder.TrackingTimeEntry == nil {
|
|
||||||
return TrackingTimeEntry{}, ErrNoTracker
|
|
||||||
}
|
|
||||||
return *r.TimeTrackerHolder.TrackingTimeEntry, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a APIClient) post(path string, data any) error {
|
|
||||||
|
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", baseurl(a.domain, path), bytes.NewBuffer(b))
|
req, err := http.NewRequest("POST", baseurl(domain, path), bytes.NewBuffer(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("X-MiteApiKey", a.apiKey)
|
req.Header.Add("X-MiteApiKey", apiKey)
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
@ -325,12 +285,12 @@ func (a APIClient) post(path string, data any) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a APIClient) get(path string, data any) error {
|
func get(domain, apiKey, path string, data any) error {
|
||||||
req, err := http.NewRequest("GET", baseurl(a.domain, path), nil)
|
req, err := http.NewRequest("GET", baseurl(domain, path), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("X-MiteApiKey", a.apiKey)
|
req.Header.Add("X-MiteApiKey", apiKey)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -345,6 +305,7 @@ func (a APIClient) get(path string, data any) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func baseurl(domain, path string) string {
|
func baseurl(domain, path string) string {
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
package mite
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestUnmarshalTimeTracking(t *testing.T) {
|
|
||||||
trackerOn := []byte(`
|
|
||||||
{
|
|
||||||
"tracker": {
|
|
||||||
"tracking_time_entry": {
|
|
||||||
"id": 36135321,
|
|
||||||
"minutes": 247,
|
|
||||||
"since": "2015-10-15T17:05:04+02:00"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}`)
|
|
||||||
trackerOff := []byte(`{
|
|
||||||
"tracker": {}
|
|
||||||
}`)
|
|
||||||
off := apiTimeTrackerEntry{}
|
|
||||||
err := json.Unmarshal(trackerOff, &off)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if off.TimeTrackerHolder.TrackingTimeEntry != nil {
|
|
||||||
t.Error("expected nil, but is not")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
on := apiTimeTrackerEntry{}
|
|
||||||
err = json.Unmarshal(trackerOn, &on)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if on.TimeTrackerHolder.TrackingTimeEntry == nil {
|
|
||||||
t.Error("expected note nil, but is not")
|
|
||||||
} else {
|
|
||||||
if on.TimeTrackerHolder.TrackingTimeEntry.ID != 36135321 {
|
|
||||||
t.Error("bad unmarshal?")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user