mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 21:26:05 +03:00
untether HTTPClientBuilder from Engine
This commit is contained in:
parent
d047090307
commit
7186c1aa66
@ -137,7 +137,12 @@ func (e *Engine) Router() *gin.Engine {
|
|||||||
// BuildHTTPClient builds HTTP client with provided configuration
|
// BuildHTTPClient builds HTTP client with provided configuration
|
||||||
func (e *Engine) BuildHTTPClient(replaceDefault ...bool) *Engine {
|
func (e *Engine) BuildHTTPClient(replaceDefault ...bool) *Engine {
|
||||||
if e.Config.GetHTTPClientConfig() != nil {
|
if e.Config.GetHTTPClientConfig() != nil {
|
||||||
if client, err := NewHTTPClientBuilder().FromEngine(e).Build(replaceDefault...); err != nil {
|
client, err := NewHTTPClientBuilder().
|
||||||
|
WithLogger(e.Logger).
|
||||||
|
SetLogging(e.Config.IsDebug()).
|
||||||
|
FromEngine(e).Build(replaceDefault...)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
e.httpClient = client
|
e.httpClient = client
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/op/go-logging"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ type HTTPClientBuilder struct {
|
|||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
httpTransport *http.Transport
|
httpTransport *http.Transport
|
||||||
dialer *net.Dialer
|
dialer *net.Dialer
|
||||||
engine *Engine
|
logger *logging.Logger
|
||||||
built bool
|
built bool
|
||||||
logging bool
|
logging bool
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
@ -68,6 +69,15 @@ func NewHTTPClientBuilder() *HTTPClientBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithLogger sets provided logger into HTTPClientBuilder
|
||||||
|
func (b *HTTPClientBuilder) WithLogger(logger *logging.Logger) *HTTPClientBuilder {
|
||||||
|
if logger != nil {
|
||||||
|
b.logger = logger
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
// SetTimeout sets timeout for http client
|
// SetTimeout sets timeout for http client
|
||||||
func (b *HTTPClientBuilder) SetTimeout(timeout time.Duration) *HTTPClientBuilder {
|
func (b *HTTPClientBuilder) SetTimeout(timeout time.Duration) *HTTPClientBuilder {
|
||||||
timeout = timeout * time.Second
|
timeout = timeout * time.Second
|
||||||
@ -105,9 +115,9 @@ func (b *HTTPClientBuilder) SetSSLVerification(enabled bool) *HTTPClientBuilder
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnableLogging enables logging in mocks
|
// SetLogging enables or disables logging in mocks
|
||||||
func (b *HTTPClientBuilder) EnableLogging() *HTTPClientBuilder {
|
func (b *HTTPClientBuilder) SetLogging(flag bool) *HTTPClientBuilder {
|
||||||
b.logging = true
|
b.logging = flag
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,8 +143,6 @@ func (b *HTTPClientBuilder) FromConfig(config HTTPClientConfigInterface) *HTTPCl
|
|||||||
|
|
||||||
// FromEngine fulfills mock configuration from ConfigInterface inside Engine
|
// FromEngine fulfills mock configuration from ConfigInterface inside Engine
|
||||||
func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder {
|
func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder {
|
||||||
b.engine = engine
|
|
||||||
b.logging = engine.Config.IsDebug()
|
|
||||||
return b.FromConfig(engine.Config.GetHTTPClientConfig())
|
return b.FromConfig(engine.Config.GetHTTPClientConfig())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,8 +220,8 @@ func (b *HTTPClientBuilder) buildMocks() error {
|
|||||||
// logf prints logs via Engine or via fmt.Printf
|
// logf prints logs via Engine or via fmt.Printf
|
||||||
func (b *HTTPClientBuilder) logf(format string, args ...interface{}) {
|
func (b *HTTPClientBuilder) logf(format string, args ...interface{}) {
|
||||||
if b.logging {
|
if b.logging {
|
||||||
if b.engine != nil && b.engine.Logger != nil {
|
if b.logger != nil {
|
||||||
b.engine.Logger.Infof(format, args...)
|
b.logger.Infof(format, args...)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf(format, args...)
|
fmt.Printf(format, args...)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/op/go-logging"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,9 +27,12 @@ func (t *HTTPClientBuilderTest) Test_SetTimeout() {
|
|||||||
assert.Equal(t.T(), 90*time.Second, t.builder.httpClient.Timeout)
|
assert.Equal(t.T(), 90*time.Second, t.builder.httpClient.Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HTTPClientBuilderTest) Test_EnableLogging() {
|
func (t *HTTPClientBuilderTest) Test_SetLogging() {
|
||||||
t.builder.EnableLogging()
|
t.builder.SetLogging(true)
|
||||||
assert.Equal(t.T(), true, t.builder.logging)
|
assert.True(t.T(), t.builder.logging)
|
||||||
|
|
||||||
|
t.builder.SetLogging(false)
|
||||||
|
assert.False(t.T(), t.builder.logging)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HTTPClientBuilderTest) Test_SetMockAddress() {
|
func (t *HTTPClientBuilderTest) Test_SetMockAddress() {
|
||||||
@ -97,7 +102,7 @@ func (t *HTTPClientBuilderTest) Test_FromEngine() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.builder.FromEngine(engine)
|
t.builder.FromEngine(engine)
|
||||||
assert.NotNil(t.T(), engine, t.builder.engine)
|
assert.Equal(t.T(), engine.Config.GetHTTPClientConfig().GetMockAddress(), t.builder.mockAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HTTPClientBuilderTest) Test_buildDialer() {
|
func (t *HTTPClientBuilderTest) Test_buildDialer() {
|
||||||
@ -114,6 +119,18 @@ func (t *HTTPClientBuilderTest) Test_buildMocks() {
|
|||||||
assert.NoError(t.T(), t.builder.buildMocks())
|
assert.NoError(t.T(), t.builder.buildMocks())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *HTTPClientBuilderTest) Test_WithLogger() {
|
||||||
|
logger := NewLogger("telegram", logging.ERROR, DefaultLogFormatter())
|
||||||
|
builder := NewHTTPClientBuilder()
|
||||||
|
require.Nil(t.T(), builder.logger)
|
||||||
|
|
||||||
|
builder.WithLogger(nil)
|
||||||
|
assert.Nil(t.T(), builder.logger)
|
||||||
|
|
||||||
|
builder.WithLogger(logger)
|
||||||
|
assert.NotNil(t.T(), builder.logger)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *HTTPClientBuilderTest) Test_logf() {
|
func (t *HTTPClientBuilderTest) Test_logf() {
|
||||||
defer func() {
|
defer func() {
|
||||||
assert.Nil(t.T(), recover())
|
assert.Nil(t.T(), recover())
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// NewLogger will create new logger with specified formatter.
|
// NewLogger will create new logger with specified formatter.
|
||||||
// Usage:
|
// Usage:
|
||||||
// logger := NewLogger(config, DefaultLogFormatter())
|
// logger := NewLogger("telegram", logging.ERROR, DefaultLogFormatter())
|
||||||
func NewLogger(transportCode string, logLevel logging.Level, logFormat logging.Formatter) *logging.Logger {
|
func NewLogger(transportCode string, logLevel logging.Level, logFormat logging.Formatter) *logging.Logger {
|
||||||
logger := logging.MustGetLogger(transportCode)
|
logger := logging.MustGetLogger(transportCode)
|
||||||
logBackend := logging.NewLogBackend(os.Stdout, "", 0)
|
logBackend := logging.NewLogBackend(os.Stdout, "", 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user