Refactor: move http client to the separate package
This commit is contained in:
29
pkg/httpclient/httpclient.go
Normal file
29
pkg/httpclient/httpclient.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package httpclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewHttpClient returns new http client
|
||||||
|
func NewHttpClient() *http.Client {
|
||||||
|
return &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
||||||
|
},
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
MaxIdleConns: 100,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
},
|
||||||
|
Timeout: time.Duration(time.Second * 30),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,17 +3,15 @@ package zabbixapi
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/alexanderzobnin/grafana-zabbix/pkg/httpclient"
|
||||||
"github.com/bitly/go-simplejson"
|
"github.com/bitly/go-simplejson"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||||
"golang.org/x/net/context/ctxhttp"
|
"golang.org/x/net/context/ctxhttp"
|
||||||
@@ -32,26 +30,6 @@ type ZabbixAPI struct {
|
|||||||
|
|
||||||
type ZabbixAPIParams = map[string]interface{}
|
type ZabbixAPIParams = map[string]interface{}
|
||||||
|
|
||||||
func newHttpClient() *http.Client {
|
|
||||||
return &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{
|
|
||||||
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
||||||
},
|
|
||||||
Proxy: http.ProxyFromEnvironment,
|
|
||||||
Dial: (&net.Dialer{
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
MaxIdleConns: 100,
|
|
||||||
IdleConnTimeout: 90 * time.Second,
|
|
||||||
},
|
|
||||||
Timeout: time.Duration(time.Second * 30),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns new ZabbixAPI instance initialized with given URL or error.
|
// New returns new ZabbixAPI instance initialized with given URL or error.
|
||||||
func New(api_url string) (*ZabbixAPI, error) {
|
func New(api_url string) (*ZabbixAPI, error) {
|
||||||
apiLogger := log.New()
|
apiLogger := log.New()
|
||||||
@@ -63,7 +41,7 @@ func New(api_url string) (*ZabbixAPI, error) {
|
|||||||
return &ZabbixAPI{
|
return &ZabbixAPI{
|
||||||
url: zabbixURL,
|
url: zabbixURL,
|
||||||
logger: apiLogger,
|
logger: apiLogger,
|
||||||
httpClient: newHttpClient(),
|
httpClient: httpclient.NewHttpClient(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user