-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscovery.go
More file actions
98 lines (88 loc) · 3.76 KB
/
discovery.go
File metadata and controls
98 lines (88 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package hyperdrive
import (
"encoding/xml"
"net/http"
)
// RootResource contains information about the API and its Endpoints, and is
// the hypermedia respresentation returned by the Discovery URL endpoint for
// API clients to learn about the API.
type RootResource struct {
XMLName xml.Name `json:"-" xml:"api"`
Resource string `json:"resource" xml:"-"`
Name string `json:"name" xml:"name,attr"`
Endpoints []EndpointResource `json:"endpoints" xml:"endpoints>endpoint"`
}
// EndpointResource contains information about and Endpoint, and is
// the hypermedia respresentation returned by the Discovery URL endpoint for
// API clients to learn about the Endpoint.
type EndpointResource struct {
XMLName xml.Name `json:"-" xml:"endpoint"`
Resource string `json:"resource" xml:"-"`
Name string `json:"name" xml:"name,attr"`
Path string `json:"path" xml:"path,attr"`
MethodsList string `json:"-" xml:"methods,attr"`
Methods []string `json:"methods" xml:"-"`
MediaTypesList string `json:"-" xml:"media-types,attr"`
MediaTypes []string `json:"media-types" xml:"-"`
Desc string `json:"description" xml:"description"`
Params []EndpointResourceParam `json:"params" xml:"params>param"`
}
// EndpointResourceParam contains information about endpoint parameters, and is
// part of the hypermedia representation returned by the Discovery URL endpoint
// for API clients to learn about input allowed (and/or required) by the
// Endpoint.
type EndpointResourceParam struct {
XMLName xml.Name `json:"-" xml:"param"`
Name string `json:"name" xml:"name,attr"`
Desc string `json:"description" xml:"description"`
AllowedList string `json:"-" xml:"allowed,attr"`
Allowed []string `json:"allowed" xml:"-"`
RequiredList string `json:"-" xml:"required,attr"`
Required []string `json:"required" xml:"-"`
}
// NewRootResource creates an instance of RootResource from the given API.
func NewRootResource(api API) *RootResource {
return &RootResource{Resource: "api", Name: api.Name}
}
// NewEndpointResource creates an instance of EndpointResource from the given Endpointer.
func NewEndpointResource(e Endpointer) EndpointResource {
return EndpointResource{
Resource: "endpoint",
Name: e.GetName(),
Path: e.GetPath(),
MethodsList: GetMethodsList(e),
Methods: GetMethods(e),
MediaTypesList: GetContentTypesList(hAPI, e),
MediaTypes: GetContentTypes(hAPI, e),
Desc: e.GetDesc(),
Params: createEndpointResourceParams(e),
}
}
// NewEndpointResourceParam creates an instance of EndpointResourceParam from the given parsedParam.
func NewEndpointResourceParam(p parsedParam) EndpointResourceParam {
return EndpointResourceParam{
Name: p.Name,
Desc: p.Desc,
Allowed: p.Allowed,
AllowedList: p.AllowedList(),
Required: p.Required,
RequiredList: p.RequiredList(),
}
}
func createEndpointResourceParams(e Endpointer) []EndpointResourceParam {
var params = []EndpointResourceParam{}
pp := parseEndpoint(e)
for _, p := range pp {
params = append(params, NewEndpointResourceParam(p))
}
return params
}
// AddEndpoint adds EndpointResources to the slice of Endpoints on an instance of RootResource.
func (root *RootResource) AddEndpoint(e Endpointer) {
root.Endpoints = append(root.Endpoints, NewEndpointResource(e))
}
// ServeHTTP satisfies the http.Handler interface and returns the hypermedia
// representation of the Discovery URL.
func (root *RootResource) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
Respond(rw, r, 200, root)
}