123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package http_client
- import (
- "encoding/json"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/go-resty/resty/v2"
- "github.com/pkg/errors"
- "io"
- )
- type RequestOption func(request *Request)
- func WithRequestHeaders(headers map[string]string) RequestOption {
- return func(request *Request) {
- request.SetHeaders(headers)
- }
- }
- func WithRequestQueryParams(queryParams map[string]string) RequestOption {
- return func(request *Request) {
- request.SetQueryParams(queryParams)
- }
- }
- func WithRequestPathParams(pathParams map[string]string) RequestOption {
- return func(request *Request) {
- request.SetPathParams(pathParams)
- }
- }
- func WithRequestFiles(files map[string]string) RequestOption {
- return func(request *Request) {
- request.SetFiles(files)
- }
- }
- func WithRequestFileReader(paramName string, fileName string, reader io.Reader) RequestOption {
- return func(request *Request) {
- request.SetFileReader(paramName, fileName, reader)
- }
- }
- func WithRequestFormData(data map[string]string) RequestOption {
- return func(request *Request) {
- request.SetFormData(data)
- }
- }
- type Request struct {
- request *resty.Request
- }
- func (request *Request) SetHeaders(headers map[string]string) {
- if headers == nil || len(headers) == 0 {
- return
- }
- request.request.SetHeaders(headers)
- }
- func (request *Request) SetQueryParams(queryParams map[string]string) {
- if queryParams == nil || len(queryParams) == 0 {
- return
- }
- request.request.SetQueryParams(queryParams)
- }
- func (request *Request) SetPathParams(pathParams map[string]string) {
- if pathParams == nil || len(pathParams) == 0 {
- return
- }
- request.request.SetPathParams(pathParams)
- }
- func (request *Request) SetFiles(files map[string]string) {
- if files == nil || len(files) == 0 {
- return
- }
- request.request.SetFiles(files)
- }
- func (request *Request) SetFileReader(paramName string, fileName string, reader io.Reader) {
- if strutils.IsStringEmpty(paramName) || strutils.IsStringEmpty(fileName) || reader == nil {
- return
- }
- request.request.SetFileReader(paramName, fileName, reader)
- }
- func (request *Request) SetFormData(data map[string]string) {
- if data == nil || len(data) == 0 {
- return
- }
- request.request.SetFormData(data)
- }
- func (request *Request) Post(url string, body any, opts ...RequestOption) (*Response, error) {
- for _, opt := range opts {
- opt(request)
- }
- restyRequest := request.request
- if body != nil {
- restyRequest = restyRequest.SetBody(body)
- }
- resp, err := restyRequest.Post(url)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, errors.New(resp.Status())
- }
- return &Response{response: resp}, nil
- }
- func (request *Request) Delete(url string, opts ...RequestOption) (*Response, error) {
- for _, opt := range opts {
- opt(request)
- }
- restyRequest := request.request
- resp, err := restyRequest.Delete(url)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, errors.New(resp.Status())
- }
- return &Response{response: resp}, nil
- }
- func (request *Request) Put(url string, body any, opts ...RequestOption) (*Response, error) {
- for _, opt := range opts {
- opt(request)
- }
- restyRequest := request.request
- if body != nil {
- restyRequest = restyRequest.SetBody(body)
- }
- resp, err := restyRequest.Put(url)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, errors.New(resp.Status())
- }
- return &Response{response: resp}, nil
- }
- func (request *Request) Get(url string, opts ...RequestOption) (*Response, error) {
- for _, opt := range opts {
- opt(request)
- }
- restyRequest := request.request
- resp, err := restyRequest.Get(url)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, errors.New(resp.Status())
- }
- return &Response{response: resp}, nil
- }
- type Response struct {
- response *resty.Response
- }
- func (response *Response) Body() []byte {
- return response.response.Body()
- }
- func (response *Response) Json(resp any) error {
- body := response.response.Body()
- if body == nil || len(body) == 0 {
- return errors.New("响应体为空")
- }
- return json.Unmarshal(body, resp)
- }
|