agecem-org/apirequest/document.go
Victor Lacasse-Beaudoin 17fb5f16d1 fix!: ajouter struct tags à routes documents
BREAKING: ajouter struct tags pour documents request
2023-11-20 17:20:55 -05:00

100 lines
2.4 KiB
Go

package apirequest
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"codeberg.org/vlbeaudoin/voki"
"codeberg.org/vlbeaudoin/voki/request"
"git.agecem.com/agecem/agecem-org/apiresponse"
)
var _ request.Requester[apiresponse.V1DocumentPOST] = V1DocumentPOST{}
type V1DocumentPOST struct {
Data struct {
Document *multipart.FileHeader `document`
}
Params struct {
Bucket string `json:"bucket"`
}
}
func NewV1DocumentPOST(bucket string, document *multipart.FileHeader) (request V1DocumentPOST, 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 V1DocumentPOST) Complete() bool {
return request.Params.Bucket != "" && request.Data.Document != nil
}
func (request V1DocumentPOST) Request(v *voki.Voki) (response apiresponse.V1DocumentPOST, 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 _ request.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{}
type V1DocumentDELETE struct {
Params struct {
Bucket string `json:"bucket"`
Document string `json:"document"`
}
}
func NewV1DocumentDELETE(bucket, document string) (request V1DocumentDELETE, 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 V1DocumentDELETE) Complete() bool {
return request.Params.Bucket != "" && request.Params.Document != ""
}
func (request V1DocumentDELETE) Request(v *voki.Voki) (response apiresponse.V1DocumentDELETE, 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)
}