ducky/subscriptions

Paddy 2015-10-04 Parent:aab6ba5ae392 Child:7eef47ecc01c

16:b063bc0a6e84 Browse Files

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.

api/context_helpers.go api/response.go api/subscription_handlers.go

     1.1 --- a/api/context_helpers.go	Wed Sep 30 01:33:53 2015 -0700
     1.2 +++ b/api/context_helpers.go	Sun Oct 04 21:58:07 2015 -0700
     1.3 @@ -14,8 +14,12 @@
     1.4  )
     1.5  
     1.6  var (
     1.7 +	// ErrSubscriptionStoreNotSet is returned when the Context is asked for a SubscriptionStore
     1.8 +	// but doesn't have one set.
     1.9  	ErrSubscriptionStoreNotSet = errors.New("SubscriptionStore not set")
    1.10 -	ErrStripeClientNotSet      = errors.New("Stripe not set")
    1.11 +	// ErrStripeClientNotSet is returned when the Context is asked for a Stripe client but doesn't
    1.12 +	// have one set.
    1.13 +	ErrStripeClientNotSet = errors.New("Stripe not set")
    1.14  )
    1.15  
    1.16  func getSubscriptionStore(c context.Context) (subscriptions.SubscriptionStore, error) {
    1.17 @@ -29,6 +33,8 @@
    1.18  	return nil, ErrSubscriptionStoreNotSet
    1.19  }
    1.20  
    1.21 +// WithSubscriptionStore adds the passed SubscriptionStore to a copy of the passed Context (overwriting
    1.22 +// any SubscriptionStore already set in the Context) and returns the new Context.
    1.23  func WithSubscriptionStore(store subscriptions.SubscriptionStore, c context.Context) context.Context {
    1.24  	return context.WithValue(c, subscriptionStoreKey, store)
    1.25  }
    1.26 @@ -44,6 +50,8 @@
    1.27  	return subscriptions.Stripe{}, ErrStripeClientNotSet
    1.28  }
    1.29  
    1.30 +// WithStripeClient adds the passed Stripe client to a copy of the passed Context (overwriting
    1.31 +// any Stripe client already set in the Context) and returns the new Context.
    1.32  func WithStripeClient(stripe subscriptions.Stripe, c context.Context) context.Context {
    1.33  	return context.WithValue(c, stripeKey, stripe)
    1.34  }
     2.1 --- a/api/response.go	Wed Sep 30 01:33:53 2015 -0700
     2.2 +++ b/api/response.go	Sun Oct 04 21:58:07 2015 -0700
     2.3 @@ -5,6 +5,8 @@
     2.4  	"code.secondbit.org/ducky/subscriptions.hg"
     2.5  )
     2.6  
     2.7 +// Response is used to structure the output returned as HTTP responses
     2.8 +// to requests.
     2.9  type Response struct {
    2.10  	Subscriptions []subscriptions.Subscription `json:"subscriptions,omitempty"`
    2.11  	Errors        []api.RequestError           `json:"errors,omitempty"`
     3.1 --- a/api/subscription_handlers.go	Wed Sep 30 01:33:53 2015 -0700
     3.2 +++ b/api/subscription_handlers.go	Sun Oct 04 21:58:07 2015 -0700
     3.3 @@ -14,7 +14,16 @@
     3.4  )
     3.5  
     3.6  var (
     3.7 -	ScopeSubscription      = auth.Scope{ID: "subscriptions", Name: "Manage Subscriptions", Description: "Read and update your subscription information."}
     3.8 +	// ScopeSubscription is an auth.Scope that controls access to
     3.9 +	// subscriptions.
    3.10 +	ScopeSubscription = auth.Scope{
    3.11 +		ID:          "subscriptions",
    3.12 +		Name:        "Manage Subscriptions",
    3.13 +		Description: "Read and update your subscription information.",
    3.14 +	}
    3.15 +	// ScopeSubscriptionAdmin is an auth.Scope that controls access
    3.16 +	// to subscriptions, bypassing the ACL to achieve administrator
    3.17 +	// access.
    3.18  	ScopeSubscriptionAdmin = auth.Scope{ID: "subscriptions_admin", Name: "Administer Subscriptions", Description: "Read and update subscription information, bypassing ACL."}
    3.19  )
    3.20  
    3.21 @@ -57,16 +66,19 @@
    3.22  	return changes
    3.23  }
    3.24  
    3.25 +// HandleSubscriptions registers the endpoints for the subscriptions API
    3.26 +// with the passed router. the passed Context will be passed to all the
    3.27 +// http.Handlers for the endpoints.
    3.28  func HandleSubscriptions(router *trout.Router, c context.Context) {
    3.29  	router.Endpoint("/subscriptions").Methods("POST", "OPTIONS").Handler(
    3.30 -		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, CreateSubscriptionHandler))))
    3.31 +		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, createSubscriptionHandler))))
    3.32  	router.Endpoint("/subscriptions/{id}").Methods("GET", "OPTIONS").Handler(
    3.33 -		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, GetSubscriptionHandler))))
    3.34 +		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, getSubscriptionHandler))))
    3.35  	router.Endpoint("/subscriptions/{id}").Methods("PATCH", "OPTIONS").Handler(
    3.36 -		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, PatchSubscriptionHandler))))
    3.37 +		api.CORSMiddleware(api.NegotiateMiddleware(api.ContextWrapper(c, patchSubscriptionHandler))))
    3.38  }
    3.39  
    3.40 -func CreateSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.41 +func createSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.42  	store, err := getSubscriptionStore(c)
    3.43  	if err != nil {
    3.44  		api.Encode(w, r, http.StatusInternalServerError, Response{Errors: api.ActOfGodError})
    3.45 @@ -110,7 +122,7 @@
    3.46  	api.Encode(w, r, http.StatusCreated, resp)
    3.47  }
    3.48  
    3.49 -func GetSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.50 +func getSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.51  	store, err := getSubscriptionStore(c)
    3.52  	if err != nil {
    3.53  		api.Encode(w, r, http.StatusInternalServerError, Response{Errors: api.ActOfGodError})
    3.54 @@ -154,7 +166,7 @@
    3.55  	api.Encode(w, r, http.StatusOK, resp)
    3.56  }
    3.57  
    3.58 -func PatchSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.59 +func patchSubscriptionHandler(w http.ResponseWriter, r *http.Request, c context.Context) {
    3.60  	store, err := getSubscriptionStore(c)
    3.61  	if err != nil {
    3.62  		api.Encode(w, r, http.StatusInternalServerError, Response{Errors: api.ActOfGodError})