1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/httpserver"
19 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
21 type responseOptions struct {
26 func (rtr *router) responseOptions(opts interface{}) (responseOptions, error) {
27 var rOpts responseOptions
28 switch opts := opts.(type) {
29 case *arvados.GetOptions:
30 rOpts.Select = opts.Select
31 case *arvados.ListOptions:
32 rOpts.Select = opts.Select
33 rOpts.Count = opts.Count
38 func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} {
39 if len(selectParam) == 0 {
42 selected := map[string]interface{}{}
43 for _, attr := range selectParam {
44 if v, ok := orig[attr]; ok {
48 // Some keys are always preserved, even if not requested
49 for _, k := range []string{"etag", "kind", "writable_by"} {
50 if v, ok := orig[k]; ok {
57 func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
58 var tmp map[string]interface{}
60 if resp, ok := resp.(http.Handler); ok {
61 // resp knows how to write its own http response
63 resp.ServeHTTP(w, req)
67 err := rtr.transcode(resp, &tmp)
73 respKind := kind(resp)
75 tmp["kind"] = respKind
77 if included, ok := tmp["included"]; ok && included == nil {
78 tmp["included"] = make([]interface{}, 0)
82 if strings.HasSuffix(respKind, "List") {
83 defaultItemKind = strings.TrimSuffix(respKind, "List")
86 if items, ok := tmp["items"].([]interface{}); ok {
87 for i, item := range items {
88 // Fill in "kind" by inspecting UUID/PDH if
89 // possible; fall back on assuming each
90 // Items[] entry in an "arvados#fooList"
91 // response should have kind="arvados#foo".
92 item, _ := item.(map[string]interface{})
94 if uuid, _ := item["uuid"].(string); len(uuid) == 27 {
97 if k := kind(infixMap[infix]); k != "" {
99 } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" {
100 item["kind"] = "arvados#collection"
101 } else if defaultItemKind != "" {
102 item["kind"] = defaultItemKind
104 item = applySelectParam(opts.Select, item)
105 rtr.mungeItemFields(item)
108 if opts.Count == "none" {
109 delete(tmp, "items_available")
112 tmp = applySelectParam(opts.Select, tmp)
113 rtr.mungeItemFields(tmp)
116 w.Header().Set("Content-Type", "application/json")
117 enc := json.NewEncoder(w)
118 enc.SetEscapeHTML(false)
122 func (rtr *router) sendError(w http.ResponseWriter, err error) {
123 code := http.StatusInternalServerError
124 if err, ok := err.(interface{ HTTPStatus() int }); ok {
125 code = err.HTTPStatus()
127 httpserver.Error(w, err.Error(), code)
130 var infixMap = map[string]interface{}{
131 "4zz18": arvados.Collection{},
132 "xvhdp": arvados.ContainerRequest{},
133 "dz642": arvados.Container{},
134 "j7d0g": arvados.Group{},
135 "8i9sb": arvados.Job{},
136 "d1hrv": arvados.PipelineInstance{},
137 "p5p6p": arvados.PipelineTemplate{},
138 "j58dm": arvados.Specimen{},
139 "q1cn2": arvados.Trait{},
140 "7fd4e": arvados.Workflow{},
143 var mungeKind = regexp.MustCompile(`\..`)
145 func kind(resp interface{}) string {
146 t := fmt.Sprintf("%T", resp)
147 if !strings.HasPrefix(t, "arvados.") {
150 return mungeKind.ReplaceAllStringFunc(t, func(s string) string {
151 // "arvados.CollectionList" => "arvados#collectionList"
152 return "#" + strings.ToLower(s[1:])
156 func (rtr *router) mungeItemFields(tmp map[string]interface{}) {
157 for k, v := range tmp {
158 if strings.HasSuffix(k, "_at") {
159 // Format non-nil timestamps as
160 // rfc3339NanoFixed (otherwise they would use
161 // the default time encoding, which omits
163 switch tv := v.(type) {
165 if tv == nil || tv.IsZero() {
168 tmp[k] = tv.Format(rfc3339NanoFixed)
174 tmp[k] = tv.Format(rfc3339NanoFixed)
179 } else if t, err := time.Parse(time.RFC3339Nano, tv); err != nil {
180 // pass through an invalid time value (?)
181 } else if t.IsZero() {
184 tmp[k] = t.Format(rfc3339NanoFixed)
188 // Arvados API spec says when these fields are empty
189 // they appear in responses as null, rather than a
192 case "output_uuid", "output_name", "log_uuid", "description", "requesting_container_uuid", "container_uuid":
196 case "container_count_max":