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.CreateOptions:
30 rOpts.Select = opts.Select
31 case *arvados.UpdateOptions:
32 rOpts.Select = opts.Select
33 case *arvados.GetOptions:
34 rOpts.Select = opts.Select
35 case *arvados.ListOptions:
36 rOpts.Select = opts.Select
37 rOpts.Count = opts.Count
38 case *arvados.GroupContentsOptions:
39 rOpts.Select = opts.Select
40 rOpts.Count = opts.Count
45 func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} {
46 if len(selectParam) == 0 {
49 selected := map[string]interface{}{}
50 for _, attr := range selectParam {
51 if v, ok := orig[attr]; ok {
55 // Some keys are always preserved, even if not requested
56 for _, k := range []string{"etag", "kind", "writable_by"} {
57 if v, ok := orig[k]; ok {
64 func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
65 var tmp map[string]interface{}
67 if resp, ok := resp.(http.Handler); ok {
68 // resp knows how to write its own http response
70 resp.ServeHTTP(w, req)
74 err := rtr.transcode(resp, &tmp)
80 respKind := kind(resp)
82 tmp["kind"] = respKind
84 if included, ok := tmp["included"]; ok && included == nil {
85 tmp["included"] = make([]interface{}, 0)
88 if strings.HasSuffix(respKind, "List") {
89 defaultItemKind = strings.TrimSuffix(respKind, "List")
92 if _, isListResponse := tmp["items"].([]interface{}); isListResponse {
93 items, _ := tmp["items"].([]interface{})
94 included, _ := tmp["included"].([]interface{})
95 for _, slice := range [][]interface{}{items, included} {
96 for i, item := range slice {
97 // Fill in "kind" by inspecting UUID/PDH if
98 // possible; fall back on assuming each
99 // Items[] entry in an "arvados#fooList"
100 // response should have kind="arvados#foo".
101 item, _ := item.(map[string]interface{})
103 if uuid, _ := item["uuid"].(string); len(uuid) == 27 {
106 if k := kind(infixMap[infix]); k != "" {
108 } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" {
109 item["kind"] = "arvados#collection"
110 } else if defaultItemKind != "" {
111 item["kind"] = defaultItemKind
113 item = applySelectParam(opts.Select, item)
114 rtr.mungeItemFields(item)
118 if opts.Count == "none" {
119 delete(tmp, "items_available")
122 tmp = applySelectParam(opts.Select, tmp)
123 rtr.mungeItemFields(tmp)
126 w.Header().Set("Content-Type", "application/json")
127 enc := json.NewEncoder(w)
128 enc.SetEscapeHTML(false)
132 func (rtr *router) sendError(w http.ResponseWriter, err error) {
133 code := http.StatusInternalServerError
134 if err, ok := err.(interface{ HTTPStatus() int }); ok {
135 code = err.HTTPStatus()
137 httpserver.Error(w, err.Error(), code)
140 var infixMap = map[string]interface{}{
141 "gj3su": arvados.APIClientAuthorization{},
142 "4zz18": arvados.Collection{},
143 "xvhdp": arvados.ContainerRequest{},
144 "dz642": arvados.Container{},
145 "j7d0g": arvados.Group{},
146 "8i9sb": arvados.Job{},
147 "d1hrv": arvados.PipelineInstance{},
148 "p5p6p": arvados.PipelineTemplate{},
149 "j58dm": arvados.Specimen{},
150 "q1cn2": arvados.Trait{},
151 "7fd4e": arvados.Workflow{},
154 var specialKindTransforms = map[string]string{
155 "arvados.APIClientAuthorization": "arvados#apiClientAuthorization",
156 "arvados.APIClientAuthorizationList": "arvados#apiClientAuthorizationList",
159 var mungeKind = regexp.MustCompile(`\..`)
161 func kind(resp interface{}) string {
162 t := fmt.Sprintf("%T", resp)
163 if !strings.HasPrefix(t, "arvados.") {
166 if k, ok := specialKindTransforms[t]; ok {
169 return mungeKind.ReplaceAllStringFunc(t, func(s string) string {
170 // "arvados.CollectionList" => "arvados#collectionList"
171 return "#" + strings.ToLower(s[1:])
175 func (rtr *router) mungeItemFields(tmp map[string]interface{}) {
176 for k, v := range tmp {
177 if strings.HasSuffix(k, "_at") {
178 // Format non-nil timestamps as
179 // rfc3339NanoFixed (otherwise they would use
180 // the default time encoding, which omits
182 switch tv := v.(type) {
184 if tv == nil || tv.IsZero() {
187 tmp[k] = tv.Format(rfc3339NanoFixed)
193 tmp[k] = tv.Format(rfc3339NanoFixed)
198 } else if t, err := time.Parse(time.RFC3339Nano, tv); err != nil {
199 // pass through an invalid time value (?)
200 } else if t.IsZero() {
203 tmp[k] = t.Format(rfc3339NanoFixed)
207 // Arvados API spec says when these fields are empty
208 // they appear in responses as null, rather than a
211 case "output_uuid", "output_name", "log_uuid", "description", "requesting_container_uuid", "container_uuid":
215 case "container_count_max":