feat: make http transport client exchangeable

This commit is contained in:
franklin 2021-07-30 17:37:10 +02:00
parent ae29f1060c
commit 93fcca1a5f
3 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package client
import (
"net/http"
"time"
"github.com/foomo/contentserver/content"
@ -18,16 +19,26 @@ func NewClient(
server string,
connectionPoolSize int,
waitTimeout time.Duration,
) (c *Client, err error) {
return NewClientWithTransport(NewSocketTransport(server, connectionPoolSize, waitTimeout))
}
func NewClientWithTransport(
transport transport,
) (c *Client, err error) {
c = &Client{
t: newSocketTransport(server, connectionPoolSize, waitTimeout),
t: transport,
}
return
}
func NewHTTPClient(server string) (c *Client, err error) {
return NewHTTPClientWithTransport(NewHTTPTransport(server, http.DefaultClient))
}
func NewHTTPClientWithTransport(transport transport) (c *Client, err error) {
c = &Client{
t: newHTTPTransport(server),
t: transport,
}
return
}

View File

@ -14,10 +14,10 @@ type httpTransport struct {
endpoint string
}
func newHTTPTransport(server string) transport {
func NewHTTPTransport(server string, client *http.Client) transport {
return &httpTransport{
endpoint: server,
client: http.DefaultClient,
client: client,
}
}

View File

@ -24,7 +24,7 @@ type socketTransport struct {
connPool *connectionPool
}
func newSocketTransport(server string, connectionPoolSize int, waitTimeout time.Duration) transport {
func NewSocketTransport(server string, connectionPoolSize int, waitTimeout time.Duration) transport {
return &socketTransport{
connPool: newConnectionPool(server, connectionPoolSize, waitTimeout),
}