ducky/subscriptions
ducky/subscriptions/client/subscription.go
Make api subpackage golint-passing. Add comments to all the exported functions, methods, and variables in the api subpackage, to make golint happy. Also, make the individual endpoints in the api subpackage unexported, as there's no real use case for exporting them. The handlers depend on the placeholders in the endpoint, so we need them to be controlled in unison, which means it's probably a bad idea to declare the route outside of the API package. And the only reason to expose the Handler is so people can declare custom endpoints.
| 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@7 | 11 func (c *Client) CreateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) { |
| paddy@7 | 12 resp, err := c.Post("/subscriptions/", change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID) |
| paddy@7 | 13 if err != nil { |
| paddy@10 | 14 hErr, ok := err.(httpErrors) |
| paddy@10 | 15 if ok { |
| paddy@10 | 16 for _, e := range hErr { |
| paddy@10 | 17 if e.Slug == commonAPI.RequestErrConflict && |
| paddy@10 | 18 e.Field == "/user_id" { |
| paddy@10 | 19 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionAlreadyExists |
| paddy@10 | 20 } else if e.Slug == commonAPI.RequestErrConflict && |
| paddy@10 | 21 e.Field == "/stripe_token" { |
| paddy@10 | 22 return subscriptions.Subscription{}, subscriptions.ErrStripeSubscriptionAlreadyExists |
| paddy@10 | 23 } |
| paddy@10 | 24 } |
| paddy@10 | 25 } |
| paddy@7 | 26 return subscriptions.Subscription{}, err |
| paddy@7 | 27 } |
| paddy@7 | 28 if len(resp.Subscriptions) < 1 { |
| paddy@7 | 29 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound |
| paddy@7 | 30 } |
| paddy@7 | 31 return resp.Subscriptions[0], nil |
| paddy@7 | 32 } |
| paddy@10 | 33 |
| paddy@10 | 34 func (c *Client) UpdateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) { |
| paddy@10 | 35 resp, err := c.Patch("/subscriptions/"+change.UserID.String(), change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID) |
| paddy@10 | 36 if err != nil { |
| paddy@10 | 37 return subscriptions.Subscription{}, err |
| paddy@10 | 38 } |
| paddy@10 | 39 if len(resp.Subscriptions) < 1 { |
| paddy@10 | 40 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound |
| paddy@10 | 41 } |
| paddy@10 | 42 return resp.Subscriptions[0], nil |
| paddy@10 | 43 } |