agecem-org/pkg/agecemorg/request.go

307 lines
7.6 KiB
Go
Raw Permalink Normal View History

/*
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
2023-11-20 16:29:10 -05:00
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
2023-11-20 16:29:10 -05:00
)
2024-08-22 12:33:54 -04:00
var _ voki.Requester[ListBucketsResponse] = ListBucketsRequest{}
2024-08-22 12:33:54 -04:00
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"`
}
}
2024-08-22 12:33:54 -04:00
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
}
2024-08-22 12:33:54 -04:00
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 != ""
}
2024-08-22 12:33:54 -04:00
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)
}
2024-08-22 12:33:54 -04:00
var _ voki.Requester[CreateDocumentResponse] = CreateDocumentRequest{}
2023-11-20 16:29:10 -05:00
2024-08-22 12:33:54 -04:00
type CreateDocumentRequest struct {
2023-11-20 16:29:10 -05:00
Data struct {
2024-08-22 12:33:54 -04:00
Document *multipart.FileHeader `json:"document"`
2023-11-20 16:29:10 -05:00
}
Params struct {
Bucket string `json:"bucket"`
}
2023-11-20 16:29:10 -05:00
}
2024-08-22 12:33:54 -04:00
func NewV1DocumentPOST(bucket string, document *multipart.FileHeader) (request CreateDocumentRequest, err error) {
2023-11-20 16:29:10 -05:00
if bucket == "" {
err = fmt.Errorf("NewV1DocumentPOST requires non-nil bucket name")
return
}
request.Params.Bucket = bucket
2023-11-20 16:29:10 -05:00
if document == nil {
err = fmt.Errorf("NewV1DocumentPOST requires non-nil document")
return
}
request.Data.Document = document
return
}
2024-08-22 12:33:54 -04:00
func (request CreateDocumentRequest) Complete() bool {
return request.Params.Bucket != "" && request.Data.Document != nil
2023-11-20 16:29:10 -05:00
}
2024-08-22 12:33:54 -04:00
func (request CreateDocumentRequest) Request(v *voki.Voki) (response CreateDocumentResponse, err error) {
2023-11-20 16:29:10 -05:00
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)
2023-11-20 16:29:10 -05:00
}
2023-11-20 16:56:44 -05:00
2024-08-22 12:33:54 -04:00
var _ voki.Requester[DeleteDocumentResponse] = DeleteDocumentRequest{}
2023-11-20 16:56:44 -05:00
2024-08-22 12:33:54 -04:00
type DeleteDocumentRequest struct {
2023-11-20 16:56:44 -05:00
Params struct {
Bucket string `json:"bucket"`
Document string `json:"document"`
2023-11-20 16:56:44 -05:00
}
}
2024-08-22 12:33:54 -04:00
func NewV1DocumentDELETE(bucket, document string) (request DeleteDocumentRequest, err error) {
2023-11-20 16:56:44 -05:00
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
}
2024-08-22 12:33:54 -04:00
func (request DeleteDocumentRequest) Complete() bool {
2023-11-20 16:56:44 -05:00
return request.Params.Bucket != "" && request.Params.Document != ""
}
2024-08-22 12:33:54 -04:00
func (request DeleteDocumentRequest) Request(v *voki.Voki) (response DeleteDocumentResponse, err error) {
2023-11-20 16:56:44 -05:00
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)
}
2024-08-22 12:33:54 -04:00
var _ voki.Requester[UpdateDocumentKeyResponse] = UpdateDocumentKeyRequest{}
2024-08-22 12:33:54 -04:00
type UpdateDocumentKeyRequest struct {
Data struct {
NewKey string `json:"newKey"`
}
Params struct {
Bucket string `json:"bucket"`
Document string `json:"document"`
}
}
2024-08-22 12:33:54 -04:00
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
}
2024-08-22 12:33:54 -04:00
func (request UpdateDocumentKeyRequest) Complete() bool {
return request.Params.Bucket != "" && request.Params.Document != "" && request.Data.NewKey != ""
}
2024-08-22 12:33:54 -04:00
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)
}
2024-08-22 12:33:54 -04:00
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)
}