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
34 case *arvados.GroupContentsOptions:
35 rOpts.Select = opts.Select
36 rOpts.Count = opts.Count
41 func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} {
42 if len(selectParam) == 0 {
45 selected := map[string]interface{}{}
46 for _, attr := range selectParam {
47 if v, ok := orig[attr]; ok {
51 // Some keys are always preserved, even if not requested
52 for _, k := range []string{"etag", "kind", "writable_by"} {
53 if v, ok := orig[k]; ok {
60 func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
61 var tmp map[string]interface{}
63 if resp, ok := resp.(http.Handler); ok {
64 // resp knows how to write its own http response
66 resp.ServeHTTP(w, req)
70 w.Header().Set("X-Request-Id", req.Header.Get("X-Request-Id"))
71 err := rtr.transcode(resp, &tmp)
77 respKind := kind(resp)
79 tmp["kind"] = respKind
81 if included, ok := tmp["included"]; ok && included == nil {
82 tmp["included"] = make([]interface{}, 0)
85 if strings.HasSuffix(respKind, "List") {
86 defaultItemKind = strings.TrimSuffix(respKind, "List")
89 if _, isListResponse := tmp["items"].([]interface{}); isListResponse {
90 items, _ := tmp["items"].([]interface{})
91 included, _ := tmp["included"].([]interface{})
92 for _, slice := range [][]interface{}{items, included} {
93 for i, item := range slice {
94 // Fill in "kind" by inspecting UUID/PDH if
95 // possible; fall back on assuming each
96 // Items[] entry in an "arvados#fooList"
97 // response should have kind="arvados#foo".
98 item, _ := item.(map[string]interface{})
100 if uuid, _ := item["uuid"].(string); len(uuid) == 27 {
103 if k := kind(infixMap[infix]); k != "" {
105 } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" {
106 item["kind"] = "arvados#collection"
107 } else if defaultItemKind != "" {
108 item["kind"] = defaultItemKind
110 item = applySelectParam(opts.Select, item)
111 rtr.mungeItemFields(item)
115 if opts.Count == "none" {
116 delete(tmp, "items_available")
119 tmp = applySelectParam(opts.Select, tmp)
120 rtr.mungeItemFields(tmp)
123 w.Header().Set("Content-Type", "application/json")
124 enc := json.NewEncoder(w)
125 enc.SetEscapeHTML(false)
129 func (rtr *router) sendError(w http.ResponseWriter, err error) {
130 code := http.StatusInternalServerError
131 if err, ok := err.(interface{ HTTPStatus() int }); ok {
132 code = err.HTTPStatus()
134 httpserver.Error(w, err.Error(), code)
137 var infixMap = map[string]interface{}{
138 "4zz18": arvados.Collection{},
139 "xvhdp": arvados.ContainerRequest{},
140 "dz642": arvados.Container{},
141 "j7d0g": arvados.Group{},
142 "8i9sb": arvados.Job{},
143 "d1hrv": arvados.PipelineInstance{},
144 "p5p6p": arvados.PipelineTemplate{},
145 "j58dm": arvados.Specimen{},
146 "q1cn2": arvados.Trait{},
147 "7fd4e": arvados.Workflow{},
150 var mungeKind = regexp.MustCompile(`\..`)
152 func kind(resp interface{}) string {
153 t := fmt.Sprintf("%T", resp)
154 if !strings.HasPrefix(t, "arvados.") {
157 return mungeKind.ReplaceAllStringFunc(t, func(s string) string {
158 // "arvados.CollectionList" => "arvados#collectionList"
159 return "#" + strings.ToLower(s[1:])
163 func (rtr *router) mungeItemFields(tmp map[string]interface{}) {
164 for k, v := range tmp {
165 if strings.HasSuffix(k, "_at") {
166 // Format non-nil timestamps as
167 // rfc3339NanoFixed (otherwise they would use
168 // the default time encoding, which omits
170 switch tv := v.(type) {
172 if tv == nil || tv.IsZero() {
175 tmp[k] = tv.Format(rfc3339NanoFixed)
181 tmp[k] = tv.Format(rfc3339NanoFixed)
186 } else if t, err := time.Parse(time.RFC3339Nano, tv); err != nil {
187 // pass through an invalid time value (?)
188 } else if t.IsZero() {
191 tmp[k] = t.Format(rfc3339NanoFixed)
195 // Arvados API spec says when these fields are empty
196 // they appear in responses as null, rather than a
199 case "output_uuid", "output_name", "log_uuid", "description", "requesting_container_uuid", "container_uuid":
203 case "container_count_max":