Make great

This commit is contained in:
2025-06-18 11:06:12 +02:00
parent 77d11c4351
commit 682c04ca79
3 changed files with 206 additions and 85 deletions

View File

@@ -1,6 +1,7 @@
package mite
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
@@ -219,6 +220,64 @@ func (a APIClient) GetTimeEntries(from, to time.Time) (TimeEntries, error) {
return out, nil
}
type requestAddTimeEntry struct {
RequestTimeEntryHolder struct {
DateAt string `json:"date_at"`
Minutes int `json:"minutes"`
ProjectID int `json:"project_id,omit_empty"`
ServiceID int `json:"service_id,omit_empty"`
Note string `json:"note"`
} `json:"time_entry"`
}
func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) error {
// POST /time_entries.json
// {
// "time_entry": {
// "date_at": "2015-9-15",
// "minutes": 185,
// "service_id": 243
// }
// }
req := requestAddTimeEntry{}
req.RequestTimeEntryHolder.DateAt = date
req.RequestTimeEntryHolder.Minutes = minutes
req.RequestTimeEntryHolder.Note = notes
req.RequestTimeEntryHolder.ProjectID = projectId
req.RequestTimeEntryHolder.ServiceID = serviceId
err := post(a.domain, a.apiKey, "/time_entries.json", req)
return err
}
func post(domain, apiKey, path string, data any) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
req, err := http.NewRequest("POST", baseurl(domain, path), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", apiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
}
return nil
}
func get(domain, apiKey, path string, data any) error {
req, err := http.NewRequest("GET", baseurl(domain, path), nil)
if err != nil {