From ef369723be6fa946efdfeae0dd93e4ad4d8c6520 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 6 Aug 2019 17:39:28 -0700 Subject: [PATCH] Merge endpoint package into proxy package (#78) --- pkg/{endpoint/client.go => proxy/endpoint.go} | 42 ++++++++----------- .../client_test.go => proxy/endpoint_test.go} | 8 ++-- pkg/proxy/proxy.go | 9 ++-- 3 files changed, 26 insertions(+), 33 deletions(-) rename pkg/{endpoint/client.go => proxy/endpoint.go} (67%) rename pkg/{endpoint/client_test.go => proxy/endpoint_test.go} (88%) diff --git a/pkg/endpoint/client.go b/pkg/proxy/endpoint.go similarity index 67% rename from pkg/endpoint/client.go rename to pkg/proxy/endpoint.go index 9a1867db6..4cb4ade64 100644 --- a/pkg/endpoint/client.go +++ b/pkg/proxy/endpoint.go @@ -1,4 +1,4 @@ -package endpoint +package proxy import ( "bytes" @@ -13,17 +13,17 @@ 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) } @@ -31,27 +31,27 @@ type ResponseHandler interface { // 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 @@ -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))) @@ -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} @@ -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, @@ -122,12 +122,6 @@ const ( defaultTimeout = 30 * time.Second ) -// -// Private variables -// - -var nullResponseHandler = ResponseHandlerFunc(func(string, *http.Response) {}) - // // Private functions // diff --git a/pkg/endpoint/client_test.go b/pkg/proxy/endpoint_test.go similarity index 88% rename from pkg/endpoint/client_test.go rename to pkg/proxy/endpoint_test.go index 89cf7bf53..6c492669f 100644 --- a/pkg/endpoint/client_test.go +++ b/pkg/proxy/endpoint_test.go @@ -1,4 +1,4 @@ -package endpoint +package proxy import ( "io/ioutil" @@ -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) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index c6a0786f2..a4f0f1c8a 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -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" ) @@ -56,7 +55,7 @@ type Config struct { type Proxy struct { cfg *Config - endpointClients []*endpoint.Client + endpointClients []*EndpointClient stripeAuthClient *stripeauth.Client webSocketClient *websocket.Client @@ -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), }, )) }