ducky/subscriptions

Paddy 2015-10-04 Parent:b063bc0a6e84

17:7eef47ecc01c Go to Latest

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.

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