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