simpleRequest/body.go

100 lines
2.6 KiB
Go
Raw Permalink Normal View History

2022-03-13 13:24:29 +00:00
/*
2023-02-08 14:35:27 +00:00
*FileName: body.go
*Author: JJXu
*CreateTime: 2022/3/2 上午1:23
*Description:
2022-03-13 13:24:29 +00:00
*/
package simpleRequest
import (
"bytes"
"io"
"mime/multipart"
"strings"
)
2022-12-09 18:08:17 +00:00
// EntryMark 请求体条目标记用于标记输入的body内容格式
type EntryMark string
func (b EntryMark) string() string {
return string(b)
}
const (
StringEntryType EntryMark = "__STRING_ENTRY__"
BytesEntryType EntryMark = "__BYTES_ENTRY__"
ModelEntryType EntryMark = "__MODEL_ENTRY__"
MapEntryType EntryMark = "__MAP_ENTRY__"
MultipartEntryType EntryMark = "__MULTIPART_ENTRY__"
FormFilePathKey EntryMark = "__FORM_FILE_PATH_KEY__"
2022-03-13 13:24:29 +00:00
)
func GetStringEntryTypeBody(bodyEntries map[string]any) io.Reader {
data, ok := bodyEntries[StringEntryType.string()]
if !ok || data == nil {
return nil
}
return strings.NewReader(data.(string))
}
func GetBytesEntryTypeBody(bodyEntries map[string]any) io.Reader {
data, ok := bodyEntries[BytesEntryType.string()]
if !ok || data == "" {
return nil
}
return bytes.NewReader(data.([]byte))
}
2022-03-13 13:24:29 +00:00
type BodyConf struct {
simpleReq *SimpleRequest
}
func (s *BodyConf) Set(key string, value any) *BodyConf {
s.simpleReq.BodyEntries[key] = value
2022-03-13 13:24:29 +00:00
return s
}
func (s *BodyConf) Sets(data map[string]any) *BodyConf {
s.simpleReq.BodyEntryMark = MapEntryType
2022-03-13 13:24:29 +00:00
for k, v := range data {
s.simpleReq.BodyEntries[k] = v
2022-03-13 13:24:29 +00:00
}
return s
}
func (s *BodyConf) SetString(strData string) *BodyConf {
s.simpleReq.BodyEntryMark = StringEntryType
s.simpleReq.BodyEntries[StringEntryType.string()] = strData
return s
}
func (s *BodyConf) SetBytes(byteData []byte) *BodyConf {
s.simpleReq.BodyEntryMark = BytesEntryType
s.simpleReq.BodyEntries[BytesEntryType.string()] = byteData
return s
}
func (s *BodyConf) SetModel(model any) *BodyConf {
s.simpleReq.BodyEntryMark = ModelEntryType
s.simpleReq.BodyEntries[ModelEntryType.string()] = model
2022-03-13 13:24:29 +00:00
return s
}
2023-11-16 06:27:24 +00:00
// 添加上传文件
func (s *BodyConf) SetFromDataFile(key, filePath string) *BodyConf {
s.simpleReq.BodyEntryMark = MultipartEntryType
s.simpleReq.BodyEntries[FormFilePathKey.string()+key] = filePath
if s.simpleReq.headers.Get(hdrContentTypeKey) == "" {
s.simpleReq.headers.Set(hdrContentTypeKey, formDataType)
}
return s
}
2023-11-16 06:27:24 +00:00
// 添加文件适用于服务端文件转发场景比如直接从c.FormFile("file")中获取FileHeader对象转发即可
func (s *BodyConf) SetFromDataMultipartFile(key string, multFile *multipart.FileHeader) *BodyConf {
s.simpleReq.BodyEntryMark = MultipartEntryType
s.simpleReq.BodyEntries[key] = multFile
if s.simpleReq.headers.Get(hdrContentTypeKey) == "" {
s.simpleReq.headers.Set(hdrContentTypeKey, formDataType)
}
return s
}