Skip to content

Commit

Permalink
Merge endpoint package into proxy package (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Aug 7, 2019
1 parent c8ac25c commit ef36972
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
42 changes: 18 additions & 24 deletions pkg/endpoint/client.go → pkg/proxy/endpoint.go
@@ -1,4 +1,4 @@
package endpoint
package proxy

import (
"bytes"
Expand All @@ -13,45 +13,45 @@ import (
// Public types
//

// Config contains the optional configuration parameters of a Client.
type Config struct {
// EndpointConfig contains the optional configuration parameters of an EndpointClient.
type EndpointConfig struct {
HTTPClient *http.Client

Log *log.Logger

ResponseHandler ResponseHandler
ResponseHandler EndpointResponseHandler
}

// ResponseHandler handles a response from the endpoint.
type ResponseHandler interface {
type EndpointResponseHandler interface {
ProcessResponse(string, *http.Response)
}

// ResponseHandlerFunc is an adapter to allow the use of ordinary
// functions as response handlers. If f is a function with the
// appropriate signature, ResponseHandler(f) is a
// ResponseHandler that calls f.
type ResponseHandlerFunc func(string, *http.Response)
type EndpointResponseHandlerFunc func(string, *http.Response)

// ProcessResponse calls f(webhookID, resp).
func (f ResponseHandlerFunc) ProcessResponse(webhookID string, resp *http.Response) {
func (f EndpointResponseHandlerFunc) ProcessResponse(webhookID string, resp *http.Response) {
f(webhookID, resp)
}

// Client is the client used to POST webhook requests to the local endpoint.
type Client struct {
// EndpointClient is the client used to POST webhook requests to the local endpoint.
type EndpointClient struct {
// URL the client sends POST requests to
URL string

events map[string]bool

// Optional configuration parameters
cfg *Config
cfg *EndpointConfig
}

// SupportsEventType takes an event of a webhook and compares it to the internal
// list of supported events
func (c *Client) SupportsEventType(eventType string) bool {
func (c *EndpointClient) SupportsEventType(eventType string) bool {
// Endpoint supports all events, always return true
if c.events["*"] || c.events[eventType] {
return true
Expand All @@ -61,9 +61,9 @@ func (c *Client) SupportsEventType(eventType string) bool {
}

// Post sends a message to the local endpoint.
func (c *Client) Post(webhookID string, body string, headers map[string]string) error {
func (c *EndpointClient) Post(webhookID string, body string, headers map[string]string) error {
c.cfg.Log.WithFields(log.Fields{
"prefix": "endpoint.Client.Post",
"prefix": "proxy.EndpointClient.Post",
}).Debug("Forwarding event to local endpoint")

req, err := http.NewRequest(http.MethodPost, c.URL, bytes.NewBuffer([]byte(body)))
Expand All @@ -90,10 +90,10 @@ func (c *Client) Post(webhookID string, body string, headers map[string]string)
// Public functions
//

// NewClient returns a new Client.
func NewClient(url string, events []string, cfg *Config) *Client {
// NewEndpointClient returns a new EndpointClient.
func NewEndpointClient(url string, events []string, cfg *EndpointConfig) *EndpointClient {
if cfg == nil {
cfg = &Config{}
cfg = &EndpointConfig{}
}
if cfg.Log == nil {
cfg.Log = &log.Logger{Out: ioutil.Discard}
Expand All @@ -104,10 +104,10 @@ func NewClient(url string, events []string, cfg *Config) *Client {
}
}
if cfg.ResponseHandler == nil {
cfg.ResponseHandler = nullResponseHandler
cfg.ResponseHandler = EndpointResponseHandlerFunc(func(string, *http.Response) {})
}

return &Client{
return &EndpointClient{
URL: url,
events: convertToMap(events),
cfg: cfg,
Expand All @@ -122,12 +122,6 @@ const (
defaultTimeout = 30 * time.Second
)

//
// Private variables
//

var nullResponseHandler = ResponseHandlerFunc(func(string, *http.Response) {})

//
// Private functions
//
Expand Down
8 changes: 4 additions & 4 deletions pkg/endpoint/client_test.go → pkg/proxy/endpoint_test.go
@@ -1,4 +1,4 @@
package endpoint
package proxy

import (
"io/ioutil"
Expand Down Expand Up @@ -30,11 +30,11 @@ func TestClientHandler(t *testing.T) {

rcvBody := ""
rcvWebhookID := ""
client := NewClient(
client := NewEndpointClient(
ts.URL,
[]string{"*"},
&Config{
ResponseHandler: ResponseHandlerFunc(func(webhookID string, resp *http.Response) {
&EndpointConfig{
ResponseHandler: EndpointResponseHandlerFunc(func(webhookID string, resp *http.Response) {
buf, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)

Expand Down
9 changes: 4 additions & 5 deletions pkg/proxy/proxy.go
Expand Up @@ -13,7 +13,6 @@ import (
log "github.com/sirupsen/logrus"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/endpoint"
"github.com/stripe/stripe-cli/pkg/stripeauth"
"github.com/stripe/stripe-cli/pkg/websocket"
)
Expand Down Expand Up @@ -56,7 +55,7 @@ type Config struct {
type Proxy struct {
cfg *Config

endpointClients []*endpoint.Client
endpointClients []*EndpointClient
stripeAuthClient *stripeauth.Client
webSocketClient *websocket.Client

Expand Down Expand Up @@ -226,12 +225,12 @@ func New(cfg *Config) *Proxy {

for url, events := range cfg.EndpointsMap {
// append to endpointClients
p.endpointClients = append(p.endpointClients, endpoint.NewClient(
p.endpointClients = append(p.endpointClients, NewEndpointClient(
url,
events,
&endpoint.Config{
&EndpointConfig{
Log: p.cfg.Log,
ResponseHandler: endpoint.ResponseHandlerFunc(p.processEndpointResponse),
ResponseHandler: EndpointResponseHandlerFunc(p.processEndpointResponse),
},
))
}
Expand Down

0 comments on commit ef36972

Please sign in to comment.