Skip to content

Commit

Permalink
Merge branch 'master' into charliecruzan-listen-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecruzan-stripe authored Sep 11, 2024
2 parents 33ac89f + 5be357c commit ee0f50b
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 43 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/resource/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ func (oc *OperationCmd) runOperationCmd(cmd *cobra.Command, args []string) error
}

// if confirmation is provided, make the request
_, err = oc.MakeRequest(cmd.Context(), apiKey, path, &oc.Parameters, false)
_, err = oc.MakeRequest(cmd.Context(), apiKey, path, &oc.Parameters, false, nil)

return err
}
// else
_, err = oc.MakeRequest(cmd.Context(), apiKey, path, &oc.Parameters, false)
_, err = oc.MakeRequest(cmd.Context(), apiKey, path, &oc.Parameters, false, nil)
return err
}

Expand Down
74 changes: 71 additions & 3 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -45,6 +46,8 @@ type FixtureRequest struct {
Params map[string]interface{} `json:"params"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
Context string `json:"context,omitempty"`
APIBase string `json:"api_base,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}

// Fixture contains a mapping of an individual fixtures responses for querying
Expand Down Expand Up @@ -310,6 +313,9 @@ func (fxt *Fixture) Execute(ctx context.Context, apiVersion string) ([]string, e
}

func errWasExpected(err error, expectedErrorType string) bool {
if expectedErrorType == "" {
return false
}
if rerr, ok := err.(requests.RequestError); ok {
return rerr.ErrorType == expectedErrorType
}
Expand All @@ -326,15 +332,43 @@ func (fxt *Fixture) UpdateEnv() error {
return nil
}

func (fxt *Fixture) getAPIBase(request FixtureRequest) string {
if request.APIBase != "" {
return request.APIBase
}
return fxt.BaseURL
}

func (fxt *Fixture) unsupportedAPIKey(path string) bool {
return strings.HasPrefix(path, "/v2/") && !strings.HasPrefix(fxt.APIKey, "sk_")
}

func (fxt *Fixture) addCustomHeaders(headers map[string]string) func(req *http.Request) error {
return func(req *http.Request) error {
for k, v := range headers {
value, err := parsers.ParseQuery(v, fxt.Responses)
if err != nil {
return fmt.Errorf("error parsing %s field: %w", k, err)
}
req.Header.Set(k, value)
}
return nil
}
}

func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVersion string) ([]byte, error) {
req := requests.Base{
Method: strings.ToUpper(data.Method),
SuppressOutput: true,
APIBaseURL: fxt.BaseURL,
APIBaseURL: fxt.getAPIBase(data),
}

path, err := parsers.ParsePath(data.Path, fxt.Responses)

if fxt.unsupportedAPIKey(path) {
return make([]byte, 0), fmt.Errorf("this trigger must be run with a secret API key (starts with 'sk_')")
}

if err != nil {
return make([]byte, 0), err
}
Expand All @@ -352,12 +386,29 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data FixtureRequest, apiVer
params.SetIdempotency(idempotencyKey)
}

return req.MakeRequest(ctx, fxt.APIKey, path, params, true)
var additionalConfigure func(req *http.Request) error
if data.Headers != nil {
additionalConfigure = fxt.addCustomHeaders(data.Headers)
}

if strings.HasPrefix(path, "/v2/") {
jsonPayload := ""
if strings.ToLower(data.Method) == "post" {
jsonPayload, err = fxt.createJSONPayload(data.Params)
if err != nil {
return make([]byte, 0), err
}
}

return req.MakeV2Request(ctx, fxt.APIKey, path, params, true, additionalConfigure, jsonPayload)
}

return req.MakeRequest(ctx, fxt.APIKey, path, params, true, additionalConfigure)
}

func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*requests.RequestParameters, error) {
requestParams := requests.RequestParameters{}
parsed, err := parsers.ParseInterface(params, fxt.Responses)
parsed, err := parsers.ParseToFormData(params, fxt.Responses)
if err != nil {
return &requestParams, err
}
Expand All @@ -372,6 +423,23 @@ func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*reques
return &requestParams, nil
}

func (fxt *Fixture) createJSONPayload(params interface{}) (string, error) {
if params == nil {
return "{}", nil
}
parsedJSON, err := parsers.ParseToApplicationJSON(params, fxt.Responses)
if err != nil {
return "", err
}

jsonParams, err := json.Marshal(parsedJSON)
if err != nil {
return "", err
}

return string(jsonParams), nil
}

func (fxt *Fixture) updateEnv(env map[string]string) error {
dir, err := os.Getwd()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/fixtures/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ var Events = map[string]string{
"transfer.created": "triggers/transfer.created.json",
"transfer.reversed": "triggers/transfer.reversed.json",
"transfer.updated": "triggers/transfer.updated.json",
"v1.billing.meter.error_report_triggered": "triggers/v1.billing.meter.error_report_triggered.json",
"v1.billing.meter.no_meter_found": "triggers/v1.billing.meter.no_meter_found.json",
}

// BuildFromFixtureFile creates a new fixture struct for a file
Expand Down
67 changes: 67 additions & 0 deletions pkg/fixtures/triggers/v1.billing.meter.error_report_triggered.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@


{
"_meta": {
"template_version": 0
},
"fixtures": [
{
"name": "list_billing_meters",
"path": "/v1/billing/meters",
"method": "get",
"params": {
"status": "active"
}
},
{
"name": "billing_meter",
"path": "/v1/billing/meters",
"method": "post",
"params": {
"display_name": "Stripe CLI Billing Meter",
"event_name": "stripe_cli_billing_meter_for_fixture",
"default_aggregation": {
"formula": "sum"
}
},
"expected_error_type": "invalid_request_error"
},
{
"name": "list_billing_meters_after_creation",
"path": "/v1/billing/meters",
"method": "get",
"params": {
"status": "active"
}
},
{
"name": "billing_meter_event_session",
"path": "/v2/billing/meter_event_session",
"method": "post",
"headers": {
"Content-Type": "application/json",
"Stripe-Version": "unsafe-development"
},
"params": {}
},
{
"name": "create_billing_meter_event_stream",
"path": "/v2/billing/meter_event_stream",
"method": "post",
"api_base": "https://events.stripe.com",
"headers": {
"Content-Type": "application/json",
"Stripe-Version": "unsafe-development",
"Authorization": "Bearer ${billing_meter_event_session:authentication_token}"
},
"params": {
"event_name": "${list_billing_meters_after_creation:data.0.event_name}",
"timestamp":"${time-now-RFC3339}",
"identifier": "${generate-uuid}",
"payload": {
"value":"1"
}
}
}
]
}
35 changes: 35 additions & 0 deletions pkg/fixtures/triggers/v1.billing.meter.no_meter_found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"_meta": {
"template_version": 0
},
"fixtures": [
{
"name": "billing_meter_event_session",
"path": "/v2/billing/meter_event_session",
"method": "post",
"headers": {
"Content-Type": "application/json",
"Stripe-Version": "unsafe-development"
},
"params": {}
},
{
"name": "create_billing_meter_event_stream",
"path": "/v2/billing/meter_event_stream",
"method": "post",
"api_base": "https://events.stripe.com",
"headers": {
"Content-Type": "application/json",
"Stripe-Version": "unsafe-development",
"Authorization": "Bearer ${billing_meter_event_session:authentication_token}"
},
"params": {
"event_name": "${generate-uuid}",
"timestamp":"${time-now-RFC3339}",
"payload": {
"value":"10"
}
}
}
]
}
Loading

0 comments on commit ee0f50b

Please sign in to comment.