Skip to content

Commit

Permalink
updates open-api spec with overrides for beta /v1/orders create/updat…
Browse files Browse the repository at this point in the history
…e behavior (#867)

* adds support for Beta /v1/orders idioms
  • Loading branch information
wmadison-stripe committed May 25, 2022
1 parent fc14f2b commit 927f5ba
Show file tree
Hide file tree
Showing 9 changed files with 26,360 additions and 16,452 deletions.
42,500 changes: 26,074 additions & 16,426 deletions api/openapi-spec/spec3.sdk.json

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions pkg/cmd/resource/orders.go
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
}
35 changes: 35 additions & 0 deletions pkg/cmd/resource/orders_create.go
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
}
51 changes: 51 additions & 0 deletions pkg/cmd/resource/orders_create_test.go
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)
}
34 changes: 34 additions & 0 deletions pkg/cmd/resource/orders_update.go
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
}
50 changes: 50 additions & 0 deletions pkg/cmd/resource/orders_update_test.go
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)
}
Loading

0 comments on commit 927f5ba

Please sign in to comment.