Skip to content

Commit

Permalink
Remove rawrequest Post, Get, and Delete (#1933)
Browse files Browse the repository at this point in the history
* removed rawrequest Post, Get, and Delete

* fixed example link and test in README
  • Loading branch information
jar-stripe authored Oct 8, 2024
1 parent c4e7524 commit 2e92acc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@ import (
func make_raw_request() error {
stripe.Key = "sk_test_123"

b, err := stripe.GetRawRequestBackend(stripe.APIBackend)
if err != nil {
return err
}

client := rawrequest.Client{B: b, Key: apiKey}

payload := map[string]interface{}{
"event_name": "hotdogs_eaten",
"payload": map[string]string{
Expand All @@ -588,7 +595,7 @@ func make_raw_request() error {
return err
}

v2_resp, err := rawrequest.Post("/v2/billing/meter_events", string(body), nil)
v2_resp, err := client.RawRequest(http.MethodPost, "/v2/billing/meter_events", string(body), nil)
if err != nil {
return err
}
Expand All @@ -605,7 +612,7 @@ func make_raw_request() error {
form.AppendTo(formValues, payload)
content := formValues.Encode()

v1_resp, err := rawrequest.Post("/v1/billing/meter_events", content, nil)
v1_resp, err := client.RawRequest(http.MethodPost, "/v1/billing/meter_events", content, nil)
if err != nil {
return err
}
Expand All @@ -619,7 +626,9 @@ func make_raw_request() error {

return nil
}

```
See more examples in the [/example/v2 folder](example/v2).

## Support

Expand Down
14 changes: 0 additions & 14 deletions rawrequest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package rawrequest

import (
"net/http"

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

Expand All @@ -16,15 +14,3 @@ type Client struct {
func (c Client) RawRequest(method string, path string, content string, params *stripe.RawParams) (*stripe.APIResponse, error) {
return c.B.RawRequest(method, path, c.Key, content, params)
}

func Get(path string, params *stripe.RawParams) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodGet, path, "", params)
}

func Post(path, content string, params *stripe.RawParams) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodPost, path, content, params)
}

func Delete(path string, params *stripe.RawParams) (*stripe.APIResponse, error) {
return stripe.RawRequest(http.MethodDelete, path, "", params)
}
17 changes: 9 additions & 8 deletions rawrequest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
_ "github.com/stripe/stripe-go/v80/testing"
)

func stubAPIBackend(testServer *httptest.Server) {
func createTestClient(testServer *httptest.Server) Client {
backend := stripe.GetBackendWithConfig(
stripe.APIBackend,
&stripe.BackendConfig{
Expand All @@ -23,7 +23,8 @@ func stubAPIBackend(testServer *httptest.Server) {
},
).(*stripe.BackendImplementation)

stripe.SetBackend(stripe.APIBackend, backend)
return Client{B: backend, Key: stripe.Key}
// stripe.SetBackend(stripe.APIBackend, backend)
}

func TestV2PostRequest(t *testing.T) {
Expand All @@ -42,11 +43,11 @@ func TestV2PostRequest(t *testing.T) {
w.Write([]byte(`{"object": "abc", "xyz": {"def": "jih"}}`))
}))

stubAPIBackend(testServer)
client := createTestClient(testServer)

var params *stripe.RawParams

_, err := Post("/v2/abc", `{"xyz": {"def": "jih"}}`, params)
_, err := client.RawRequest(http.MethodPost, "/v2/abc", `{"xyz": {"def": "jih"}}`, params)
assert.NoError(t, err)

assert.Nil(t, params) // original params should not be modified
Expand Down Expand Up @@ -74,11 +75,11 @@ func TestRawV1PostRequest(t *testing.T) {
w.Write([]byte(`{"object": "abc", "xyz": {"def": "jih"}}`))
}))

stubAPIBackend(testServer)
client := createTestClient(testServer)

var params *stripe.RawParams

_, err := Post("/v1/abc", `abc=123&a[name]=nested`, params)
_, err := client.RawRequest(http.MethodPost, "/v1/abc", `abc=123&a[name]=nested`, params)
assert.NoError(t, err)

assert.Nil(t, params) // original params should not be modified
Expand Down Expand Up @@ -110,13 +111,13 @@ func TestV2GetRequestWithAdditionalHeaders(t *testing.T) {
w.Write([]byte(`{"object": "abc", "xyz": {"def": "jih"}}`))
}))

stubAPIBackend(testServer)
client := createTestClient(testServer)

headers := http.Header{}
headers.Set("foo", "bar")
params := &stripe.RawParams{Params: stripe.Params{Headers: headers}, StripeContext: "acct_123"}

_, err := Get("/v2/abc", params)
_, err := client.RawRequest(http.MethodGet, "/v2/abc", "", params)
assert.NoError(t, err)

assert.Equal(t, ``, body)
Expand Down

0 comments on commit 2e92acc

Please sign in to comment.