bottin/request.go

276 lines
5.8 KiB
Go
Raw Normal View History

package main
import (
2024-06-17 14:07:49 -04:00
"bytes"
"encoding/json"
"fmt"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
)
var _ voki.Requester[HealthGETResponse] = HealthGETRequest{}
type HealthGETRequest struct{}
func (request HealthGETRequest) Complete() bool { return true }
func (request HealthGETRequest) Request(v *voki.Voki) (response HealthGETResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete HealthGET request")
return
}
statusCode, body, err := v.CallAndParse(
http.MethodGet,
"/api/v7/health/",
nil,
true,
)
if err != nil {
err = fmt.Errorf("%d: %s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
2024-06-17 14:07:49 -04:00
var _ voki.Requester[ProgrammesPOSTResponse] = ProgrammesPOSTRequest{}
type ProgrammesPOSTRequest struct {
Data struct {
Programmes []Programme
}
}
func (request ProgrammesPOSTRequest) Complete() bool {
return len(request.Data.Programmes) != 0
}
func (request ProgrammesPOSTRequest) Request(v *voki.Voki) (response ProgrammesPOSTResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete ProgrammesPOSTRequest")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
statusCode, body, err := v.CallAndParse(
http.MethodPost,
"/api/v7/programme/",
2024-06-17 14:07:49 -04:00
&buf,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
var _ voki.Requester[MembresPOSTResponse] = MembresPOSTRequest{}
type MembresPOSTRequest struct {
Data struct {
Membres []Membre
}
}
func (request MembresPOSTRequest) Complete() bool {
return len(request.Data.Membres) != 0
}
func (request MembresPOSTRequest) Request(v *voki.Voki) (response MembresPOSTResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete MembresPOSTRequest")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
statusCode, body, err := v.CallAndParse(
http.MethodPost,
"/api/v7/membre/",
&buf,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
var _ voki.Requester[MembreGETResponse] = MembreGETRequest{}
type MembreGETRequest struct {
Param struct {
MembreID string `json:"membre_id" param:"membre_id"`
}
}
func (request MembreGETRequest) Complete() bool {
return request.Param.MembreID != ""
}
func (request MembreGETRequest) Request(v *voki.Voki) (response MembreGETResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete MembreGETRequest")
return
}
if id := request.Param.MembreID; !IsMembreID(id) {
err = fmt.Errorf("MembreID '%s' invalide", id)
return
}
statusCode, body, err := v.CallAndParse(
http.MethodGet,
fmt.Sprintf("/api/v7/membre/%s/", request.Param.MembreID),
nil,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
2024-06-18 21:21:30 -04:00
var _ voki.Requester[MembresGETResponse] = MembresGETRequest{}
type MembresGETRequest struct {
Query struct {
Limit int `json:"limit" query:"limit"`
}
}
func (request MembresGETRequest) Complete() bool { return true }
func (request MembresGETRequest) Request(v *voki.Voki) (response MembresGETResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete MembresGETRequest")
return
}
statusCode, body, err := v.CallAndParse(
http.MethodGet,
fmt.Sprintf("/api/v7/membre/?limit=%d", request.Query.Limit),
nil,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
var _ voki.Requester[MembrePreferedNamePUTResponse] = MembrePreferedNamePUTRequest{}
type MembrePreferedNamePUTRequest struct {
Data struct {
PreferedName string `json:"prefered_name"`
} `json:"data"`
Param struct {
MembreID string `json:"membre_id" param:"membre_id"`
} `json:"param"`
}
func (request MembrePreferedNamePUTRequest) Complete() bool {
return IsMembreID(request.Param.MembreID) && len(request.Data.PreferedName) != 0
}
func (request MembrePreferedNamePUTRequest) Request(v *voki.Voki) (response MembrePreferedNamePUTResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete MembrePreferedNamePUTRequest")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
statusCode, body, err := v.CallAndParse(
http.MethodPut,
"/api/v7/membre/%s/",
&buf,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}
var _ voki.Requester[ProgrammesGETResponse] = ProgrammesGETRequest{}
type ProgrammesGETRequest struct {
Query struct {
Limit int `json:"limit" query:"limit"`
}
}
func (request ProgrammesGETRequest) Complete() bool { return true }
func (request ProgrammesGETRequest) Request(v *voki.Voki) (response ProgrammesGETResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete ProgrammesGETRequest")
return
}
statusCode, body, err := v.CallAndParse(
http.MethodGet,
fmt.Sprintf("/api/v7/programme/?limit=%d", request.Query.Limit),
nil,
true,
)
if err != nil {
err = fmt.Errorf("code=%d err=%s", statusCode, err)
return
}
response.SetStatusCode(statusCode)
if err = json.Unmarshal(body, &response); err != nil {
return
}
return
}