ducky/subscriptions

Paddy 2015-10-04 Parent:36e90e828dd0

16:b063bc0a6e84 Go to Latest

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.

History
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 }