mirror of
https://github.com/rancher/norman.git
synced 2025-09-03 08:14:40 +00:00
APIContext constructor
This commit is contained in:
@@ -69,14 +69,9 @@ func DefaultURLParser(schemas *types.Schemas, url *url.URL) (ParsedURL, error) {
|
|||||||
func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, urlParser URLParser, resolverFunc ResolverFunc) (*types.APIContext, error) {
|
func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, urlParser URLParser, resolverFunc ResolverFunc) (*types.APIContext, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
result := &types.APIContext{
|
result := types.NewAPIContext(req, rw, schemas)
|
||||||
Schemas: schemas,
|
result.Method = parseMethod(req)
|
||||||
Request: req,
|
result.ResponseFormat = parseResponseFormat(req)
|
||||||
Response: rw,
|
|
||||||
Method: parseMethod(req),
|
|
||||||
ResponseFormat: parseResponseFormat(req),
|
|
||||||
}
|
|
||||||
|
|
||||||
result.URLBuilder, _ = urlbuilder.New(req, types.APIVersion{}, schemas)
|
result.URLBuilder, _ = urlbuilder.New(req, types.APIVersion{}, schemas)
|
||||||
|
|
||||||
// The response format is guarenteed to be set even in the event of an error
|
// The response format is guarenteed to be set even in the event of an error
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -100,6 +101,23 @@ type APIContext struct {
|
|||||||
Response http.ResponseWriter
|
Response http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type apiContextKey struct{}
|
||||||
|
|
||||||
|
func NewAPIContext(req *http.Request, resp http.ResponseWriter, schemas *Schemas) *APIContext {
|
||||||
|
apiCtx := &APIContext{
|
||||||
|
Response: resp,
|
||||||
|
Schemas: schemas,
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(req.Context(), apiContextKey{}, apiCtx)
|
||||||
|
apiCtx.Request = req.WithContext(ctx)
|
||||||
|
return apiCtx
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAPIContext(ctx context.Context) *APIContext {
|
||||||
|
apiContext, _ := ctx.Value(apiContextKey{}).(*APIContext)
|
||||||
|
return apiContext
|
||||||
|
}
|
||||||
|
|
||||||
func (r *APIContext) WriteResponse(code int, obj interface{}) {
|
func (r *APIContext) WriteResponse(code int, obj interface{}) {
|
||||||
r.ResponseWriter.Write(r, code, obj)
|
r.ResponseWriter.Write(r, code, obj)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user