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

Refactor: Replace hardcoded strings with constants and update dependencies #578

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions fastly/account_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io"
"testing"

"github.com/google/jsonapi"
)

func TestClient_APIEvents(t *testing.T) {
Expand Down Expand Up @@ -65,12 +67,12 @@ func TestGetAPIEventsFilterInput_formatFilters(t *testing.T) {
PageNumber: 2,
},
expected: map[string]string{
"filter[customer_id]": "65135846153687547",
"filter[service_id]": "5343548168357658",
"filter[event_type]": "version.activate",
"filter[user_id]": "654681384354746951",
"page[size]": "1",
"page[number]": "2",
"filter[customer_id]": "65135846153687547",
"filter[service_id]": "5343548168357658",
"filter[event_type]": "version.activate",
"filter[user_id]": "654681384354746951",
jsonapi.QueryParamPageSize: "1",
jsonapi.QueryParamPageNumber: "2",
},
},
}
Expand Down
19 changes: 10 additions & 9 deletions fastly/account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func (c *Client) GetAPIEvent(i *GetAPIEventInput) (*Event, error) {
}

// interpretAPIEventsPage accepts a Fastly response for a set of WAF rule statuses
// and unmarshals the results. If there are more pages of results, it fetches the next
// and unmarshal the results.
// If there are more pages of results, it fetches the next
// page, adds that response to the array of results, and repeats until all results have
// been fetched.
func (c *Client) interpretAPIEventsPage(answer *GetAPIEventsResponse, pageNum int, received *http.Response) error {
Expand Down Expand Up @@ -174,12 +175,12 @@ func getEventsPages(body io.Reader) (EventsPaginationInfo, io.Reader, error) {
func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string {
result := map[string]string{}
pairings := map[string]any{
"filter[customer_id]": i.CustomerID,
"filter[service_id]": i.ServiceID,
"filter[event_type]": i.EventType,
"filter[user_id]": i.UserID,
"page[size]": i.MaxResults,
"page[number]": i.PageNumber, // starts at 1, not 0
"filter[customer_id]": i.CustomerID,
"filter[service_id]": i.ServiceID,
"filter[event_type]": i.EventType,
"filter[user_id]": i.UserID,
jsonapi.QueryParamPageSize: i.MaxResults,
jsonapi.QueryParamPageNumber: i.PageNumber, // starts at 1, not 0
}
// NOTE: This setup means we will not be able to send the zero value
// of any of these filters. It doesn't appear we would need to at present.
Expand All @@ -188,12 +189,12 @@ func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string {
switch t := reflect.TypeOf(value).String(); t {
case "string":
if value != "" {
v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type)
v, _ := value.(string) // type asserts to avoid runtime panic (v will have zero value for its type)
result[key] = v
}
case "int":
if value != 0 {
v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type)
v, _ := value.(int) // type asserts to avoid runtime panic (v will have zero value for its type)
result[key] = strconv.Itoa(v)
}
}
Expand Down
3 changes: 2 additions & 1 deletion fastly/acl_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fastly

import (
"fmt"
"net/http"
"time"
)

Expand Down Expand Up @@ -244,7 +245,7 @@ func (c *Client) UpdateACLEntry(i *UpdateACLEntryInput) (*ACLEntry, error) {

path := ToSafeURL("service", i.ServiceID, "acl", i.ACLID, "entry", i.EntryID)

resp, err := c.RequestForm("PATCH", path, i, nil)
resp, err := c.RequestForm(http.MethodPatch, path, i, nil)
if err != nil {
return nil, err
}
Expand Down
54 changes: 27 additions & 27 deletions fastly/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ type Client struct {

// apiKey is the Fastly API key to authenticate requests.
apiKey string
// remaining is last observed value of http header Fastly-RateLimit-Remaining
// remaining is the last observed value of http header Fastly-RateLimit-Remaining
remaining int
// reset is last observed value of http header Fastly-RateLimit-Reset
// reset is the last observed value of http header Fastly-RateLimit-Reset
reset int64
// updateLock forces serialization of calls that modify a service.
// Concurrent modifications have undefined semantics.
Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *Client) Get(p string, ro *RequestOptions) (*http.Response, error) {
ro = new(RequestOptions)
}
ro.Parallel = true
return c.Request("GET", p, ro)
return c.Request(http.MethodGet, p, ro)
}

// GetJSON issues an HTTP GET request and indicates that the response
Expand All @@ -221,7 +221,7 @@ func (c *Client) GetJSON(p string, ro *RequestOptions) (*http.Response, error) {
}
ro.Parallel = true
ro.Headers["Accept"] = JSONMimeType
return c.Request("GET", p, ro)
return c.Request(http.MethodGet, p, ro)
}

// Head issues an HTTP HEAD request.
Expand All @@ -230,97 +230,97 @@ func (c *Client) Head(p string, ro *RequestOptions) (*http.Response, error) {
ro = new(RequestOptions)
}
ro.Parallel = true
return c.Request("HEAD", p, ro)
return c.Request(http.MethodHead, p, ro)
}

// Patch issues an HTTP PATCH request.
func (c *Client) Patch(p string, ro *RequestOptions) (*http.Response, error) {
return c.Request("PATCH", p, ro)
return c.Request(http.MethodPatch, p, ro)
}

// PatchForm issues an HTTP PUT request with the given interface form-encoded.
func (c *Client) PatchForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("PATCH", p, i, ro)
return c.RequestForm(http.MethodPatch, p, i, ro)
}

// PatchJSON issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PatchJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("PATCH", p, i, ro)
return c.RequestJSON(http.MethodPatch, p, i, ro)
}

// PatchJSONAPI issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PatchJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("PATCH", p, i, ro)
return c.RequestJSONAPI(http.MethodPatch, p, i, ro)
}

// Post issues an HTTP POST request.
func (c *Client) Post(p string, ro *RequestOptions) (*http.Response, error) {
return c.Request("POST", p, ro)
return c.Request(http.MethodPost, p, ro)
}

// PostForm issues an HTTP POST request with the given interface form-encoded.
func (c *Client) PostForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("POST", p, i, ro)
return c.RequestForm(http.MethodPost, p, i, ro)
}

// PostJSON issues an HTTP POST request with the given interface json-encoded.
func (c *Client) PostJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("POST", p, i, ro)
return c.RequestJSON(http.MethodPost, p, i, ro)
}

// PostJSONAPI issues an HTTP POST request with the given interface json-encoded.
func (c *Client) PostJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("POST", p, i, ro)
return c.RequestJSONAPI(http.MethodPost, p, i, ro)
}

// PostJSONAPIBulk issues an HTTP POST request with the given interface json-encoded and bulk requests.
func (c *Client) PostJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPIBulk("POST", p, i, ro)
return c.RequestJSONAPIBulk(http.MethodPost, p, i, ro)
}

// Put issues an HTTP PUT request.
func (c *Client) Put(p string, ro *RequestOptions) (*http.Response, error) {
return c.Request("PUT", p, ro)
return c.Request(http.MethodPut, p, ro)
}

// PutForm issues an HTTP PUT request with the given interface form-encoded.
func (c *Client) PutForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("PUT", p, i, ro)
return c.RequestForm(http.MethodPut, p, i, ro)
}

// PutFormFile issues an HTTP PUT request (multipart/form-encoded) to put a file to an endpoint.
func (c *Client) PutFormFile(urlPath, filePath, fieldName string, ro *RequestOptions) (*http.Response, error) {
return c.RequestFormFile("PUT", urlPath, filePath, fieldName, ro)
return c.RequestFormFile(http.MethodPut, urlPath, filePath, fieldName, ro)
}

// PutFormFileFromReader issues an HTTP PUT request (multipart/form-encoded) to put a file to an endpoint.
func (c *Client) PutFormFileFromReader(urlPath, fileName string, fileBytes io.Reader, fieldName string, ro *RequestOptions) (*http.Response, error) {
return c.RequestFormFileFromReader("PUT", urlPath, fileName, fileBytes, fieldName, ro)
return c.RequestFormFileFromReader(http.MethodPut, urlPath, fileName, fileBytes, fieldName, ro)
}

// PutJSON issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PutJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("PUT", p, i, ro)
return c.RequestJSON(http.MethodPut, p, i, ro)
}

// PutJSONAPI issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PutJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("PUT", p, i, ro)
return c.RequestJSONAPI(http.MethodPut, p, i, ro)
}

// Delete issues an HTTP DELETE request.
func (c *Client) Delete(p string, ro *RequestOptions) (*http.Response, error) {
return c.Request("DELETE", p, ro)
return c.Request(http.MethodDelete, p, ro)
}

// DeleteJSONAPI issues an HTTP DELETE request with the given interface json-encoded.
func (c *Client) DeleteJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("DELETE", p, i, ro)
return c.RequestJSONAPI(http.MethodDelete, p, i, ro)
}

// DeleteJSONAPIBulk issues an HTTP DELETE request with the given interface json-encoded and bulk requests.
func (c *Client) DeleteJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPIBulk("DELETE", p, i, ro)
return c.RequestJSONAPIBulk(http.MethodDelete, p, i, ro)
}

// Request makes an HTTP request against the HTTPClient using the given verb,
Expand All @@ -343,7 +343,7 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er
// contents since io.Reader is not seekable and cannot
// be rewound

r.Header.Del("Fastly-Key")
r.Header.Del(APIKeyHeader)
dump, _ := httputil.DumpRequest(r, true)

// httputil.DumpRequest has read the Body from 'r',
Expand All @@ -365,11 +365,11 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er
fmt.Printf("http.Response (dump): %q\n", dump)
}

if err != nil {
if err != nil || resp == nil {
return resp, err
}

if verb != "GET" && verb != "HEAD" {
if verb != http.MethodGet && verb != http.MethodHead {
remaining := resp.Header.Get("Fastly-RateLimit-Remaining")
if remaining != "" {
if val, err := strconv.Atoi(remaining); err == nil {
Expand Down Expand Up @@ -402,7 +402,7 @@ type RequestOptions struct {
// TODO: Lookout for this when it comes to the future code-generated API
// client world, as this special case might get omitted accidentally.
HealthCheckHeaders bool
// Can this request run in parallel
// Can this request run in parallel?
Parallel bool
// Params is a map of key-value pairs that will be added to the Request.
Params map[string]string
Expand Down
5 changes: 3 additions & 2 deletions fastly/client_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package fastly

import (
"net/http"
"net/url"
"strings"
"testing"
)

func TestClient_RawRequest(t *testing.T) {
validAPIHosts := []string{
"https://api.fastly.com",
DefaultEndpoint,
"https://api.fastly.com/",
}
purgeAPIPaths := []string{
Expand All @@ -29,7 +30,7 @@ func TestClient_RawRequest(t *testing.T) {
}
for _, p := range purgeAPIPaths {
for _, k := range cacheKeys {
r, err := c.RawRequest("GET", p+url.PathEscape(k), nil)
r, err := c.RawRequest(http.MethodGet, p+url.PathEscape(k), nil)
// Cannot test results for success if we get an error
if err != nil {
t.Fatal("Could not make RawRequest for ", h, p, k)
Expand Down
14 changes: 7 additions & 7 deletions fastly/config_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Client) CreateConfigStore(i *CreateConfigStoreInput) (*ConfigStore, err
resp, err := c.PostForm(path, i, &RequestOptions{
Headers: map[string]string{
// PostForm adds the appropriate Content-Type header.
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *Client) DeleteConfigStore(i *DeleteConfigStoreInput) error {

resp, err := c.Delete(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -101,7 +101,7 @@ func (c *Client) GetConfigStore(i *GetConfigStoreInput) (*ConfigStore, error) {

resp, err := c.Get(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -134,7 +134,7 @@ func (c *Client) GetConfigStoreMetadata(i *GetConfigStoreMetadataInput) (*Config

resp, err := c.Get(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -163,7 +163,7 @@ func (c *Client) ListConfigStores(i *ListConfigStoresInput) ([]*ConfigStore, err

requestOptions := &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (c *Client) ListConfigStoreServices(i *ListConfigStoreServicesInput) ([]*Se

resp, err := c.Get(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -243,7 +243,7 @@ func (c *Client) UpdateConfigStore(i *UpdateConfigStoreInput) (*ConfigStore, err
resp, err := c.PutForm(path, i, &RequestOptions{
Headers: map[string]string{
// PutForm adds the appropriate Content-Type header.
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down
8 changes: 4 additions & 4 deletions fastly/config_store_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Client) CreateConfigStoreItem(i *CreateConfigStoreItemInput) (*ConfigSt
resp, err := c.PostForm(path, i, &RequestOptions{
Headers: map[string]string{
// PostForm adds the appropriate Content-Type header.
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c *Client) DeleteConfigStoreItem(i *DeleteConfigStoreItemInput) error {

resp, err := c.Delete(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *Client) GetConfigStoreItem(i *GetConfigStoreItemInput) (*ConfigStoreIte

resp, err := c.Get(path, &RequestOptions{
Headers: map[string]string{
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *Client) UpdateConfigStoreItem(i *UpdateConfigStoreItemInput) (*ConfigSt
resp, err := c.RequestForm(httpMethod, path, i, &RequestOptions{
Headers: map[string]string{
// RequestForm adds the appropriate Content-Type header.
"Accept": "application/json",
"Accept": JSONMimeType,
},
Parallel: true,
})
Expand Down
Loading