From 9f0b5abaa510bdf678287989b43f4b119d6d0714 Mon Sep 17 00:00:00 2001 From: lance547 <437653103@qq.com> Date: Wed, 22 May 2024 21:51:32 +0800 Subject: [PATCH] feat:add some api --- request.go | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 3 deletions(-) diff --git a/request.go b/request.go index df8a392..87f7b89 100644 --- a/request.go +++ b/request.go @@ -18,10 +18,15 @@ package easy_http import ( "context" - "github.com/cloudwego/hertz/pkg/common/config" - "github.com/cloudwego/hertz/pkg/protocol" + "fmt" + "io" "net/http" "net/url" + "reflect" + "strings" + + "github.com/cloudwego/hertz/pkg/common/config" + "github.com/cloudwego/hertz/pkg/protocol" ) type Request struct { @@ -29,10 +34,10 @@ type Request struct { URL string Method string QueryParam url.Values + FormData url.Values Header http.Header Cookies []*http.Cookie PathParams map[string]string - FormData map[string]string FileData map[string]string BodyParams interface{} RawRequest *protocol.Request @@ -40,6 +45,8 @@ type Request struct { RequestOptions []config.RequestOption Result interface{} Error interface{} + isMultiPart bool + multipartFiles []*File } const ( @@ -64,7 +71,176 @@ const ( // MethodOptions HTTP method MethodOptions = "OPTIONS" ) +type File struct { + Name string + ParamName string + io.Reader +} +func (r *Request) SetQueryParam(param, value string) *Request { + r.QueryParam.Set(param, value) + return r +} +func (r *Request) SetQueryParams(params map[string]string) *Request { + for p, v := range params { + r.SetQueryParam(p, v) + } + return r +} +func (r *Request) SetQueryParamsFromValues(params url.Values) *Request { + for p, v := range params { + for _, pv := range v { + r.QueryParam.Add(p, pv) + } + } + return r +} +func (r *Request) SetQueryString(query string) *Request { + params, err := url.ParseQuery(strings.TrimSpace(query)) + if err == nil { + for p, v := range params { + for _, pv := range v { + r.QueryParam.Add(p, pv) + } + } + } else { + fmt.Printf("%v",err) + } + return r +} +func (r * Request) AddQueryParam(params,value string)*Request{ + r.QueryParam.Add(params,value) + return r +} +func (r *Request) AddQueryParams(params map[string]string)*Request{ + for k,v :=range params{ + r.AddQueryParam(k,v) + } + return r +} +func (r *Request) SetPathParam(param, value string) *Request { + r.PathParams[param] = value + return r +} +func (r *Request) SetPathParams(params map[string]string) *Request { + for p, v := range params { + r.SetPathParam(p, v) + } + return r +} + +func (r *Request) SetHeader(header, value string) *Request { + r.Header.Set(header, value) + return r +} + +func (r *Request) SetHeaders(headers map[string]string) *Request { + for h, v := range headers { + r.SetHeader(h, v) + } + return r +} +func (r *Request) SetHeaderMultiValues(headers map[string][]string) *Request { + for key, values := range headers { + r.SetHeader(key, strings.Join(values, ", ")) + } + return r +} +func (r * Request) AddHeader(header , value string)*Request{ + r.Header.Add(header,value) + return r +} +func (r *Request) AddHeaders(headers map[string]string)*Request{ + for k , v :=range headers{ + r.AddHeader(k,v) + } + return r +} +func (r *Request)AddHeaderMultiValues(headers map[string][]string)*Request{ + for key ,value := range headers{ + r.AddHeader(key,strings.Join(value,", ")) + } + return r +} +// [] SetContentType(contentType string) +// [] SetJSONContentType() (可添加一些常用的 content-type) +func (r *Request) SetCookie(hc *http.Cookie) *Request { + r.Cookies = append(r.Cookies, hc) + return r +} +func (r *Request) SetCookies(rs []*http.Cookie) *Request { + r.Cookies = append(r.Cookies, rs...) + return r +} +// [] SetJSONBody(body interface{}) (自动注入 json content-type,参数可以是 strcut、map、[]byte、string 等) +// [] SetUrlEncodeBody(body url.Value) (自动注入 urlencode content-type) +func (r *Request) SetBody(body interface{}) *Request { + r.BodyParams= body + return r +} +func (r *Request) SetFormData(data map[string]string) *Request { + for k ,v :=range data{ + r.FormData.Set(k,v) + } + return r +} +func (r *Request) SetFormDataFromValues(data url.Values) *Request { + for key ,value := range data{ + for _,v :=range value{ + r.FormData.Add(key,v) + } + } + return r +} +func (r *Request) SetFiles(files map[string]string) *Request { + r.isMultiPart = true + for f, fp := range files { + r.FormData.Set("@"+f, fp) + } + return r +} + +func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Request { + r.isMultiPart = true + r.multipartFiles = append(r.multipartFiles, &File{ + Name: fileName, + ParamName: param, + Reader: reader, + }) + return r +} +func (r *Request) SetResult(res interface{}) *Request { + if res != nil { + vv := reflect.ValueOf(res) + if vv.Kind() == reflect.Ptr{ + r.Result = res + }else { + r.Result = reflect.New(vv.Type()).Interface() + } + } + return r +} +// [] WithContext(ctx) +// [] WithDC(dc) +// [] WithCluster(cluster) +// [] WithEnv(env) +// [] WIthCallTimeout(t) +func (r * Request)WithContext(ctx context.Context)*Request{ + r.Ctx = ctx + return r +} +func (r * Request)WithDC()*Request{ + return r +} +func (r * Request)WithCluster()*Request{ + return r +} +func (r * Request)WithEnv()*Request{ + return r +} +func (r * Request)WIthCallTimeout()*Request{ + return r +} func (r *Request) Get(url string) (*Response, error) { return r.Execute(MethodGet, url) } @@ -110,3 +286,4 @@ func (r *Request) Execute(method, url string) (*Response, error) { res, err = r.client.execute(r) return res, err } +