Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set custom http client #67

Merged
merged 11 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion async_transport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rollbar

import (
"net/http"
"sync"
)

Expand All @@ -27,6 +28,7 @@ type AsyncTransport struct {
PrintPayloadOnError bool
bodyChannel chan payload
waitGroup sync.WaitGroup
httpClient *http.Client
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, wdyt about extracting duplicated code into BaseTransport struct and embedding it in both transports?

}

type payload struct {
Expand Down Expand Up @@ -150,5 +152,17 @@ func (t *AsyncTransport) SetPrintPayloadOnError(printPayloadOnError bool) {
}

func (t *AsyncTransport) post(p payload) (error, bool) {
return clientPost(t.Token, t.Endpoint, p.body, t.Logger)
return clientPost(t.Token, t.Endpoint, p.body, t.Logger, t.getHttpClient())
}

func (t *AsyncTransport) SetHttpClient(c *http.Client) {
t.httpClient = c
}

func (t *AsyncTransport) getHttpClient() *http.Client {
if t.httpClient != nil {
return t.httpClient
}

return http.DefaultClient
}
18 changes: 11 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func (c *Client) SetPerson(id, username, email string) {
}

c.diagnostic.configuredOptions["person"] = map[string]string{
"Id": id,
"Id": id,
"Username": username,
"Email": email,
"Email": email,
}
c.configuration.person = person
}
Expand Down Expand Up @@ -248,6 +248,10 @@ func (c *Client) SetPrintPayloadOnError(printPayloadOnError bool) {
c.Transport.SetPrintPayloadOnError(printPayloadOnError)
}

func (c *Client) SetHttpClient(httpClient *http.Client) {
c.Transport.SetHttpClient(httpClient)
}

// Token is the currently set Rollbar access token.
func (c *Client) Token() string {
return c.configuration.token
Expand Down Expand Up @@ -682,23 +686,23 @@ func createConfiguration(token, environment, codeVersion, serverHost, serverRoot
}

type diagnostic struct {
languageVersion string
languageVersion string
configuredOptions map[string]interface{}
}

func createDiagnostic() diagnostic {
return diagnostic{
languageVersion: runtime.Version(),
languageVersion: runtime.Version(),
configuredOptions: map[string]interface{}{},
}
}

// clientPost returns an error which indicates the type of error that occured while attempting to
// clientPost returns an error which indicates the type of error that occurred while attempting to
// send the body input to the endpoint given, or nil if no error occurred. If error is not nil, the
// boolean return parameter indicates whether the error is temporary or not. If this boolean return
// value is true then the caller could call this function again with the same input and possibly
// see a non-error response.
func clientPost(token, endpoint string, body map[string]interface{}, logger ClientLogger) (error, bool) {
func clientPost(token, endpoint string, body map[string]interface{}, logger ClientLogger, httpClient *http.Client) (error, bool) {
if len(token) == 0 {
rollbarError(logger, "empty token")
return nil, false
Expand All @@ -710,7 +714,7 @@ func clientPost(token, endpoint string, body map[string]interface{}, logger Clie
return err, false
}

resp, err := http.Post(endpoint, "application/json", bytes.NewReader(jsonBody))
resp, err := httpClient.Post(endpoint, "application/json", bytes.NewReader(jsonBody))
if err != nil {
rollbarError(logger, "POST failed: %s", err.Error())
return err, isTemporary(err)
Expand Down
4 changes: 4 additions & 0 deletions rollbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ func SetPrintPayloadOnError(printPayloadOnError bool) {
std.SetPrintPayloadOnError(printPayloadOnError)
}

func SetHttpClient(httpClient *http.Client) {
std.SetHttpClient(httpClient)
}

// -- Getters

// Token returns the currently set Rollbar access token on the managed Client instance.
Expand Down
17 changes: 16 additions & 1 deletion sync_transport.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rollbar

import "net/http"

// SyncTransport is a concrete implementation of the Transport type which communicates with the
// Rollbar API synchronously.
type SyncTransport struct {
Expand All @@ -18,6 +20,7 @@ type SyncTransport struct {
// PrintPayloadOnError is whether or not to output the payload to the set logger or to stderr if
// an error occurs during transport to the Rollbar API.
PrintPayloadOnError bool
httpClient *http.Client
}

// NewSyncTransport builds a synchronous transport which sends data to the Rollbar API at the
Expand All @@ -40,7 +43,7 @@ func (t *SyncTransport) Send(body map[string]interface{}) error {
}

func (t *SyncTransport) doSend(body map[string]interface{}, retriesLeft int) error {
err, canRetry := clientPost(t.Token, t.Endpoint, body, t.Logger)
err, canRetry := clientPost(t.Token, t.Endpoint, body, t.Logger, t.getHttpClient())
if err != nil {
if !canRetry || retriesLeft <= 0 {
if t.PrintPayloadOnError {
Expand Down Expand Up @@ -89,3 +92,15 @@ func (t *SyncTransport) SetRetryAttempts(retryAttempts int) {
func (t *SyncTransport) SetPrintPayloadOnError(printPayloadOnError bool) {
t.PrintPayloadOnError = printPayloadOnError
}

func (t *SyncTransport) SetHttpClient(c *http.Client) {
t.httpClient = c
}

func (t *SyncTransport) getHttpClient() *http.Client {
if t.httpClient != nil {
return t.httpClient
}

return http.DefaultClient
}
3 changes: 3 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
)

Expand Down Expand Up @@ -37,6 +38,8 @@ type Transport interface {
SetRetryAttempts(retryAttempts int)
// Set whether to print the payload to the set logger or to stderr upon failing to send.
SetPrintPayloadOnError(printPayloadOnError bool)
// Set custom http client instead of http.DefaultClient
SetHttpClient(httpClient *http.Client)
}

// ClientLogger is the interface used by the rollbar Client/Transport to report problems.
Expand Down