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