agecem-org/apirequest/document.go

59 lines
1.3 KiB
Go
Raw Normal View History

2023-11-20 16:29:10 -05:00
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 {
Bucket string
Document *multipart.FileHeader
}
}
func NewV1DocumentPOST(bucket string, document *multipart.FileHeader) (request V1DocumentPOST, err error) {
if bucket == "" {
err = fmt.Errorf("NewV1DocumentPOST requires non-nil bucket name")
return
}
request.Data.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.Data.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.Data.Bucket), &buf, true, &response)
}