113 lines
2.4 KiB
Go
113 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"codeberg.org/vlbeaudoin/voki/v3"
|
|
)
|
|
|
|
type APIClient struct {
|
|
Voki *voki.Voki
|
|
}
|
|
|
|
func (c APIClient) GetHealth() (health string, err error) {
|
|
var request HealthGETRequest
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Message, nil
|
|
}
|
|
|
|
func (c APIClient) InsertProgrammes(programmes ...Programme) (amountInserted int64, err error) {
|
|
var request ProgrammesPOSTRequest
|
|
request.Data.Programmes = programmes
|
|
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Data.ProgrammesInserted, nil
|
|
}
|
|
|
|
func (c APIClient) InsertMembres(membres ...Membre) (amountInserted int64, err error) {
|
|
var request MembresPOSTRequest
|
|
request.Data.Membres = membres
|
|
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Data.MembresInserted, nil
|
|
}
|
|
|
|
func (c APIClient) GetMembre(membreID string) (membre Membre, err error) {
|
|
var request MembreGETRequest
|
|
request.Param.MembreID = membreID
|
|
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Data.Membre, nil
|
|
}
|
|
|
|
func (c APIClient) GetMembres(limit int) (membres []Membre, err error) {
|
|
var request MembresGETRequest
|
|
|
|
request.Query.Limit = limit
|
|
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Data.Membres, nil
|
|
}
|
|
|
|
func (c APIClient) GetProgrammes(limit int) (programmes []Programme, err error) {
|
|
var request ProgrammesGETRequest
|
|
|
|
request.Query.Limit = limit
|
|
|
|
response, err := request.Request(c.Voki)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if code, message := response.StatusCode(), response.Message; code >= 400 {
|
|
err = fmt.Errorf("%d: %s", code, message)
|
|
return
|
|
}
|
|
|
|
return response.Data.Programmes, nil
|
|
}
|