agecem-org/pkg/agecemorg/request.go
Victor Lacasse-Beaudoin 7d4a747774 refactor: déplacer fichiers go source vers pkg/ et cmd/
Déplacer public/ et templates/ vers ui/ et ui/public/

Bump projet à v3, API toujours v1
2024-09-27 20:25:12 -04:00

306 lines
7.6 KiB
Go

/*
File request.go contains the JSON request types for HTTP endpoints.
Le fichier request.go contient les types de requêtes pour les endpoints HTTP.
*/
package agecemorg
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
)
var _ voki.Requester[ListBucketsResponse] = ListBucketsRequest{}
type ListBucketsRequest struct{}
func NewV1BucketsGET() (request ListBucketsRequest, err error) {
return
}
func (request ListBucketsRequest) Complete() bool { return true }
func (request ListBucketsRequest) Request(v *voki.Voki) (response ListBucketsResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1BucketsGET request")
return
}
return response, v.UnmarshalIfComplete(http.MethodGet, "/v1/bucket", nil, true, &response)
}
var _ voki.Requester[ReadBucketResponse] = ReadBucketRequest{}
type ReadBucketRequest struct {
Params struct {
Bucket string `json:"bucket"`
}
}
func NewV1BucketGET(bucket string) (request ReadBucketRequest, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1BucketGET requires non-nil bucket name")
}
request.Params.Bucket = bucket
return
}
func (request ReadBucketRequest) Complete() bool { return request.Params.Bucket != "" }
func (request ReadBucketRequest) Request(v *voki.Voki) (response ReadBucketResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1BucketGET request")
return
}
return response, v.UnmarshalIfComplete(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", request.Params.Bucket), nil, true, &response)
}
var _ voki.Requester[CreateDocumentsResponse] = CreateDocumentsRequest{}
type CreateDocumentsRequest struct {
Data struct {
Documents []*multipart.FileHeader `json:"documents"`
}
Params struct {
Bucket string `json:"bucket"`
}
}
func NewV1DocumentsPOST(bucket string, documents ...*multipart.FileHeader) (request CreateDocumentsRequest, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentsPOST requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
if documents == nil {
err = fmt.Errorf("NewV1DocumentsPOST requires non-nil documents")
return
}
for _, document := range documents {
if document == nil {
err = fmt.Errorf("NewV1DocumentsPOST requires non-nil documents")
return
}
}
request.Data.Documents = documents
return
}
func (request CreateDocumentsRequest) Complete() bool {
if request.Data.Documents == nil {
return false
}
for _, document := range request.Data.Documents {
if document == nil {
return false
}
}
return request.Params.Bucket != ""
}
func (request CreateDocumentsRequest) Request(v *voki.Voki) (response CreateDocumentsResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1DocumentsPOST request")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s/many", request.Params.Bucket), &buf, true, &response)
}
var _ voki.Requester[CreateDocumentResponse] = CreateDocumentRequest{}
type CreateDocumentRequest struct {
Data struct {
Document *multipart.FileHeader `json:"document"`
}
Params struct {
Bucket string `json:"bucket"`
}
}
func NewV1DocumentPOST(bucket string, document *multipart.FileHeader) (request CreateDocumentRequest, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentPOST requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
if document == nil {
err = fmt.Errorf("NewV1DocumentPOST requires non-nil document")
return
}
request.Data.Document = document
return
}
func (request CreateDocumentRequest) Complete() bool {
return request.Params.Bucket != "" && request.Data.Document != nil
}
func (request CreateDocumentRequest) Request(v *voki.Voki) (response CreateDocumentResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1DocumentPOST request")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
return response, v.UnmarshalIfComplete(http.MethodPost, fmt.Sprintf("/v1/bucket/%s", request.Params.Bucket), &buf, true, &response)
}
var _ voki.Requester[DeleteDocumentResponse] = DeleteDocumentRequest{}
type DeleteDocumentRequest struct {
Params struct {
Bucket string `json:"bucket"`
Document string `json:"document"`
}
}
func NewV1DocumentDELETE(bucket, document string) (request DeleteDocumentRequest, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentDELETE requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
if document == "" {
err = fmt.Errorf("NewV1DocumentDELETE requires non-nil document name")
return
}
request.Params.Document = document
return
}
func (request DeleteDocumentRequest) Complete() bool {
return request.Params.Bucket != "" && request.Params.Document != ""
}
func (request DeleteDocumentRequest) Request(v *voki.Voki) (response DeleteDocumentResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1DocumentDELETE request")
return
}
return response, v.UnmarshalIfComplete(http.MethodDelete, fmt.Sprintf("/v1/bucket/%s/%s", request.Params.Bucket, request.Params.Document), nil, true, &response)
}
var _ voki.Requester[UpdateDocumentKeyResponse] = UpdateDocumentKeyRequest{}
type UpdateDocumentKeyRequest struct {
Data struct {
NewKey string `json:"newKey"`
}
Params struct {
Bucket string `json:"bucket"`
Document string `json:"document"`
}
}
func NewV1DocumentKeyPUT(bucket, document, newKey string) (request UpdateDocumentKeyRequest, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentKeyPUT requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
if document == "" {
err = fmt.Errorf("NewV1DocumentKeyPUT requires non-nil src document name")
return
}
request.Params.Document = document
if newKey == "" {
err = fmt.Errorf("NewV1DocumentKeyPUT requires non-nil dst document name")
}
request.Data.NewKey = newKey
return
}
func (request UpdateDocumentKeyRequest) Complete() bool {
return request.Params.Bucket != "" && request.Params.Document != "" && request.Data.NewKey != ""
}
func (request UpdateDocumentKeyRequest) Request(v *voki.Voki) (response UpdateDocumentKeyResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1DocumentKeyPUT request")
return
}
var buf bytes.Buffer
if err = json.NewEncoder(&buf).Encode(request.Data); err != nil {
return
}
return response, v.UnmarshalIfComplete(http.MethodPut, fmt.Sprintf("/v1/bucket/%s/%s", request.Params.Bucket, request.Params.Document), &buf, true, &response)
}
var _ voki.Requester[ExecuteSeedResponse] = ExecuteSeedRequest{}
type ExecuteSeedRequest struct{}
func NewV1SeedPOST() (request ExecuteSeedRequest, err error) {
return
}
func (r ExecuteSeedRequest) Complete() bool { return true }
func (r ExecuteSeedRequest) Request(v *voki.Voki) (response ExecuteSeedResponse, err error) {
if !r.Complete() {
err = fmt.Errorf("Incomplete V1SeedPOST")
return
}
return response, v.UnmarshalIfComplete(http.MethodPost, "/v1/seed", nil, true, &response)
}
var _ voki.Requester[ReadSpecResponse] = ReadSpecRequest{}
type ReadSpecRequest struct{}
func NewV1SpecGET() (request ReadSpecRequest, err error) {
return
}
func (request ReadSpecRequest) Complete() bool { return true }
func (request ReadSpecRequest) Request(v *voki.Voki) (response ReadSpecResponse, err error) {
if !request.Complete() {
err = fmt.Errorf("Incomplete V1SpecGET")
return
}
return response, v.UnmarshalIfComplete(http.MethodGet, "/v1/spec", nil, true, &response)
}