forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[breaking] New() returns error; supports Options.
- New now has a variadic opts argument that accepts a slice of Option. - Options for changing the underlying http.Client and http.Header map added. - Updated flarectl to handle the updated New() signature. - A new Error() interface for returning different errors, and wrapping the cause, to provide easier inspection of errors without having to type-assert for all possible causes.
- Loading branch information
Showing
5 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ func setup() { | |
server = httptest.NewServer(mux) | ||
|
||
// CloudFlare client configured to use test server | ||
client = New("[email protected]", "deadbeef") | ||
client, _ = New("[email protected]", "deadbeef") | ||
client.BaseURL = server.URL | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cloudflare | ||
|
||
import "errors" | ||
|
||
var errEmptyCredentials = errors.New("invalid credentials: key & email must not be empty") | ||
|
||
// Error represents an error returned from this library. | ||
type Error interface { | ||
// Raised when user credentials or configuration is invalid. | ||
User() bool | ||
// Raised when a network error occurs. | ||
Network() bool | ||
// Contains the original (wrapped) error. | ||
Cause() error | ||
} | ||
|
||
// UserError represents a user-generated error. | ||
type UserError struct { | ||
error | ||
} | ||
|
||
// User is a user-caused error. | ||
func (e UserError) User() bool { | ||
return true | ||
} | ||
|
||
// Network error. | ||
func (e UserError) Network() bool { | ||
return false | ||
} | ||
|
||
// Cause wraps the underlying error. | ||
func (e UserError) Cause() error { | ||
return e.error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cloudflare | ||
|
||
import "net/http" | ||
|
||
// Option is a functional option for configuring the API client. | ||
type Option func(*API) error | ||
|
||
// HTTPClient accepts a custom *http.Client for making API calls. | ||
func HTTPClient(client *http.Client) Option { | ||
return func(api *API) error { | ||
api.httpClient = client | ||
return nil | ||
} | ||
} | ||
|
||
// Headers allows you to set custom HTTP headers when making API calls (e.g. for | ||
// satisfying HTTP proxies, or for debugging). | ||
func Headers(headers http.Header) Option { | ||
return func(api *API) error { | ||
api.headers = headers | ||
return nil | ||
} | ||
} | ||
|
||
// parseOptions parses the supplied options functions and returns a configured | ||
// *API instance. | ||
func (api *API) parseOptions(opts ...Option) error { | ||
// Range over each options function and apply it to our API type to | ||
// configure it. Options functions are applied in order, with any | ||
// conflicting options overriding earlier calls. | ||
for _, option := range opts { | ||
err := option(api) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |