1 Star 0 Fork 0

余济舟/aid

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
http_client_attributer.go 8.67 KB
一键复制 编辑 原始数据 按行查看 历史
余济舟 提交于 2025-09-18 16:52 +08:00 . [feature]增加regexp包
package httpClientV2
import (
"bytes"
"encoding/base64"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"maps"
"mime/multipart"
"net/http"
"net/url"
"os"
"time"
"gitee.com/jericho-yu/aid/str"
"github.com/spf13/cast"
)
type (
HttpClientAttributer interface {
Register(req *HttpClient)
Error() error
implIHttpClientAttributer()
}
AttrUrl struct{ url string }
AttrQueries struct{ queries map[string]any }
AttrMethod struct{ method string }
AttrHeaders struct{ headers map[string][]string }
AttrSetHeaders struct{ headers map[string][]string }
AttrBody struct {
err error
body []byte
contentType ContentType
}
AttrTimeout struct{ timeout time.Duration }
AttrTransport struct{ transport *http.Transport }
AttrCert struct{ cert []byte }
AttrAutoCopyResBody struct{ autoCopy bool }
)
func Url(urls ...string) HttpClientAttributer {
ins := &AttrUrl{url: ""}
switch {
}
if len(urls) == 0 {
} else if len(urls) == 1 {
ins.url = urls[0]
} else {
ins.url = str.APP.Buffer.JoinString(urls...)
}
return ins
}
func (my *AttrUrl) Register(req *HttpClient) { req.url = my.url }
func (my *AttrUrl) Error() error { return nil }
func (*AttrUrl) implIHttpClientAttributer() {}
func Queries(queries map[string]any) *AttrQueries {
if queries == nil {
queries = map[string]any{}
}
return &AttrQueries{queries: queries}
}
func (my *AttrQueries) Append(queries map[string]any) *AttrQueries {
if len(queries) > 0 {
maps.Copy(my.queries, queries)
}
return my
}
func (my *AttrQueries) AppendOne(key string, value any) *AttrQueries {
my.queries[key] = value
return my
}
func (my *AttrQueries) Register(req *HttpClient) { req.queries = my.queries }
func (my *AttrQueries) Error() error { return nil }
func (*AttrQueries) implIHttpClientAttributer() {}
func Method(method string) *AttrMethod {
return &AttrMethod{method: method}
}
func (my *AttrMethod) Register(req *HttpClient) { req.method = my.method }
func (my *AttrMethod) Error() error { return nil }
func (*AttrMethod) implIHttpClientAttributer() {}
func Headers(headers map[string][]string) *AttrHeaders {
if headers == nil {
headers = map[string][]string{}
}
return &AttrHeaders{headers: headers}
}
func (my *AttrHeaders) Append(headers map[string][]string) *AttrHeaders {
if len(headers) > 0 {
maps.Copy(my.headers, headers)
}
return my
}
func (my *AttrHeaders) AppendOne(key string, values ...string) *AttrHeaders {
my.headers[key] = values
return my
}
func (my *AttrHeaders) ContentType(contentType ContentType) *AttrHeaders {
my.headers["Content-Type"] = []string{ContentTypes[contentType]}
return my
}
func (my *AttrHeaders) Accept(accept Accept) *AttrHeaders {
my.headers["Accept"] = []string{Accepts[accept]}
return my
}
func (my *AttrHeaders) Authorization(username, password, title string) *AttrHeaders {
my.headers["Authorization"] = []string{str.BufferApp.NewString(title).S(" ", base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "%s:%s", username, password))).String()}
return my
}
func (my *AttrHeaders) Register(req *HttpClient) {
if req.headers == nil {
req.headers = my.headers
} else {
for key, values := range my.headers {
if _, exists := req.headers[key]; !exists {
req.headers[key] = values
} else {
req.headers[key] = append(req.headers[key], values...)
}
}
}
}
func (my *AttrHeaders) Error() error { return nil }
func (*AttrHeaders) implIHttpClientAttributer() {}
func SetHeaders(headers map[string][]string) *AttrSetHeaders {
if headers == nil {
headers = map[string][]string{}
}
return &AttrSetHeaders{headers: headers}
}
func (my *AttrSetHeaders) ContentType(contentType ContentType) *AttrSetHeaders {
my.headers["Content-Type"] = []string{ContentTypes[contentType]}
return my
}
func (my *AttrSetHeaders) Accept(accept Accept) *AttrSetHeaders {
my.headers["Accept"] = []string{Accepts[accept]}
return my
}
func (my *AttrSetHeaders) Authorization(username, password, title string) *AttrSetHeaders {
my.headers["Authorization"] = []string{str.BufferApp.NewString(title).S(" ", base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "%s:%s", username, password))).String()}
return my
}
func (my *AttrSetHeaders) Register(req *HttpClient) {
if req.headers == nil {
req.headers = my.headers
} else {
maps.Copy(req.headers, my.headers)
}
}
func (my *AttrSetHeaders) Error() error { return nil }
func (*AttrSetHeaders) implIHttpClientAttributer() {}
func Json(body any) *AttrBody {
ins := &AttrBody{}
ins.body, ins.err = json.Marshal(body)
ins.contentType = ContentTypeJson
return ins
}
func Xml(body any) HttpClientAttributer {
ins := &AttrBody{}
ins.body, ins.err = xml.Marshal(body)
ins.contentType = ContentTypeXml
return ins
}
func Form(body map[string]any) *AttrBody {
ins := &AttrBody{}
params := url.Values{}
for k, v := range body {
params.Add(k, cast.ToString(v))
}
ins.body = []byte(params.Encode())
ins.contentType = ContentTypeXWwwFormUrlencoded
return ins
}
func FormData(fields, files map[string]string) *AttrBody {
var (
e error
buffer bytes.Buffer
ins = &AttrBody{}
)
writer := multipart.NewWriter(&buffer)
if len(fields) > 0 {
for k, v := range fields {
if e = writer.WriteField(k, v); e != nil {
ins.err = e
return ins
}
}
}
if len(files) > 0 {
for k, v := range files {
fileWriter, _ := writer.CreateFormFile("fileField", k)
file, e := os.Open(v)
if e != nil {
ins.err = e
return ins
}
_, e = io.Copy(fileWriter, file)
if e != nil {
ins.err = e
return ins
}
_ = file.Close()
}
}
ins.body = []byte(writer.FormDataContentType())
ins.contentType = ContentTypeFormData
return ins
}
func Plain(body string) *AttrBody {
ins := &AttrBody{}
ins.body = []byte(body)
ins.contentType = ContentTypePlain
return ins
}
func Html(body string) *AttrBody {
ins := &AttrBody{}
ins.body = []byte(body)
ins.contentType = ContentTypeXml
return ins
}
func Css(body string) *AttrBody {
ins := &AttrBody{}
ins.body = []byte(body)
ins.contentType = ContentTypeCss
return ins
}
func Javascript(body string) *AttrBody {
ins := &AttrBody{}
ins.body = []byte(body)
ins.contentType = ContentTypeJavascript
return ins
}
func Bytes(body []byte) *AttrBody {
ins := &AttrBody{body: body}
return ins
}
func Reader(body io.ReadCloser) *AttrBody {
var (
ins = &AttrBody{}
buffer = bytes.NewBuffer([]byte{})
)
if body == nil {
ins.err = errors.New("设置steam流失败:不能为空")
return ins
}
if _, ins.err = io.Copy(buffer, body); ins.err != nil {
return ins
}
ins.body = buffer.Bytes()
return ins
}
func File(filename string) *AttrBody {
var (
ins = &AttrBody{}
file *os.File
buffer = bytes.NewBuffer([]byte{})
)
if file, ins.err = os.Open(filename); ins.err != nil {
return ins
}
defer func(file *os.File) {
if err := file.Close(); err != nil {
fmt.Printf("Failed to close file: %v", err)
}
}(file)
// 获取文件大小
stat, _ := file.Stat()
size := stat.Size()
// 创建RequestBodyReader用于读取文件内容
if size > 1*1024*1024 {
if _, ins.err = io.Copy(buffer, file); ins.err != nil {
return ins
}
ins.body = buffer.Bytes()
} else {
if ins.body, ins.err = io.ReadAll(file); ins.err != nil {
return ins
}
}
return ins
}
func (my *AttrBody) Register(req *HttpClient) {
req.requestBody = my.body
if my.contentType != "" {
req.headers["Content-Type"] = []string{ContentTypes[my.contentType]}
}
req.err = my.err
}
func (my *AttrBody) Error() error { return my.err }
func (*AttrBody) implIHttpClientAttributer() {}
func Timeout(timeout time.Duration) *AttrTimeout {
if timeout <= 0 {
timeout = 0
}
return &AttrTimeout{timeout: timeout}
}
func (my *AttrTimeout) Register(req *HttpClient) { req.timeout = my.timeout }
func (*AttrTimeout) Error() error { return nil }
func (*AttrTimeout) implIHttpClientAttributer() {}
func Transport(transport *http.Transport) *AttrTransport {
return &AttrTransport{transport: transport}
}
func (my *AttrTransport) Register(req *HttpClient) { req.transport = my.transport }
func (my *AttrTransport) Error() error { return nil }
func (*AttrTransport) implIHttpClientAttributer() {}
func Cert(cert []byte) *AttrCert { return &AttrCert{cert: cert} }
func (my *AttrCert) Register(req *HttpClient) { req.cert = my.cert }
func (my *AttrCert) Error() error { return nil }
func (*AttrCert) implIHttpClientAttributer() {}
func AutoCopy(autoCopy bool) *AttrAutoCopyResBody { return &AttrAutoCopyResBody{autoCopy: autoCopy} }
func (my *AttrAutoCopyResBody) Register(req *HttpClient) { req.autoCopy = my.autoCopy }
func (*AttrAutoCopyResBody) Error() error { return nil }
func (*AttrAutoCopyResBody) implIHttpClientAttributer() {}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jericho-yu/aid.git
git@gitee.com:jericho-yu/aid.git
jericho-yu
aid
aid
v1.35.18

搜索帮助