From 3b65138337b795875b6ca40292c2bd971c0c9499 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 18 Dec 2023 17:39:26 -0500 Subject: [PATCH] feat: ajouter `V1DocumentsPOST` request et response --- apirequest/document.go | 63 +++++++++++++++++++++++++++++++++++++++++ apiresponse/document.go | 11 +++++++ 2 files changed, 74 insertions(+) diff --git a/apirequest/document.go b/apirequest/document.go index e4cfa3c..92f317d 100644 --- a/apirequest/document.go +++ b/apirequest/document.go @@ -12,6 +12,69 @@ import ( "git.agecem.com/agecem/agecem-org/apiresponse" ) +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) +} + var _ request.Requester[apiresponse.V1DocumentPOST] = V1DocumentPOST{} type V1DocumentPOST struct { diff --git a/apiresponse/document.go b/apiresponse/document.go index beab0ae..d02fe83 100644 --- a/apiresponse/document.go +++ b/apiresponse/document.go @@ -1,5 +1,16 @@ package apiresponse +type V1DocumentsPOST struct { + Response + Data struct { + Bucket string + Documents struct { + Key string + Size int64 + } + } +} + type V1DocumentPOST struct { Response Data struct {