options.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package http
  5. import (
  6. "crypto/tls"
  7. "net"
  8. "net/http"
  9. "time"
  10. )
  11. // Options holds http configuration properties for communicating with InfluxDB server
  12. type Options struct {
  13. // HTTP client. Default is http.DefaultClient.
  14. httpClient *http.Client
  15. // doer is an http Doer - if set it overrides httpClient
  16. doer Doer
  17. // Flag whether http client was created internally
  18. ownClient bool
  19. // TLS configuration for secure connection. Default nil
  20. tlsConfig *tls.Config
  21. // HTTP request timeout in sec. Default 20
  22. httpRequestTimeout uint
  23. // Application name in the User-Agent HTTP header string
  24. appName string
  25. }
  26. // HTTPClient returns the http.Client that is configured to be used
  27. // for HTTP requests. It will return the one that has been set using
  28. // SetHTTPClient or it will construct a default client using the
  29. // other configured options.
  30. // HTTPClient panics if SetHTTPDoer was called.
  31. func (o *Options) HTTPClient() *http.Client {
  32. if o.doer != nil {
  33. panic("HTTPClient called after SetHTTPDoer")
  34. }
  35. if o.httpClient == nil {
  36. o.httpClient = &http.Client{
  37. Timeout: time.Second * time.Duration(o.HTTPRequestTimeout()),
  38. Transport: &http.Transport{
  39. Proxy: http.ProxyFromEnvironment,
  40. DialContext: (&net.Dialer{
  41. Timeout: 5 * time.Second,
  42. }).DialContext,
  43. TLSHandshakeTimeout: 5 * time.Second,
  44. TLSClientConfig: o.TLSConfig(),
  45. MaxIdleConns: 100,
  46. MaxIdleConnsPerHost: 100,
  47. IdleConnTimeout: 90 * time.Second,
  48. },
  49. }
  50. o.ownClient = true
  51. }
  52. return o.httpClient
  53. }
  54. // SetHTTPClient will configure the http.Client that is used
  55. // for HTTP requests. If set to nil, an HTTPClient will be
  56. // generated.
  57. //
  58. // Setting the HTTPClient will cause the other HTTP options
  59. // to be ignored.
  60. // In case of UsersAPI.SignIn() is used, HTTPClient.Jar will be used for storing session cookie.
  61. func (o *Options) SetHTTPClient(c *http.Client) *Options {
  62. o.httpClient = c
  63. o.ownClient = false
  64. return o
  65. }
  66. // OwnHTTPClient returns true of HTTP client was created internally. False if it was set externally.
  67. func (o *Options) OwnHTTPClient() bool {
  68. return o.ownClient
  69. }
  70. // Doer allows proving custom Do for HTTP operations
  71. type Doer interface {
  72. Do(*http.Request) (*http.Response, error)
  73. }
  74. // SetHTTPDoer will configure the http.Client that is used
  75. // for HTTP requests. If set to nil, this has no effect.
  76. //
  77. // Setting the HTTPDoer will cause the other HTTP options
  78. // to be ignored.
  79. func (o *Options) SetHTTPDoer(d Doer) *Options {
  80. if d != nil {
  81. o.doer = d
  82. o.ownClient = false
  83. }
  84. return o
  85. }
  86. // HTTPDoer returns actual Doer if set, or http.Client
  87. func (o *Options) HTTPDoer() Doer {
  88. if o.doer != nil {
  89. return o.doer
  90. }
  91. return o.HTTPClient()
  92. }
  93. // TLSConfig returns tls.Config
  94. func (o *Options) TLSConfig() *tls.Config {
  95. return o.tlsConfig
  96. }
  97. // SetTLSConfig sets TLS configuration for secure connection
  98. func (o *Options) SetTLSConfig(tlsConfig *tls.Config) *Options {
  99. o.tlsConfig = tlsConfig
  100. return o
  101. }
  102. // HTTPRequestTimeout returns HTTP request timeout
  103. func (o *Options) HTTPRequestTimeout() uint {
  104. return o.httpRequestTimeout
  105. }
  106. // SetHTTPRequestTimeout sets HTTP request timeout in sec
  107. func (o *Options) SetHTTPRequestTimeout(httpRequestTimeout uint) *Options {
  108. o.httpRequestTimeout = httpRequestTimeout
  109. return o
  110. }
  111. // ApplicationName returns application name used in the User-Agent HTTP header
  112. func (o *Options) ApplicationName() string {
  113. return o.appName
  114. }
  115. // SetApplicationName sets an application name to the User-Agent HTTP header
  116. func (o *Options) SetApplicationName(appName string) *Options {
  117. o.appName = appName
  118. return o
  119. }
  120. // DefaultOptions returns Options object with default values
  121. func DefaultOptions() *Options {
  122. return &Options{httpRequestTimeout: 20}
  123. }