ducky/subscriptions
2015-10-04
Parent:b063bc0a6e84
ducky/subscriptions/api/context_helpers.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@4 | 1 package api |
| paddy@4 | 2 |
| paddy@4 | 3 import ( |
| paddy@4 | 4 "errors" |
| paddy@4 | 5 |
| paddy@4 | 6 "code.secondbit.org/ducky/subscriptions.hg" |
| paddy@4 | 7 |
| paddy@4 | 8 "golang.org/x/net/context" |
| paddy@4 | 9 ) |
| paddy@4 | 10 |
| paddy@4 | 11 const ( |
| paddy@4 | 12 subscriptionStoreKey = "SubscriptionStore" |
| paddy@4 | 13 stripeKey = "Stripe" |
| paddy@4 | 14 ) |
| paddy@4 | 15 |
| paddy@4 | 16 var ( |
| paddy@16 | 17 // ErrSubscriptionStoreNotSet is returned when the Context is asked for a SubscriptionStore |
| paddy@16 | 18 // but doesn't have one set. |
| paddy@4 | 19 ErrSubscriptionStoreNotSet = errors.New("SubscriptionStore not set") |
| paddy@16 | 20 // ErrStripeClientNotSet is returned when the Context is asked for a Stripe client but doesn't |
| paddy@16 | 21 // have one set. |
| paddy@16 | 22 ErrStripeClientNotSet = errors.New("Stripe not set") |
| paddy@4 | 23 ) |
| paddy@4 | 24 |
| paddy@4 | 25 func getSubscriptionStore(c context.Context) (subscriptions.SubscriptionStore, error) { |
| paddy@4 | 26 store := c.Value(subscriptionStoreKey) |
| paddy@4 | 27 if store == nil { |
| paddy@4 | 28 return nil, ErrSubscriptionStoreNotSet |
| paddy@4 | 29 } |
| paddy@4 | 30 if s, ok := store.(subscriptions.SubscriptionStore); ok { |
| paddy@4 | 31 return s, nil |
| paddy@4 | 32 } |
| paddy@4 | 33 return nil, ErrSubscriptionStoreNotSet |
| paddy@4 | 34 } |
| paddy@4 | 35 |
| paddy@16 | 36 // WithSubscriptionStore adds the passed SubscriptionStore to a copy of the passed Context (overwriting |
| paddy@16 | 37 // any SubscriptionStore already set in the Context) and returns the new Context. |
| paddy@4 | 38 func WithSubscriptionStore(store subscriptions.SubscriptionStore, c context.Context) context.Context { |
| paddy@4 | 39 return context.WithValue(c, subscriptionStoreKey, store) |
| paddy@4 | 40 } |
| paddy@4 | 41 |
| paddy@4 | 42 func getStripeClient(c context.Context) (subscriptions.Stripe, error) { |
| paddy@4 | 43 stripe := c.Value(stripeKey) |
| paddy@4 | 44 if stripe == nil { |
| paddy@4 | 45 return subscriptions.Stripe{}, ErrStripeClientNotSet |
| paddy@4 | 46 } |
| paddy@4 | 47 if s, ok := stripe.(subscriptions.Stripe); ok { |
| paddy@4 | 48 return s, nil |
| paddy@4 | 49 } |
| paddy@4 | 50 return subscriptions.Stripe{}, ErrStripeClientNotSet |
| paddy@4 | 51 } |
| paddy@4 | 52 |
| paddy@16 | 53 // WithStripeClient adds the passed Stripe client to a copy of the passed Context (overwriting |
| paddy@16 | 54 // any Stripe client already set in the Context) and returns the new Context. |
| paddy@4 | 55 func WithStripeClient(stripe subscriptions.Stripe, c context.Context) context.Context { |
| paddy@4 | 56 return context.WithValue(c, stripeKey, stripe) |
| paddy@4 | 57 } |