Skip to content
Merged
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
13 changes: 10 additions & 3 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var method string
var data string
var format string
var portal bool
var debug bool

// apiCmd represents the api command
var apiCmd = &cobra.Command{
Expand All @@ -27,13 +28,18 @@ var apiCmd = &cobra.Command{

var client *api.Client
var err error
var opts []api.ClientOption

if debug {
opts = append(opts, api.WithDebug())
}

if portal {
client, err = api.NewPortalClient()
client, err = api.NewPortalClient(opts...)
} else if isMetalPath(path) {
client, err = api.NewMetalClient()
client, err = api.NewMetalClient(opts...)
} else {
client, err = api.NewStandardClient()
client, err = api.NewStandardClient(opts...)
}

if err != nil {
Expand Down Expand Up @@ -75,6 +81,7 @@ func init() {
apiCmd.Flags().StringVarP(&data, "data", "d", "", "Data to send with POST/PUT requests")
apiCmd.Flags().StringVarP(&format, "format", "f", "json", "Format to use for output (json or yaml)")
apiCmd.Flags().BoolVar(&portal, "portal", false, "Use Equinix Portal API (cookie auth)")
apiCmd.Flags().BoolVar(&debug, "debug", false, "Enable debug logging for HTTP requests and responses")
}

func isMetalPath(path string) bool {
Expand Down
1 change: 1 addition & 0 deletions docs/equinix_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ equinix api [url-path] [flags]

```
-d, --data string Data to send with POST/PUT requests
--debug Enable debug logging for HTTP requests and responses
-f, --format string Format to use for output (json or yaml) (default "json")
-h, --help help for api
-X, --method string HTTP method to use (default "GET")
Expand Down
25 changes: 19 additions & 6 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func WithDebug() ClientOption {

// NewPortalClient creates a new Client for Equinix APIs that exist under
// portal.equinix.com and rely on Cookies to transmit OAuth2 tokens
func NewPortalClient() (*Client, error) {
func NewPortalClient(options ...ClientOption) (*Client, error) {
client := &Client{
BaseURL: "https://portal.equinix.com/api",
DefaultHeaders: standardHeaders,
Expand All @@ -127,11 +127,19 @@ func NewPortalClient() (*Client, error) {
client.DefaultHeaders["Accept"] = "*/*"
client.DefaultHeaders["Accept-Encoding"] = "*/*"

// Apply options to potentially wrap the transport
transport := http.RoundTripper(http.DefaultTransport)
for _, opt := range options {
transport = opt(transport)
}

client.HTTPClient.Transport = transport

return client, nil
}

// NewMetalClient creates a new Client for the Equinix Metal API
func NewMetalClient() (*Client, error) {
func NewMetalClient(options ...ClientOption) (*Client, error) {
client := &Client{
BaseURL: "https://api.equinix.com",
DefaultHeaders: standardHeaders,
Expand All @@ -143,14 +151,19 @@ func NewMetalClient() (*Client, error) {
return nil, errors.New("metal_auth_token not found in env or config")
}
client.DefaultHeaders["X-Auth-Token"] = token

// Apply options to potentially wrap the transport
transport := http.RoundTripper(http.DefaultTransport)
for _, opt := range options {
transport = opt(transport)
}

client.HTTPClient.Transport = transport

return client, nil
}

// Request makes an HTTP request to the specified API path with the given method and data.
//
// TODO: May be useful to support debug logging of requests/responses. That could
// be done here but probably better to do it with a custom Transport that is shared
// across generic and generated clients for a consistent debug experience.
func (c *Client) Request(apiPath, method string, data string) ([]byte, error) {
url := c.BaseURL + "/" + apiPath
var body io.Reader
Expand Down