tiko/client.go

123 lines
2.0 KiB
Go
Raw Normal View History

2024-11-28 21:44:31 +00:00
package tiko
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
)
var (
ErrMissingConfiguration = errors.New("missing configuration")
)
type Config struct {
Debug bool `json:"debug,omitempty"`
Username string `json:"username"`
Password string `json:"password"`
}
type Client struct {
config *Config
httpClient *http.Client
user *User
token string
}
func NewClient(cfg *Config) *Client {
jar, _ := cookiejar.New(&cookiejar.Options{})
return &Client{
config: cfg,
httpClient: &http.Client{
Jar: jar,
},
}
}
func (c *Client) Debug() bool {
if c.config != nil {
return c.config.Debug
}
return false
}
func (c *Client) Init() error {
if c.user != nil {
return nil
}
if c.config == nil {
return ErrMissingConfiguration
}
var err error
c.user, c.token, err = c.Authenticate(c.config.Username, c.config.Password)
if err != nil {
return err
}
return nil
}
func (c *Client) GetUser() (*User, error) {
if c.user != nil {
return c.user, nil
}
if err := c.Init(); err != nil {
return nil, err
}
return c.user, nil
}
func Do[T any](c *Client, req *http.Request) (*T, error) {
// Ajouter les en-têtes à la requête
if c.token != "" {
req.Header.Set("Authorization", "token "+c.token)
}
req.Header.Set("Content-Type", "application/json")
// Effectuer la requête HTTP
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
if c.Debug() {
d, _ := httputil.DumpRequest(req, true)
fmt.Println("Request\n", string(d))
d, _ = httputil.DumpResponse(resp, true)
fmt.Println("Response\n", string(d))
}
return nil, errors.New("bad http status")
}
// Lire le corps de la réponse
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Décoder la réponse JSON
// var historyResponse HistoryResponse
var obj T
err = json.Unmarshal(body, &obj)
if err != nil {
return nil, err
}
return &obj, nil
}