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

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