-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #644 from stripe/remi-add-file-link
Add the File Link resource
- Loading branch information
Showing
6 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
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
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,60 @@ | ||
package stripe | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// FileLinkParams is the set of parameters that can be used when creating or updating a file link. | ||
type FileLinkParams struct { | ||
Params `form:"*"` | ||
ExpiresAt *int64 `form:"expires_at"` | ||
File *string `form:"file"` | ||
} | ||
|
||
// FileLinkListParams is the set of parameters that can be used when listing file links. | ||
type FileLinkListParams struct { | ||
ListParams `form:"*"` | ||
Created *int64 `form:"created"` | ||
CreatedRange *RangeQueryParams `form:"created"` | ||
Expired *bool `form:"expired"` | ||
File *string `form:"file"` | ||
} | ||
|
||
// FileLink is the resource representing a Stripe file link. | ||
// For more details see https://stripe.com/docs/api#file_links. | ||
type FileLink struct { | ||
Created int64 `json:"created"` | ||
Expired bool `json:"expired"` | ||
ExpiresAt int64 `json:"expires_at"` | ||
File *FileUpload `json:"file"` | ||
ID string `json:"id"` | ||
Livemode bool `json:"livemode"` | ||
Metadata map[string]string `json:"metadata"` | ||
Object string `json:"object"` | ||
URL string `json:"url"` | ||
} | ||
|
||
// UnmarshalJSON handles deserialization of a file link. | ||
// This custom unmarshaling is needed because the resulting | ||
// property may be an ID or the full struct if it was expanded. | ||
func (c *FileLink) UnmarshalJSON(data []byte) error { | ||
if id, ok := ParseID(data); ok { | ||
c.ID = id | ||
return nil | ||
} | ||
|
||
type fileLink FileLink | ||
var v fileLink | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
|
||
*c = FileLink(v) | ||
return nil | ||
} | ||
|
||
// FileLinkList is a list of file links as retrieved from a list endpoint. | ||
type FileLinkList struct { | ||
ListMeta | ||
Data []*FileLink `json:"data"` | ||
} |
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,89 @@ | ||
// Package filelink provides API functions related to file links. | ||
// | ||
// For more details, see: https://stripe.com/docs/api/go#file_links. | ||
package filelink | ||
|
||
import ( | ||
"net/http" | ||
|
||
stripe "github.com/stripe/stripe-go" | ||
"github.com/stripe/stripe-go/form" | ||
) | ||
|
||
// Client is used to invoke APIs related to file links. | ||
type Client struct { | ||
B stripe.Backend | ||
Key string | ||
} | ||
|
||
// New creates a new file link. | ||
func New(params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
return getC().New(params) | ||
} | ||
|
||
// New creates a new file link. | ||
func (c Client) New(params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
fileLink := &stripe.FileLink{} | ||
err := c.B.Call(http.MethodPost, "/file_links", c.Key, params, fileLink) | ||
return fileLink, err | ||
} | ||
|
||
// Get retrieves a file link. | ||
func Get(id string, params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
return getC().Get(id, params) | ||
} | ||
|
||
// Get retrieves a file link. | ||
func (c Client) Get(id string, params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
path := stripe.FormatURLPath("/file_links/%s", id) | ||
fileLink := &stripe.FileLink{} | ||
err := c.B.Call(http.MethodGet, path, c.Key, params, fileLink) | ||
return fileLink, err | ||
} | ||
|
||
// Update updates a file link. | ||
func Update(id string, params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
return getC().Update(id, params) | ||
} | ||
|
||
// Update updates a file link. | ||
func (c Client) Update(id string, params *stripe.FileLinkParams) (*stripe.FileLink, error) { | ||
path := stripe.FormatURLPath("/file_links/%s", id) | ||
fileLink := &stripe.FileLink{} | ||
err := c.B.Call(http.MethodPost, path, c.Key, params, fileLink) | ||
return fileLink, err | ||
} | ||
|
||
// List returns an iterator that iterates all file links. | ||
func List(params *stripe.FileLinkListParams) *Iter { | ||
return getC().List(params) | ||
} | ||
|
||
// List returns an iterator that iterates all file links. | ||
func (c Client) List(listParams *stripe.FileLinkListParams) *Iter { | ||
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) { | ||
list := &stripe.FileLinkList{} | ||
err := c.B.CallRaw(http.MethodGet, "/file_links", c.Key, b, p, list) | ||
|
||
ret := make([]interface{}, len(list.Data)) | ||
for i, v := range list.Data { | ||
ret[i] = v | ||
} | ||
|
||
return ret, list.ListMeta, err | ||
})} | ||
} | ||
|
||
// Iter is an iterator for file links. | ||
type Iter struct { | ||
*stripe.Iter | ||
} | ||
|
||
// FileLink returns the file link which the iterator is currently pointing to. | ||
func (i *Iter) FileLink() *stripe.FileLink { | ||
return i.Current().(*stripe.FileLink) | ||
} | ||
|
||
func getC() Client { | ||
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} | ||
} |
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,40 @@ | ||
package filelink | ||
|
||
import ( | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
stripe "github.com/stripe/stripe-go" | ||
_ "github.com/stripe/stripe-go/testing" | ||
) | ||
|
||
func TestFileLinkGet(t *testing.T) { | ||
fileLink, err := Get("link_123", nil) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, fileLink) | ||
} | ||
|
||
func TestFileLinkList(t *testing.T) { | ||
i := List(&stripe.FileLinkListParams{}) | ||
|
||
// Verify that we can get at least one fileLink | ||
assert.True(t, i.Next()) | ||
assert.Nil(t, i.Err()) | ||
assert.NotNil(t, i.FileLink()) | ||
} | ||
|
||
func TestFileLinkNew(t *testing.T) { | ||
fileLink, err := New(&stripe.FileLinkParams{ | ||
File: stripe.String("file_123"), | ||
}) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, fileLink) | ||
} | ||
|
||
func TestFileLinkUpdate(t *testing.T) { | ||
params := &stripe.FileLinkParams{} | ||
params.AddMetadata("key", "value") | ||
fileLink, err := Update("link_123", params) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, fileLink) | ||
} |
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,29 @@ | ||
package stripe | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileLink_UnmarshalJSON(t *testing.T) { | ||
// Unmarshals from a JSON string | ||
{ | ||
var v FileLink | ||
err := json.Unmarshal([]byte(`"link_123"`), &v) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "link_123", v.ID) | ||
} | ||
|
||
// Unmarshals from a JSON object | ||
{ | ||
v := FileLink{ID: "link_123"} | ||
data, err := json.Marshal(&v) | ||
assert.NoError(t, err) | ||
|
||
err = json.Unmarshal(data, &v) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "link_123", v.ID) | ||
} | ||
} |
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