From 22c17734fdbac983e8d48d3c4d626fe328e454d3 Mon Sep 17 00:00:00 2001 From: Eric Marden Date: Sat, 11 Mar 2017 17:30:34 -0600 Subject: [PATCH] routes requests to an endpoint with Accept header - this allows requests to make it to the proper endpoint to support multiple versions of the same endpoint, based on media type, at the same path fixes #27 --- hyperdrive.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/hyperdrive.go b/hyperdrive.go index 910b4e5..fd85239 100644 --- a/hyperdrive.go +++ b/hyperdrive.go @@ -45,7 +45,9 @@ func NewAPI(name string, desc string) API { // HTTP method. func (api *API) AddEndpoint(e Endpointer) { api.Root.AddEndpoint(e) - api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e))) + api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e))). + Headers("Accept", api.GetContentTypeJSON(e)). + Headers("Accet", api.GetContentTypeXML(e)) } // GetMediaType returns a media type string, sans any content-type extension (e.g. json), @@ -58,6 +60,27 @@ func (api *API) GetMediaType(e Endpointer) string { return fmt.Sprintf("application/vnd.%s.%s.%s", slug(api.Name), slug(e.GetName()), e.GetVersion()) } +// GetContentTypeJSON returns the json Content-Type an endpoint can accept and +// respond with. The Content-Type will include the versioned vendor Media +// Type returned by API.GetMediaType() with a json extension. +func (api *API) GetContentTypeJSON(e Endpointer) string { + return fmt.Sprintf("%s.json", api.GetMediaType(e)) +} + +// GetContentTypeXML returns the xml Content-Type an endpoint can accept and +// respond with. The Content-Type will include the versioned vendor Media +// Type returned by API.GetMediaType() with an xml extension. +func (api *API) GetContentTypeXML(e Endpointer) string { + return fmt.Sprintf("%s.xml", api.GetMediaType(e)) +} + +// GetContentTypes returns a slice of Content-Types the endpoint can accept and +// respond with. The Content-Types will include both the versioned vendor Media +// Type returned by API.GetMediaType() for both json and xml. +func (api *API) GetContentTypes(e Endpointer) []string { + return []string{api.GetContentTypeJSON(e), api.GetContentTypeXML(e)} +} + // Start starts the configured http server, listening on the configured Port // (default: 5000). Set the PORT environment variable to change this. func (api *API) Start() {