Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaputra07 committed Nov 10, 2024
1 parent 44d1da9 commit 817ed6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
28 changes: 14 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
}

0 comments on commit 817ed6e

Please sign in to comment.