48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
|
|
package vikunja
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Task struct {
|
||
|
|
Title string `json:"title"`
|
||
|
|
Description string `json:"description"`
|
||
|
|
Status int `json:"status"` // Assuming 1 is for completed tasks
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetTasks(instance, user, from, to string) ([]Task, error) {
|
||
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/api/v1/user/tasks", instance), nil)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("error building vikunja request: %w", err)
|
||
|
|
}
|
||
|
|
q := req.URL.Query()
|
||
|
|
q.Add("user_id", user)
|
||
|
|
q.Add("status", "1") // Assuming 1 is for completed tasks
|
||
|
|
req.URL.RawQuery = q.Encode()
|
||
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("VIKUNJA_TOKEN")))
|
||
|
|
resp, err := http.DefaultClient.Do(req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("error executing vikunja request: %w", err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
if resp.StatusCode >= 400 {
|
||
|
|
b := bytes.NewBuffer(nil)
|
||
|
|
io.Copy(b, resp.Body)
|
||
|
|
return nil, fmt.Errorf("error talking to vikunja: %s", b.String())
|
||
|
|
}
|
||
|
|
|
||
|
|
var tasks []Task
|
||
|
|
err = json.NewDecoder(resp.Body).Decode(&tasks)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("error decoding vikunja response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return tasks, nil
|
||
|
|
}
|