github.com/stripe/stripe-go/v76@v76.25.0/file/client.go (about)

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  // Package file provides the /files APIs
     8  package file
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  
    14  	stripe "github.com/stripe/stripe-go/v76"
    15  	"github.com/stripe/stripe-go/v76/form"
    16  )
    17  
    18  // Client is used to invoke /files APIs.
    19  type Client struct {
    20  	B        stripe.Backend
    21  	BUploads stripe.Backend
    22  	Key      string
    23  }
    24  
    25  // New creates a new file.
    26  func New(params *stripe.FileParams) (*stripe.File, error) {
    27  	return getC().New(params)
    28  }
    29  
    30  // New creates a new file.
    31  func (c Client) New(params *stripe.FileParams) (*stripe.File, error) {
    32  	if params == nil {
    33  		return nil, fmt.Errorf(
    34  			"params cannot be nil, and params.Purpose and params.File must be set",
    35  		)
    36  	}
    37  
    38  	bodyBuffer, boundary, err := params.GetBody()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	file := &stripe.File{}
    44  	err = c.BUploads.CallMultipart(http.MethodPost, "/v1/files", c.Key, boundary, bodyBuffer, &params.Params, file)
    45  
    46  	return file, err
    47  }
    48  
    49  // Get returns the details of a file.
    50  func Get(id string, params *stripe.FileParams) (*stripe.File, error) {
    51  	return getC().Get(id, params)
    52  }
    53  
    54  // Get returns the details of a file.
    55  func (c Client) Get(id string, params *stripe.FileParams) (*stripe.File, error) {
    56  	path := stripe.FormatURLPath("/v1/files/%s", id)
    57  	file := &stripe.File{}
    58  	err := c.B.Call(http.MethodGet, path, c.Key, params, file)
    59  	return file, err
    60  }
    61  
    62  // List returns a list of files.
    63  func List(params *stripe.FileListParams) *Iter {
    64  	return getC().List(params)
    65  }
    66  
    67  // List returns a list of files.
    68  func (c Client) List(listParams *stripe.FileListParams) *Iter {
    69  	return &Iter{
    70  		Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
    71  			list := &stripe.FileList{}
    72  			err := c.B.CallRaw(http.MethodGet, "/v1/files", c.Key, b, p, list)
    73  
    74  			ret := make([]interface{}, len(list.Data))
    75  			for i, v := range list.Data {
    76  				ret[i] = v
    77  			}
    78  
    79  			return ret, list, err
    80  		}),
    81  	}
    82  }
    83  
    84  // Iter is an iterator for files.
    85  type Iter struct {
    86  	*stripe.Iter
    87  }
    88  
    89  // File returns the file which the iterator is currently pointing to.
    90  func (i *Iter) File() *stripe.File {
    91  	return i.Current().(*stripe.File)
    92  }
    93  
    94  // FileList returns the current list object which the iterator is
    95  // currently using. List objects will change as new API calls are made to
    96  // continue pagination.
    97  func (i *Iter) FileList() *stripe.FileList {
    98  	return i.List().(*stripe.FileList)
    99  }
   100  
   101  func getC() Client {
   102  	return Client{stripe.GetBackend(stripe.APIBackend), stripe.GetBackend(stripe.UploadsBackend), stripe.Key}
   103  }