50 lines
987 B
Go
50 lines
987 B
Go
package logic
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"git.anthrove.art/Anthrove/gorse-playground/internal/config"
|
|
"git.anthrove.art/Anthrove/gorse-playground/pkg/models"
|
|
"github.com/caarlos0/env/v11"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
var gorseConfig config.Gorse
|
|
|
|
func init() {
|
|
err := env.Parse(&gorseConfig)
|
|
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
}
|
|
|
|
func UpsertUser(ctx context.Context, user models.GorseUser) error {
|
|
return executeRequest(ctx, http.MethodPost, "/api/user", user)
|
|
}
|
|
|
|
func executeRequest(ctx context.Context, method, url string, data any) error {
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest(method, gorseConfig.Endpoint+url, bytes.NewReader(jsonData))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req = req.WithContext(ctx)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-API-Key", gorseConfig.ApiKey)
|
|
_, err = client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|