variable.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package httpclent
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "net/url"
  10. "time"
  11. "git.shuncheng.lu/bigthing/gocommon/pkg/net/auth"
  12. "git.shuncheng.lu/bigthing/gocommon/pkg/net/nacos"
  13. "net/http"
  14. )
  15. var (
  16. NacosServerHostOption Option = func(options *Options) {
  17. options.ServerHostUrl = NacosServerHost
  18. }
  19. QconfServerHostOption Option = func(options *Options) {
  20. options.ServerHostUrl = DefaultServerHost
  21. }
  22. DomainServerHostOption Option = func(options *Options) {
  23. options.ServerHostUrl = DomainServerHost
  24. }
  25. )
  26. var (
  27. SetUrlRequestParams = func(values url.Values) HandlerRequestUrl {
  28. return func(ctx context.Context, _url *url.URL) error {
  29. _url.RawQuery = values.Encode()
  30. return nil
  31. }
  32. }
  33. )
  34. var (
  35. DefaultTimeOut2sOp = func(op *Options) {
  36. op.TimeOut = 2 * time.Second
  37. }
  38. DefaultTimeOut3sOp = func(op *Options) {
  39. op.TimeOut = 3 * time.Second
  40. }
  41. DefaultTimeOut4sOp = func(op *Options) {
  42. op.TimeOut = 4 * time.Second
  43. }
  44. DefaultTimeOut5sOp = func(op *Options) {
  45. op.TimeOut = 5 * time.Second
  46. }
  47. // 关闭重试
  48. CloseRetry = func(op *Options) {
  49. op.RetryMaxNum = -1
  50. }
  51. // 默认处理
  52. DefaultHandlerRequestParams HandlerRequestParams = func(ctx context.Context, oldReq interface{}) (newReq interface{}) {
  53. return RequestParams{"params": oldReq}
  54. }
  55. // 默认处理
  56. DefaultHandlerRequestHandler HandlerRequestHeader = func(ctx context.Context, header http.Header) {
  57. header.Add("Content-Type", "application/json")
  58. header.Add("Service-Token", auth.GenerateToken(getServerName(ctx)))
  59. }
  60. DefaultServerHost ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) {
  61. return GetServerUrl(serverName)
  62. }
  63. NacosServerHost ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) {
  64. return nacos.GetHost(serverName)
  65. }
  66. DomainServerHost ServerHostUrl = func(ctx context.Context, serverName string) (s string, e error) {
  67. return nacos.GetDomainHost(serverName)
  68. }
  69. DefaultEncodeRequestBody EncodeRequestBody = func(ctx context.Context, params interface{}) (reader io.Reader, e error) {
  70. if params == nil {
  71. return nil, nil
  72. }
  73. buf := bytes.Buffer{}
  74. if err := json.NewEncoder(&buf).Encode(params); err != nil {
  75. return nil, fmtError("[DefaultEncodeRequestBody] err,err=%+v,req=%+v", err, params)
  76. }
  77. return ioutil.NopCloser(&buf), nil
  78. }
  79. )
  80. var (
  81. // error
  82. validateNilError = errors.New("the result can not be null")
  83. validateTypeError = errors.New("the result must be a pointer type")
  84. )
  85. const (
  86. defaultRetryNum = 2 //默认重试2次,带上第一次请求,一共请求3次
  87. defaultMethod = MethodPost //默认Post请求
  88. defaultScheme = SchemeHttp
  89. defaultTimeOut1s = 1 * time.Second //默认超时时间1s
  90. defaultRetryIdleTime = 1 * time.Millisecond // 默认空闲时间是1ms
  91. )