Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce stripe.RawRequest as a canonical way to request APIs without definitions #1648

Merged
merged 24 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
503ae9e
Factor out newRequestHeader
richardm-stripe Apr 20, 2023
0a15226
Move calculateHeader up
richardm-stripe Apr 20, 2023
ba1b2ca
Implementation
richardm-stripe Apr 22, 2023
be7fe3a
Hoist
richardm-stripe Apr 22, 2023
56da8fd
Improvements
richardm-stripe Apr 24, 2023
6692992
Fix linter issues
richardm-stripe Apr 25, 2023
d1ee60b
Improvement
richardm-stripe May 3, 2023
cc95270
Encoding -> ApiMode
anniel-stripe May 18, 2023
bdf2e75
generate preview version
anniel-stripe May 19, 2023
14a53e5
ApiMode -> APIMode
anniel-stripe May 19, 2023
add7f3d
define preview package, add more tests, represent body as content str…
anniel-stripe May 19, 2023
4a5678c
lint
anniel-stripe May 19, 2023
04fea98
default stripe version if apiMode is preview
anniel-stripe May 19, 2023
d7dbdc1
Merge branch 'beta' into richardm-raw-request
anniel-stripe May 19, 2023
adf598b
don't modify original params
anniel-stripe May 19, 2023
c60fc65
Merge branch 'richardm-raw-request' of github.com:stripe/stripe-go in…
anniel-stripe May 19, 2023
9ad9036
Embed Params in RawParams, add StandardRawRequest tests, allow nil Ra…
anniel-stripe May 22, 2023
b09d7de
README, send Content-Type header for all requests
anniel-stripe May 22, 2023
0851c97
update preview API version
anniel-stripe May 23, 2023
b5d8e1f
feedback
anniel-stripe May 24, 2023
09d2bd8
bring back NewRequest
anniel-stripe May 24, 2023
7488d1f
better README example
anniel-stripe May 24, 2023
26a17f7
remove commented code
anniel-stripe May 24, 2023
f3e7c42
Allow nil params in preview requests
anniel-stripe May 24, 2023
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,22 @@ To install a beta version of stripe-go use the commit notation of the `go get` c
go get -u github.com/stripe/stripe-go/[email protected]
```

### Custom SDKs
If you would like to send a request to an undocumented API (for example you are in a private beta), or if you prefer to bypass the method definitions in the library and specify your request details directly, you can use the `RawRequest` method on the `stripe` backend.

```go
key := "sk_test_xyz" // set to "" to use stripe.Key
content := `foo=myFoo&bar[]=true` // form-encoded POST body
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved

resp, err := stripe.RawRequest("POST", "/v1/beta_endpoint", content, nil)

response, err := stripe.RawRequest(http.MethodPost, "/v1/beta_endpoint", key, content, params)

// Optionally use json.Unmarshal to convert the response to a strongly-typed object.
deserializedResponse := &MyResource{}
err = json.Unmarshal(response.RawJSON, deserializedResponse)
```

> **Note**
> There can be breaking changes between beta versions.

Expand Down
3 changes: 2 additions & 1 deletion api_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
package stripe

const (
apiVersion string = "2022-11-15"
apiVersion string = "2022-11-15"
previewVersion string = "2023-05-16.preview-v2"
)
28 changes: 28 additions & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ type ListParamsContainer interface {
GetListParams() *ListParams
}

type APIMode string

var PreviewAPIMode APIMode = "preview"
var StandardAPIMode APIMode = "standard"

// Params is the structure that contains the common properties
// of any *Params structure.
type Params struct {
Expand Down Expand Up @@ -246,6 +251,29 @@ type ParamsContainer interface {
GetParams() *Params
}

type RawParams struct {
Params `form:"*"`
APIMode APIMode `form:"-"`
StripeContext string `form:"-"`
}

func (p *RawParams) GetAPIMode() APIMode {
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
if p.APIMode == "" {
return StandardAPIMode
}
return p.APIMode
}

func (p *RawParams) GetStripeContext() string {
return p.StripeContext
}

type RawParamsContainer interface {
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
ParamsContainer
GetAPIMode() APIMode
GetStripeContext() string
}

// RangeQueryParams are a set of generic request parameters that are used on
// list endpoints to filter their results by some timestamp.
type RangeQueryParams struct {
Expand Down
27 changes: 27 additions & 0 deletions preview/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Package preview provides preview / private beta APIs
package preview

import (
"net/http"

stripe "github.com/stripe/stripe-go/v74"
)

func GetDefaultRequestOptions(params stripe.RawParamsContainer) stripe.RawParamsContainer {
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
rawParams := stripe.RawParams{
Params: *params.GetParams(),
APIMode: stripe.PreviewAPIMode,
StripeContext: params.GetStripeContext(),
}
return &rawParams
}

func Get(path string, params stripe.RawParamsContainer) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodGet, path, "", GetDefaultRequestOptions(params))
}
func Post(path, content string, params stripe.RawParamsContainer) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodPost, path, content, params)
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
}
func Delete(path string, params stripe.RawParamsContainer) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodDelete, path, "", params)
}
73 changes: 73 additions & 0 deletions preview/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package preview

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v74"
_ "github.com/stripe/stripe-go/v74/testing"
)

func stubAPIBackend(testServer *httptest.Server) {
backend := stripe.GetBackendWithConfig(
stripe.APIBackend,
&stripe.BackendConfig{
LeveledLogger: &stripe.LeveledLogger{
Level: stripe.LevelDebug,
},
MaxNetworkRetries: stripe.Int64(0),
URL: stripe.String(testServer.URL),
},
).(*stripe.BackendImplementation)

stripe.SetBackend(stripe.APIBackend, backend)
}

func TestPreviewRequestWithAdditionalHeaders(t *testing.T) {
var body string
var path string
var method string
var contentType string
var stripeVersion string
var fooHeader string
var stripeContext string
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
body = string(req)
path = r.URL.RequestURI()
method = r.Method
contentType = r.Header.Get("Content-Type")
stripeVersion = r.Header.Get("Stripe-Version")
fooHeader = r.Header.Get("foo")
stripeContext = r.Header.Get("Stripe-Context")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"object": "abc", "xyz": {"def": "jih"}}`))
}))

stubAPIBackend(testServer)

type myParams struct {
stripe.Params `form:"-"`
stripe.RawParams `form:"-"`
}

params := myParams{stripe.Params{Headers: http.Header{"foo": []string{"bar"}}}, stripe.RawParams{StripeContext: "acct_123"}}

_, err := Get("/v1/abc", &params)
assert.NoError(t, err)

assert.Equal(t, stripe.APIMode(""), params.RawParams.APIMode)
assert.Equal(t, ``, body)
assert.Equal(t, `/v1/abc`, path)
assert.Equal(t, `GET`, method)
assert.Equal(t, `application/json`, contentType)
assert.NotEqual(t, stripe.APIVersion, stripeVersion)
assert.Equal(t, `bar`, fooHeader)
assert.Equal(t, `acct_123`, stripeContext)
assert.NoError(t, err)
defer testServer.Close()
}
Loading