type.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package httpclent
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "sync"
  8. "time"
  9. )
  10. const (
  11. SchemeHttp Scheme = "http"
  12. SchemeHttps Scheme = "https"
  13. SchemeFtp Scheme = "ftp"
  14. SchemeWs Scheme = "ws"
  15. SchemeWss Scheme = "wss"
  16. )
  17. /**
  18. header or const
  19. */
  20. const (
  21. _ServerName = "server-name"
  22. )
  23. // http 协议方式
  24. type Scheme string
  25. type HttpMethod string
  26. func (s Scheme) NewUrl(host string) string {
  27. return string(s) + host
  28. }
  29. const (
  30. MethodGet HttpMethod = "GET"
  31. MethodHead HttpMethod = "HEAD"
  32. MethodPost HttpMethod = "POST"
  33. MethodPut HttpMethod = "PUT"
  34. MethodPatch HttpMethod = "PATCH" // RFC 5789
  35. MethodDelete HttpMethod = "DELETE"
  36. MethodConnect HttpMethod = "CONNECT"
  37. MethodOptions HttpMethod = "OPTIONS"
  38. MethodTrace HttpMethod = "TRACE"
  39. )
  40. // 请求参数
  41. type RequestParams map[string]interface{}
  42. // 创建http request
  43. type NewHttpRequestFunc func() (*http.Request, error)
  44. // 设置请求header,在请求之前设置
  45. type HandlerRequestHeader func(ctx context.Context, header http.Header)
  46. // 添加request-cookie,requestCookie是请求中已经带有的Cookie,不能返回requestCookie
  47. type AddRequestCookies func(ctx context.Context, requestCookie []*http.Cookie) []*http.Cookie
  48. // 设置请求体,在请求之前设置,oldReq传入的req,newReq传出的
  49. type HandlerRequestParams func(ctx context.Context, oldReq interface{}) (newReq interface{})
  50. // 获取server_name 对应的uri
  51. type ServerHostUrl func(ctx context.Context, serverName string) (string, error)
  52. type HandlerResponse func(ctx context.Context, response *http.Response) error
  53. type HandlerRequestUrl func(ctx context.Context, _url *url.URL) error
  54. type EncodeRequestBody func(ctx context.Context, params interface{}) (io.Reader, error)
  55. var (
  56. // 降低内存开销,切记使用pool记得用指针类型,不然会有额外的内存开销!
  57. optionsPool = sync.Pool{
  58. New: func() interface{} {
  59. return new(Options)
  60. },
  61. }
  62. )
  63. type Option func(*Options)
  64. type Options struct {
  65. // Client客户端配置
  66. TimeOut time.Duration // 超时时间,默认1s
  67. RetryMaxNum int // 重试次数,默认重试2次,如果值设置为0则不重试
  68. RetryIdleTime time.Duration // 再次重试的等待时间,默认1ms
  69. HandlerRequestHeader HandlerRequestHeader // 处理请求头,默认添加 content-type:application/json
  70. // Deprecated
  71. HandlerRequestParams HandlerRequestParams // 处理请求参数,默认添加 params,不推荐使用,推荐直接使用 EncodeRequestBody
  72. ServerHostUrl ServerHostUrl // 获取请求IP:PORT,可以为空
  73. Scheme Scheme // http协议方式,默认http
  74. AddRequestCookies AddRequestCookies // 处理http-request
  75. HttpMethod HttpMethod // Http请求方式,默认POST
  76. HandlerRequestUrl HandlerRequestUrl // 处理请求url,可以添加url的请求参数
  77. EncodeRequestBody EncodeRequestBody // 解析请求参数,默认是Json
  78. HandlerResponse HandlerResponse // 处理响应
  79. }
  80. // 加载OP-CONFIG,使用 sync.Pool 可以降低内存开销!记得用完调用FreeOp
  81. func LoadDefaultOp(option ...Option) *Options {
  82. op := optionsPool.Get().(*Options)
  83. // init
  84. op.TimeOut = 0
  85. op.RetryMaxNum = 0
  86. op.RetryIdleTime = 0
  87. op.HandlerRequestHeader = nil
  88. op.HandlerRequestParams = nil
  89. op.ServerHostUrl = nil
  90. op.Scheme = ""
  91. op.AddRequestCookies = nil
  92. op.HttpMethod = ""
  93. op.HandlerRequestUrl = nil
  94. op.EncodeRequestBody = nil
  95. op.HandlerResponse = nil
  96. for _, elem := range option {
  97. if elem != nil {
  98. elem(op)
  99. }
  100. }
  101. return op
  102. }
  103. func FreeOp(options *Options) {
  104. if options != nil {
  105. optionsPool.Put(options)
  106. }
  107. }