ducky/subscriptions
2015-06-22
Child:b063bc0a6e84
ducky/subscriptions/api/context_helpers.go
Add an API and subscriptionsd . Create a barebones implementation of the API, including only methods to create a Subscription and retrieve the Subscription associated with a user. Also create a subscriptiond service that will bootstrap the service and stores, and get everything stood up.
| 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@4 | 17 ErrSubscriptionStoreNotSet = errors.New("SubscriptionStore not set") |
| paddy@4 | 18 ErrStripeClientNotSet = errors.New("Stripe not set") |
| paddy@4 | 19 ) |
| paddy@4 | 20 |
| paddy@4 | 21 func getSubscriptionStore(c context.Context) (subscriptions.SubscriptionStore, error) { |
| paddy@4 | 22 store := c.Value(subscriptionStoreKey) |
| paddy@4 | 23 if store == nil { |
| paddy@4 | 24 return nil, ErrSubscriptionStoreNotSet |
| paddy@4 | 25 } |
| paddy@4 | 26 if s, ok := store.(subscriptions.SubscriptionStore); ok { |
| paddy@4 | 27 return s, nil |
| paddy@4 | 28 } |
| paddy@4 | 29 return nil, ErrSubscriptionStoreNotSet |
| paddy@4 | 30 } |
| paddy@4 | 31 |
| paddy@4 | 32 func WithSubscriptionStore(store subscriptions.SubscriptionStore, c context.Context) context.Context { |
| paddy@4 | 33 return context.WithValue(c, subscriptionStoreKey, store) |
| paddy@4 | 34 } |
| paddy@4 | 35 |
| paddy@4 | 36 func getStripeClient(c context.Context) (subscriptions.Stripe, error) { |
| paddy@4 | 37 stripe := c.Value(stripeKey) |
| paddy@4 | 38 if stripe == nil { |
| paddy@4 | 39 return subscriptions.Stripe{}, ErrStripeClientNotSet |
| paddy@4 | 40 } |
| paddy@4 | 41 if s, ok := stripe.(subscriptions.Stripe); ok { |
| paddy@4 | 42 return s, nil |
| paddy@4 | 43 } |
| paddy@4 | 44 return subscriptions.Stripe{}, ErrStripeClientNotSet |
| paddy@4 | 45 } |
| paddy@4 | 46 |
| paddy@4 | 47 func WithStripeClient(stripe subscriptions.Stripe, c context.Context) context.Context { |
| paddy@4 | 48 return context.WithValue(c, stripeKey, stripe) |
| paddy@4 | 49 } |