From 817ed6ee02f9ece9a5cb486d881e5e493b014bd7 Mon Sep 17 00:00:00 2001 From: Eka Putra Date: Sun, 10 Nov 2024 23:49:06 +0800 Subject: [PATCH] updated readme --- README.md | 4 ++-- api/api.go | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 532cef6..7c9e002 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ Above method works well if you're trying to connect to a single hosting provider You can create multiple instances of Warren that points to different providers: ```golang import ( - "context" - "github.com/ekaputra07/warren-go" + "context" + "github.com/ekaputra07/warren-go" "github.com/ekaputra07/warren-go/api" ) diff --git a/api/api.go b/api/api.go index 38fa8d1..ffb2658 100644 --- a/api/api.go +++ b/api/api.go @@ -34,42 +34,42 @@ type API struct { } // FormRequest make a call with form-encoded payload -func (c *API) FormRequest(ctx context.Context, cfg RequestConfig) *ClientResponse { - req, err := c.buildRequest(ctx, cfg) +func (a *API) FormRequest(ctx context.Context, cfg RequestConfig) *ClientResponse { + req, err := a.buildRequest(ctx, cfg) if err != nil { return &ClientResponse{Error: err} } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - return c.doRequest(req) + return a.doRequest(req) } // JsonRequest make a call with json-encoded payload -func (c *API) JSONRequest(ctx context.Context, cfg RequestConfig) *ClientResponse { - req, err := c.buildRequest(ctx, cfg) +func (a *API) JSONRequest(ctx context.Context, cfg RequestConfig) *ClientResponse { + req, err := a.buildRequest(ctx, cfg) if err != nil { return &ClientResponse{Error: err} } req.Header.Set("Content-Type", "application/json") - return c.doRequest(req) + return a.doRequest(req) } // buildRequest wraps `http.NewRequestWithContext` and set necessary header for authentication. -func (c *API) buildRequest(ctx context.Context, cfg RequestConfig) (*http.Request, error) { +func (a *API) buildRequest(ctx context.Context, cfg RequestConfig) (*http.Request, error) { body, err := cfg.body() if err != nil { return nil, err } - req, err := http.NewRequestWithContext(ctx, strings.ToUpper(cfg.Method), cfg.url(c.BaseURL), body) + req, err := http.NewRequestWithContext(ctx, strings.ToUpper(cfg.Method), cfg.url(a.BaseURL), body) if err != nil { return nil, err } - req.Header.Set("apikey", c.APIKey) + req.Header.Set("apikey", a.APIKey) return req, nil } // doRequest doing the actual request -func (c *API) doRequest(req *http.Request) *ClientResponse { - res, err := c.HTTPClient.Do(req) +func (a *API) doRequest(req *http.Request) *ClientResponse { + res, err := a.HTTPClient.Do(req) if err != nil { return &ClientResponse{Error: err} } @@ -101,13 +101,13 @@ func New(baseURL, apiKey string) *API { } } -// MockClientServer returns client and test server to simplify API call testing +// MockClientServer returns API client and test server to simplify API call testing func MockClientServer(fn func(w http.ResponseWriter, r *http.Request)) (*API, *httptest.Server) { s := httptest.NewServer(http.HandlerFunc(fn)) - c := &API{ + a := &API{ APIKey: "secret", BaseURL: s.URL, HTTPClient: s.Client(), } - return c, s + return a, s }