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"
|
|
|
|
)
|
|
|
|
|
2023-12-18 17:39:26 -05:00
|
|
|
var _ request.Requester[apiresponse.V1DocumentsPOST] = V1DocumentsPOST{}
|
|
|
|
|
|
|
|
type V1DocumentsPOST struct {
|
|
|
|
Data struct {
|
|
|
|
Documents []*multipart.FileHeader `json:"documents"`
|
|
|
|
}
|
|
|
|
Params struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewV1DocumentsPOST(bucket string, documents ...*multipart.FileHeader) (request V1DocumentsPOST, 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 V1DocumentsPOST) 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 V1DocumentsPOST) Request(v *voki.Voki) (response apiresponse.V1DocumentsPOST, 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)
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:29:10 -05:00
|
|
|
var _ request.Requester[apiresponse.V1DocumentPOST] = V1DocumentPOST{}
|
|
|
|
|
2023-12-19 19:32:03 -05:00
|
|
|
// Deprecated: Use V1DocumentsPOST instead
|
2023-11-20 16:29:10 -05:00
|
|
|
type V1DocumentPOST struct {
|
|
|
|
Data struct {
|
2023-11-20 17:20:26 -05:00
|
|
|
Document *multipart.FileHeader `document`
|
2023-11-20 16:29:10 -05:00
|
|
|
}
|
2023-11-20 17:15:54 -05:00
|
|
|
Params struct {
|
2023-11-20 17:20:26 -05:00
|
|
|
Bucket string `json:"bucket"`
|
2023-11-20 17:15:54 -05:00
|
|
|
}
|
2023-11-20 16:29:10 -05:00
|
|
|
}
|
|
|
|
|
2023-12-19 19:32:03 -05:00
|
|
|
// Deprecated: Use NewV1DocumentsPOST instead
|
2023-11-20 16:29:10 -05:00
|
|
|
func NewV1DocumentPOST(bucket string, document *multipart.FileHeader) (request V1DocumentPOST, err error) {
|
|
|
|
if bucket == "" {
|
|
|
|
err = fmt.Errorf("NewV1DocumentPOST requires non-nil bucket name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-20 17:15:54 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request V1DocumentPOST) Complete() bool {
|
2023-11-20 17:15:54 -05:00
|
|
|
return request.Params.Bucket != "" && request.Data.Document != nil
|
2023-11-20 16:29:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-20 17:15:54 -05:00
|
|
|
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
|
|
|
|
|
|
|
var _ request.Requester[apiresponse.V1DocumentDELETE] = V1DocumentDELETE{}
|
|
|
|
|
|
|
|
type V1DocumentDELETE struct {
|
|
|
|
Params struct {
|
2023-11-20 17:20:26 -05:00
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Document string `json:"document"`
|
2023-11-20 16:56:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-01-23 16:50:03 -05:00
|
|
|
|
|
|
|
var _ request.Requester[apiresponse.V1DocumentKeyPUT] = V1DocumentKeyPUT{}
|
|
|
|
|
|
|
|
type V1DocumentKeyPUT struct {
|
|
|
|
Data struct {
|
|
|
|
NewKey string `json:"newKey"`
|
|
|
|
}
|
|
|
|
Params struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Document string `json:"document"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewV1DocumentKeyPUT(bucket, document, newKey string) (request V1DocumentKeyPUT, 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 V1DocumentKeyPUT) Complete() bool {
|
|
|
|
return request.Params.Bucket != "" && request.Params.Document != "" && request.Data.NewKey != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request V1DocumentKeyPUT) Request(v *voki.Voki) (response apiresponse.V1DocumentKeyPUT, 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)
|
|
|
|
}
|