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.
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 func (c *Client) CreateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
12 resp, err := c.Post("/subscriptions/", change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
13 if err != nil {
14 hErr, ok := err.(httpErrors)
15 if ok {
16 for _, e := range hErr {
17 if e.Slug == commonAPI.RequestErrConflict &&
18 e.Field == "/user_id" {
19 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionAlreadyExists
20 } else if e.Slug == commonAPI.RequestErrConflict &&
21 e.Field == "/stripe_token" {
22 return subscriptions.Subscription{}, subscriptions.ErrStripeSubscriptionAlreadyExists
23 }
24 }
25 }
26 return subscriptions.Subscription{}, err
27 }
28 if len(resp.Subscriptions) < 1 {
29 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
30 }
31 return resp.Subscriptions[0], nil
32 }
34 func (c *Client) UpdateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
35 resp, err := c.Patch("/subscriptions/"+change.UserID.String(), change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
36 if err != nil {
37 return subscriptions.Subscription{}, err
38 }
39 if len(resp.Subscriptions) < 1 {
40 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
41 }
42 return resp.Subscriptions[0], nil
43 }