mirror of
https://github.com/rancher/norman.git
synced 2025-09-05 17:20:20 +00:00
Have URLParser parse the query too
This commit is contained in:
@@ -21,34 +21,34 @@ func QueryOptions(apiContext *types.APIContext, schema *types.Schema) types.Quer
|
|||||||
|
|
||||||
result := &types.QueryOptions{}
|
result := &types.QueryOptions{}
|
||||||
|
|
||||||
result.Sort = parseSort(schema, req)
|
result.Sort = parseSort(schema, apiContext)
|
||||||
result.Pagination = parsePagination(req)
|
result.Pagination = parsePagination(apiContext)
|
||||||
result.Conditions = parseFilters(schema, req)
|
result.Conditions = parseFilters(schema, apiContext)
|
||||||
|
|
||||||
return *result
|
return *result
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOrder(req *http.Request) types.SortOrder {
|
func parseOrder(apiContext *types.APIContext) types.SortOrder {
|
||||||
order := req.URL.Query().Get("order")
|
order := apiContext.Query.Get("order")
|
||||||
if types.SortOrder(order) == types.DESC {
|
if types.SortOrder(order) == types.DESC {
|
||||||
return types.DESC
|
return types.DESC
|
||||||
}
|
}
|
||||||
return types.ASC
|
return types.ASC
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSort(schema *types.Schema, req *http.Request) types.Sort {
|
func parseSort(schema *types.Schema, apiContext *types.APIContext) types.Sort {
|
||||||
sortField := req.URL.Query().Get("sort")
|
sortField := apiContext.Query.Get("sort")
|
||||||
if _, ok := schema.CollectionFilters[sortField]; !ok {
|
if _, ok := schema.CollectionFilters[sortField]; !ok {
|
||||||
sortField = ""
|
sortField = ""
|
||||||
}
|
}
|
||||||
return types.Sort{
|
return types.Sort{
|
||||||
Order: parseOrder(req),
|
Order: parseOrder(apiContext),
|
||||||
Name: sortField,
|
Name: sortField,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePagination(req *http.Request) *types.Pagination {
|
func parsePagination(apiContext *types.APIContext) *types.Pagination {
|
||||||
q := req.URL.Query()
|
q := apiContext.Query
|
||||||
limit := q.Get("limit")
|
limit := q.Get("limit")
|
||||||
marker := q.Get("marker")
|
marker := q.Get("marker")
|
||||||
|
|
||||||
@@ -86,9 +86,9 @@ func parseNameAndOp(value string) (string, types.ModifierType) {
|
|||||||
return name, types.ModifierType(op)
|
return name, types.ModifierType(op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFilters(schema *types.Schema, req *http.Request) []*types.QueryCondition {
|
func parseFilters(schema *types.Schema, apiContext *types.APIContext) []*types.QueryCondition {
|
||||||
var conditions []*types.QueryCondition
|
var conditions []*types.QueryCondition
|
||||||
for key, values := range req.URL.Query() {
|
for key, values := range apiContext.Query {
|
||||||
name, op := parseNameAndOp(key)
|
name, op := parseNameAndOp(key)
|
||||||
filter, ok := schema.CollectionFilters[name]
|
filter, ok := schema.CollectionFilters[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@@ -32,6 +32,7 @@ type ParsedURL struct {
|
|||||||
Action string
|
Action string
|
||||||
SubContext map[string]string
|
SubContext map[string]string
|
||||||
SubContextPrefix string
|
SubContextPrefix string
|
||||||
|
Query url.Values
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResolverFunc func(typeName string, context *types.APIContext) error
|
type ResolverFunc func(typeName string, context *types.APIContext) error
|
||||||
@@ -56,6 +57,7 @@ func DefaultURLParser(schemas *types.Schemas, url *url.URL) (ParsedURL, error) {
|
|||||||
result.SubContext = subContext
|
result.SubContext = subContext
|
||||||
result.SubContextPrefix = prefix
|
result.SubContextPrefix = prefix
|
||||||
result.Action, result.Method = parseAction(url)
|
result.Action, result.Method = parseAction(url)
|
||||||
|
result.Query = url.Query()
|
||||||
|
|
||||||
result.Type = safeIndex(parts, 1)
|
result.Type = safeIndex(parts, 1)
|
||||||
result.ID = safeIndex(parts, 2)
|
result.ID = safeIndex(parts, 2)
|
||||||
@@ -86,6 +88,7 @@ func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, ur
|
|||||||
result.ID = parsedURL.ID
|
result.ID = parsedURL.ID
|
||||||
result.Link = parsedURL.Link
|
result.Link = parsedURL.Link
|
||||||
result.Action = parsedURL.Action
|
result.Action = parsedURL.Action
|
||||||
|
result.Query = parsedURL.Query
|
||||||
if parsedURL.Method != "" {
|
if parsedURL.Method != "" {
|
||||||
result.Method = parsedURL.Method
|
result.Method = parsedURL.Method
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package types
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ValuesMap struct {
|
type ValuesMap struct {
|
||||||
@@ -79,6 +80,7 @@ type APIContext struct {
|
|||||||
Schema *Schema
|
Schema *Schema
|
||||||
Schemas *Schemas
|
Schemas *Schemas
|
||||||
Version *APIVersion
|
Version *APIVersion
|
||||||
|
Query url.Values
|
||||||
ResponseFormat string
|
ResponseFormat string
|
||||||
ReferenceValidator ReferenceValidator
|
ReferenceValidator ReferenceValidator
|
||||||
ResponseWriter ResponseWriter
|
ResponseWriter ResponseWriter
|
||||||
|
Reference in New Issue
Block a user