ducky/subscriptions

Paddy 2015-10-04 Parent:b063bc0a6e84

17:7eef47ecc01c tip Browse Files

Document our client to make golint happy. Take care of all the documentation warnings in the client subpackage, which means golint now returns successfully.

client/client.go client/subscription.go

     1.1 --- a/client/client.go	Sun Oct 04 21:58:07 2015 -0700
     1.2 +++ b/client/client.go	Sun Oct 04 22:27:54 2015 -0700
     1.3 @@ -17,16 +17,24 @@
     1.4  )
     1.5  
     1.6  var (
     1.7 -	ErrNilClient     = errors.New("nil client wrapper")
     1.8 +	// ErrNilClient is returned when a method is called on a nil Client.
     1.9 +	ErrNilClient = errors.New("nil client wrapper")
    1.10 +	// ErrNilHTTPClient is returned when a method is called on a Client
    1.11 +	// with its http.Client not set.
    1.12  	ErrNilHTTPClient = errors.New("nil client")
    1.13  )
    1.14  
    1.15 +// Client is a wrapper that bundles all the information necessary to interact
    1.16 +// with the subscriptions API.
    1.17  type Client struct {
    1.18  	client  *http.Client
    1.19  	address string
    1.20  	ID      uuid.ID
    1.21  }
    1.22  
    1.23 +// New returns a new Client. The passed address is the base address for the
    1.24 +// subscriptions API, as an absolute or relative URL. The passed ID is the
    1.25 +// OAuth2 client ID to use for the Client.
    1.26  func New(address string, id uuid.ID) *Client {
    1.27  	address = strings.TrimRight(address, "/")
    1.28  	return &Client{
    1.29 @@ -100,14 +108,32 @@
    1.30  	return fmt.Sprintf("%+#v", h)
    1.31  }
    1.32  
    1.33 +// Get returns the api Response object for the passed URL called
    1.34 +// over HTTP GET. The passed scope IDs will be applied to the request,
    1.35 +// and the request will be made on behalf of the subject (user)
    1.36 +// specified by the passed ID. If the zero value is passed for the ID,
    1.37 +// the request will be made without a subject. To request no scopes,
    1.38 +// pass an empty slice or nil.
    1.39  func (c *Client) Get(url string, scopes []string, subject uuid.ID) (api.Response, error) {
    1.40  	return c.do("GET", url, nil, scopes, subject)
    1.41  }
    1.42  
    1.43 +// Post returns the api Response object for the passed URL called
    1.44 +// over HTTP POST. The passed scope IDs will be applied to the request,
    1.45 +// and the request will be made on behalf of the subject (user)
    1.46 +// specified by the passed ID. If the zero value is passed for the ID,
    1.47 +// the request will be made without a subject. To request no scopes,
    1.48 +// pass an empty slice or nil.
    1.49  func (c *Client) Post(url string, request interface{}, scopes []string, subject uuid.ID) (api.Response, error) {
    1.50  	return c.do("POST", url, request, scopes, subject)
    1.51  }
    1.52  
    1.53 +// Patch returns the api Response object for the passed URL called
    1.54 +// over HTTP PATCH. The passed scope IDs will be applied to the request,
    1.55 +// and the request will be made on behalf of the subject (user)
    1.56 +// specified by the passed ID. If the zero value is passed for the ID,
    1.57 +// the request will be made without a subject. To request no scopes,
    1.58 +// pass an empty slice or nil.
    1.59  func (c *Client) Patch(url string, request interface{}, scopes []string, subject uuid.ID) (api.Response, error) {
    1.60  	return c.do("PATCH", url, request, scopes, subject)
    1.61  }
     2.1 --- a/client/subscription.go	Sun Oct 04 21:58:07 2015 -0700
     2.2 +++ b/client/subscription.go	Sun Oct 04 22:27:54 2015 -0700
     2.3 @@ -8,6 +8,12 @@
     2.4  	"code.secondbit.org/ducky/subscriptions.hg/api"
     2.5  )
     2.6  
     2.7 +// CreateSubscription creates a Subscription using the passed SubscriptionChange.
     2.8 +// It returns the created Subscription on success. It returns ErrSubscriptionAlreadyExists
     2.9 +// if a subscription already exists for that user. It returns ErrStripeSubscriptionAlreadyExists
    2.10 +// if that Stripe subscription is already associated with a user. It returns an
    2.11 +// ErrSubscriptionNotFound if no Subscription is returned in the response, which generally denotes
    2.12 +// a server error.
    2.13  func (c *Client) CreateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
    2.14  	resp, err := c.Post("/subscriptions/", change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
    2.15  	if err != nil {
    2.16 @@ -31,9 +37,13 @@
    2.17  	return resp.Subscriptions[0], nil
    2.18  }
    2.19  
    2.20 +// UpdateSubscription applies the passed SubscriptionChange to the API. It returns
    2.21 +// the update Subscription on success. It returns an ErrSubscriptionNotFound if no
    2.22 +// subscription is returned in the response, which generall denotes a server error.
    2.23  func (c *Client) UpdateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
    2.24  	resp, err := c.Patch("/subscriptions/"+change.UserID.String(), change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
    2.25  	if err != nil {
    2.26 +		// BUG(paddy): We need to handle and gracefully return errors raised by the API.
    2.27  		return subscriptions.Subscription{}, err
    2.28  	}
    2.29  	if len(resp.Subscriptions) < 1 {