ducky/subscriptions
ducky/subscriptions/client/subscription.go
Add comments, move ChangingSystemProperties to the api package. Add comments to all our exported types and variables in subscription.go, both to make golint happy and because it's good to have comments. Move the subscriptions.ChangingSystemProperties helper to api.changingSystemProperties, because it returns API-specific strings and there's no real reason it has to be in the subscriptions package--everything it needs to work on is exported.
| 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 } |