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.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/api/context_helpers.go Mon Jun 22 18:50:02 2015 -0400 1.3 @@ -0,0 +1,49 @@ 1.4 +package api 1.5 + 1.6 +import ( 1.7 + "errors" 1.8 + 1.9 + "code.secondbit.org/ducky/subscriptions.hg" 1.10 + 1.11 + "golang.org/x/net/context" 1.12 +) 1.13 + 1.14 +const ( 1.15 + subscriptionStoreKey = "SubscriptionStore" 1.16 + stripeKey = "Stripe" 1.17 +) 1.18 + 1.19 +var ( 1.20 + ErrSubscriptionStoreNotSet = errors.New("SubscriptionStore not set") 1.21 + ErrStripeClientNotSet = errors.New("Stripe not set") 1.22 +) 1.23 + 1.24 +func getSubscriptionStore(c context.Context) (subscriptions.SubscriptionStore, error) { 1.25 + store := c.Value(subscriptionStoreKey) 1.26 + if store == nil { 1.27 + return nil, ErrSubscriptionStoreNotSet 1.28 + } 1.29 + if s, ok := store.(subscriptions.SubscriptionStore); ok { 1.30 + return s, nil 1.31 + } 1.32 + return nil, ErrSubscriptionStoreNotSet 1.33 +} 1.34 + 1.35 +func WithSubscriptionStore(store subscriptions.SubscriptionStore, c context.Context) context.Context { 1.36 + return context.WithValue(c, subscriptionStoreKey, store) 1.37 +} 1.38 + 1.39 +func getStripeClient(c context.Context) (subscriptions.Stripe, error) { 1.40 + stripe := c.Value(stripeKey) 1.41 + if stripe == nil { 1.42 + return subscriptions.Stripe{}, ErrStripeClientNotSet 1.43 + } 1.44 + if s, ok := stripe.(subscriptions.Stripe); ok { 1.45 + return s, nil 1.46 + } 1.47 + return subscriptions.Stripe{}, ErrStripeClientNotSet 1.48 +} 1.49 + 1.50 +func WithStripeClient(stripe subscriptions.Stripe, c context.Context) context.Context { 1.51 + return context.WithValue(c, stripeKey, stripe) 1.52 +}