-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updates open-api spec with overrides for beta /v1/orders create/updat…
…e behavior (#867) * adds support for Beta /v1/orders idioms
- Loading branch information
1 parent
fc14f2b
commit 927f5ba
Showing
9 changed files
with
26,360 additions
and
16,452 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
package resource | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/config" | ||
) | ||
|
||
// AddOrdersSubCmds adds custom subcommands to the `orders` command created | ||
// automatically as a resource command. | ||
func AddOrdersSubCmds(rootCmd *cobra.Command, cfg *config.Config) error { | ||
found := false | ||
|
||
for _, cmd := range rootCmd.Commands() { | ||
if cmd.Use == "orders" { | ||
found = true | ||
|
||
// Remove the autogenerated `create` and `update` command(s). | ||
commands := cmd.Commands() | ||
for _, c := range commands { | ||
if c.Use == "create" { | ||
cmd.RemoveCommand(c) | ||
} else if c.Use == "update" { | ||
cmd.RemoveCommand(c) | ||
} | ||
} | ||
|
||
NewOrdersCreateCmd(cmd, cfg) | ||
NewOrdersUpdateCmd(cmd, cfg) | ||
|
||
break | ||
} | ||
} | ||
|
||
if !found { | ||
return errors.New("Could not find orders command") | ||
} | ||
|
||
return nil | ||
} |
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 resource | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/config" | ||
) | ||
|
||
// OrdersCreateCmd represents the order create API operation command. This | ||
// command is manually defined because of quirkiness with the OpenAPI spec autogenerated command and required parameters. | ||
type OrdersCreateCmd struct { | ||
opCmd *OperationCmd | ||
} | ||
|
||
func (occ *OrdersCreateCmd) runOrdersCreateCmd(cmd *cobra.Command, args []string) error { | ||
return occ.opCmd.runOperationCmd(cmd, args) | ||
} | ||
|
||
// NewOrdersCreateCmd creates a new orders creation sub command. | ||
func NewOrdersCreateCmd(parentCmd *cobra.Command, cfg *config.Config) *OrdersCreateCmd { | ||
ordersCreateCmd := &OrdersCreateCmd{ | ||
opCmd: NewOperationCmd(parentCmd, "create", "/v1/orders", http.MethodPost, map[string]string{ | ||
"currency": "string", | ||
"line_items[][product]": "string", | ||
"line_items[][quantity]": "integer", | ||
"automatic_tax[enabled]": "boolean", | ||
}, cfg), | ||
} | ||
|
||
ordersCreateCmd.opCmd.Cmd.RunE = ordersCreateCmd.runOrdersCreateCmd | ||
|
||
return ordersCreateCmd | ||
} |
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,51 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stripe/stripe-cli/pkg/config" | ||
) | ||
|
||
func TestRunOrdersCreateCmd(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
body, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, http.MethodPost, r.Method) | ||
require.Equal(t, "/v1/orders", r.URL.Path) | ||
require.Equal(t, "Bearer sk_test_1234", r.Header.Get("Authorization")) | ||
vals, err := url.ParseQuery(string(body)) | ||
require.NoError(t, err) | ||
require.Equal(t, 4, len(vals)) | ||
})) | ||
defer ts.Close() | ||
|
||
viper.Reset() | ||
|
||
parentCmd := &cobra.Command{Annotations: make(map[string]string)} | ||
profile := config.Profile{ | ||
APIKey: "sk_test_1234", | ||
} | ||
erc := NewOrdersCreateCmd(parentCmd, &config.Config{Profile: profile}) | ||
erc.opCmd.APIBaseURL = ts.URL | ||
|
||
parentCmd.SetArgs([]string{"create", | ||
"--currency", "usd", | ||
"--line-items[][product]", "dummyProduct", | ||
"--line-items[][quantity]", "1", | ||
"--automatic-tax[enabled]", "true", | ||
}) | ||
err := parentCmd.ExecuteContext(context.Background()) | ||
|
||
require.NoError(t, err) | ||
} |
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,34 @@ | ||
package resource | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/config" | ||
) | ||
|
||
// OrdersUpdateCmd represents the order update API operation command. This | ||
// command is manually defined because of quirkiness with the OpenAPI spec autogenerated command and required parameters. | ||
type OrdersUpdateCmd struct { | ||
opCmd *OperationCmd | ||
} | ||
|
||
func (ouc *OrdersUpdateCmd) runOrdersUpdateCmd(cmd *cobra.Command, args []string) error { | ||
return ouc.opCmd.runOperationCmd(cmd, args) | ||
} | ||
|
||
// NewOrdersUpdateCmd creates a new orders creation sub command. | ||
func NewOrdersUpdateCmd(parentCmd *cobra.Command, cfg *config.Config) *OrdersUpdateCmd { | ||
ordersUpdateCmd := &OrdersUpdateCmd{ | ||
opCmd: NewOperationCmd(parentCmd, "update", "/v1/orders/{id}", http.MethodPost, map[string]string{ | ||
"currency": "string", | ||
"line_items[][product]": "string", | ||
"line_items[][quantity]": "integer", | ||
}, cfg), | ||
} | ||
|
||
ordersUpdateCmd.opCmd.Cmd.RunE = ordersUpdateCmd.runOrdersUpdateCmd | ||
|
||
return ordersUpdateCmd | ||
} |
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,50 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stripe/stripe-cli/pkg/config" | ||
) | ||
|
||
func TestRunOrdersUpdateCmd(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
body, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, http.MethodPost, r.Method) | ||
require.Equal(t, "/v1/orders/order_1234", r.URL.Path) | ||
require.Equal(t, "Bearer sk_test_1234", r.Header.Get("Authorization")) | ||
vals, err := url.ParseQuery(string(body)) | ||
require.NoError(t, err) | ||
require.Equal(t, 3, len(vals)) | ||
})) | ||
defer ts.Close() | ||
|
||
viper.Reset() | ||
|
||
parentCmd := &cobra.Command{Annotations: make(map[string]string)} | ||
profile := config.Profile{ | ||
APIKey: "sk_test_1234", | ||
} | ||
erc := NewOrdersUpdateCmd(parentCmd, &config.Config{Profile: profile}) | ||
erc.opCmd.APIBaseURL = ts.URL | ||
|
||
parentCmd.SetArgs([]string{"update", "order_1234", | ||
"--currency", "usd", | ||
"--line-items[][product]", "dummyProduct", | ||
"--line-items[][quantity]", "2", | ||
}) | ||
err := parentCmd.ExecuteContext(context.Background()) | ||
|
||
require.NoError(t, err) | ||
} |
Oops, something went wrong.