3
0
corteza/sam/rest/request/attachment.go
2018-11-05 15:09:54 +01:00

127 lines
2.9 KiB
Go

package request
/*
Hello! This file is auto-generated from `docs/src/spec.json`.
For development:
In order to update the generated files, edit this file under the location,
add your struct fields, imports, API definitions and whatever you want, and:
1. run [spec](https://github.com/titpetric/spec) in the same folder,
2. run `./_gen.php` in this folder.
You may edit `attachment.go`, `attachment.util.go` or `attachment_test.go` to
implement your API calls, helper functions and tests. The file `attachment.go`
is only generated the first time, and will not be overwritten if it exists.
*/
import (
"encoding/json"
"github.com/go-chi/chi"
"github.com/jmoiron/sqlx/types"
"github.com/pkg/errors"
"io"
"mime/multipart"
"net/http"
"strings"
)
var _ = chi.URLParam
var _ = types.JSONText{}
var _ = multipart.FileHeader{}
// Attachment original request parameters
type AttachmentOriginal struct {
Download bool
Name string
AttachmentID uint64 `json:",string"`
}
func NewAttachmentOriginal() *AttachmentOriginal {
return &AttachmentOriginal{}
}
func (a *AttachmentOriginal) Fill(r *http.Request) (err error) {
if strings.ToLower(r.Header.Get("content-type")) == "application/json" {
err = json.NewDecoder(r.Body).Decode(a)
switch {
case err == io.EOF:
err = nil
case err != nil:
return errors.Wrap(err, "error parsing http request body")
}
}
if err = r.ParseForm(); err != nil {
return err
}
get := map[string]string{}
post := map[string]string{}
urlQuery := r.URL.Query()
for name, param := range urlQuery {
get[name] = string(param[0])
}
postVars := r.Form
for name, param := range postVars {
post[name] = string(param[0])
}
if val, ok := get["download"]; ok {
a.Download = parseBool(val)
}
a.Name = chi.URLParam(r, "name")
a.AttachmentID = parseUInt64(chi.URLParam(r, "attachmentID"))
return err
}
var _ RequestFiller = NewAttachmentOriginal()
// Attachment preview request parameters
type AttachmentPreview struct {
Ext string
AttachmentID uint64 `json:",string"`
}
func NewAttachmentPreview() *AttachmentPreview {
return &AttachmentPreview{}
}
func (a *AttachmentPreview) Fill(r *http.Request) (err error) {
if strings.ToLower(r.Header.Get("content-type")) == "application/json" {
err = json.NewDecoder(r.Body).Decode(a)
switch {
case err == io.EOF:
err = nil
case err != nil:
return errors.Wrap(err, "error parsing http request body")
}
}
if err = r.ParseForm(); err != nil {
return err
}
get := map[string]string{}
post := map[string]string{}
urlQuery := r.URL.Query()
for name, param := range urlQuery {
get[name] = string(param[0])
}
postVars := r.Form
for name, param := range postVars {
post[name] = string(param[0])
}
a.Ext = chi.URLParam(r, "ext")
a.AttachmentID = parseUInt64(chi.URLParam(r, "attachmentID"))
return err
}
var _ RequestFiller = NewAttachmentPreview()