ducky/subscriptions

Paddy 2015-10-04 Parent:2c8250237566

17:7eef47ecc01c Go to Latest

ducky/subscriptions/client/subscription.go

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

History
1 package client
3 import (
4 commonAPI "code.secondbit.org/api.hg"
5 "code.secondbit.org/auth.hg"
7 "code.secondbit.org/ducky/subscriptions.hg"
8 "code.secondbit.org/ducky/subscriptions.hg/api"
9 )
11 // CreateSubscription creates a Subscription using the passed SubscriptionChange.
12 // It returns the created Subscription on success. It returns ErrSubscriptionAlreadyExists
13 // if a subscription already exists for that user. It returns ErrStripeSubscriptionAlreadyExists
14 // if that Stripe subscription is already associated with a user. It returns an
15 // ErrSubscriptionNotFound if no Subscription is returned in the response, which generally denotes
16 // a server error.
17 func (c *Client) CreateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
18 resp, err := c.Post("/subscriptions/", change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
19 if err != nil {
20 hErr, ok := err.(httpErrors)
21 if ok {
22 for _, e := range hErr {
23 if e.Slug == commonAPI.RequestErrConflict &&
24 e.Field == "/user_id" {
25 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionAlreadyExists
26 } else if e.Slug == commonAPI.RequestErrConflict &&
27 e.Field == "/stripe_token" {
28 return subscriptions.Subscription{}, subscriptions.ErrStripeSubscriptionAlreadyExists
29 }
30 }
31 }
32 return subscriptions.Subscription{}, err
33 }
34 if len(resp.Subscriptions) < 1 {
35 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
36 }
37 return resp.Subscriptions[0], nil
38 }
40 // UpdateSubscription applies the passed SubscriptionChange to the API. It returns
41 // the update Subscription on success. It returns an ErrSubscriptionNotFound if no
42 // subscription is returned in the response, which generall denotes a server error.
43 func (c *Client) UpdateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
44 resp, err := c.Patch("/subscriptions/"+change.UserID.String(), change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
45 if err != nil {
46 // BUG(paddy): We need to handle and gracefully return errors raised by the API.
47 return subscriptions.Subscription{}, err
48 }
49 if len(resp.Subscriptions) < 1 {
50 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
51 }
52 return resp.Subscriptions[0], nil
53 }