Skip to content

Commit

Permalink
Merge pull request #644 from stripe/remi-add-file-link
Browse files Browse the repository at this point in the history
Add the File Link resource
  • Loading branch information
brandur-stripe authored Aug 3, 2018
2 parents 2e017fe + fd23dd4 commit df8e0fd
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 6 deletions.
4 changes: 4 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stripe/stripe-go/exchangerate"
"github.com/stripe/stripe-go/fee"
"github.com/stripe/stripe-go/feerefund"
"github.com/stripe/stripe-go/filelink"
"github.com/stripe/stripe-go/fileupload"
"github.com/stripe/stripe-go/invoice"
"github.com/stripe/stripe-go/invoiceitem"
Expand Down Expand Up @@ -91,6 +92,8 @@ type API struct {
Fees *fee.Client
// FeeRefunds is the client used to invoke /application_fees/refunds APIs.
FeeRefunds *feerefund.Client
// FileLinks is the client used to invoke the /file_links APIs.
FileLinks *filelink.Client
// FileUploads is the client used to invoke the /files APIs.
FileUploads *fileupload.Client
// Invoices is the client used to invoke /invoices APIs.
Expand Down Expand Up @@ -181,6 +184,7 @@ func (a *API) Init(key string, backends *stripe.Backends) {
a.Events = &event.Client{B: backends.API, Key: key}
a.Fees = &fee.Client{B: backends.API, Key: key}
a.FeeRefunds = &feerefund.Client{B: backends.API, Key: key}
a.FileLinks = &filelink.Client{B: backends.Uploads, Key: key}
a.FileUploads = &fileupload.Client{B: backends.Uploads, Key: key}
a.Invoices = &invoice.Client{B: backends.API, Key: key}
a.InvoiceItems = &invoiceitem.Client{B: backends.API, Key: key}
Expand Down
60 changes: 60 additions & 0 deletions filelink.go
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"`
}
89 changes: 89 additions & 0 deletions filelink/client.go
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}
}
40 changes: 40 additions & 0 deletions filelink/client_test.go
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)
}
29 changes: 29 additions & 0 deletions filelink_test.go
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)
}
}
14 changes: 8 additions & 6 deletions fileupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ type FileUploadListParams struct {
// FileUpload is the resource representing a Stripe file upload.
// For more details see https://stripe.com/docs/api#file_uploads.
type FileUpload struct {
Created int64 `json:"created"`
ID string `json:"id"`
Purpose FileUploadPurpose `json:"purpose"`
Size int64 `json:"size"`
Type string `json:"type"`
URL string `json:"url"`
Created int64 `json:"created"`
ID string `json:"id"`
Filename string `json:"filename"`
Links []*FileLink `json:"links"`
Purpose FileUploadPurpose `json:"purpose"`
Size int64 `json:"size"`
Type string `json:"type"`
URL string `json:"url"`
}

// FileUploadList is a list of file uploads as retrieved from a list endpoint.
Expand Down

0 comments on commit df8e0fd

Please sign in to comment.