forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
51 lines (47 loc) · 1.23 KB
/
schema.go
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
package api
import (
"encoding/json"
"fmt"
"net/http"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
// swagger:response
//nolint:deadcode,unused
type schemaResponse struct {
// in: body
Schemas map[string]apitypes.EntrySchema
}
// swagger:route GET /fs/schema schema entrySchema
//
// Schema for an entry at path
//
// Returns a map of Type IDs to EntrySchema objects describing the plugin schema starting at the
// given path. The first key in the map corresponds to the path's schema.
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: schemaResponse
// 400: errorResp
// 404: errorResp
// 500: errorResp
var schemaHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *errorResponse {
entry, path, errResp := getEntryFromRequest(r)
if errResp != nil {
return errResp
}
jsonEncoder := json.NewEncoder(w)
schema, err := plugin.Schema(entry)
if err != nil {
return unknownErrorResponse(err)
}
apiEntrySchema := apitypes.NewEntrySchema(schema)
if err := jsonEncoder.Encode(apiEntrySchema); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal schema for %v: %v", path, err))
}
return nil
}}