alficles commented on a change in pull request #2365: Add TO Go api helpers
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2365#discussion_r194853155
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/api/api.go
 ##########
 @@ -0,0 +1,304 @@
+package api
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "context"
+       "database/sql"
+       "encoding/json"
+       "errors"
+       "fmt"
+       "io"
+       "net/http"
+       "strconv"
+       "strings"
+
+       "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/config"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+
+       "github.com/jmoiron/sqlx"
+)
+
+const DBContextKey = "db"
+const ConfigContextKey = "context"
+
+// WriteResp takes any object, serializes it as JSON, and writes that to w. 
Any errors are logged and written to w via tc.GetHandleErrorsFunc.
+// This is a helper for the common case; not using this in unusual cases is 
perfectly acceptable.
+func WriteResp(w http.ResponseWriter, r *http.Request, v interface{}) {
+       resp := struct {
+               Response interface{} `json:"response"`
+       }{v}
+       WriteRespRaw(w, r, resp)
+}
+
+// WriteRespRaw acts like WriteResp, but doesn't wrap the object in a 
`{"response":` object. This should be used to respond with endpoints which 
don't wrap their response in a "response" object.
+func WriteRespRaw(w http.ResponseWriter, r *http.Request, v interface{}) {
+       bts, err := json.Marshal(v)
+       if err != nil {
+               log.Errorf("marshalling JSON (raw) for %T: %v", v, err)
+               tc.GetHandleErrorsFunc(w, r)(http.StatusInternalServerError, 
errors.New(http.StatusText(http.StatusInternalServerError)))
+               return
+       }
+       w.Header().Set("Content-Type", "application/json")
+       w.Write(bts)
+}
+
+// WriteRespVals is like WriteResp, but also takes a map of root-level values 
to write. The API most commonly needs these for meta-parameters, like size, 
limit, and orderby.
+// This is a helper for the common case; not using this in unusual cases is 
perfectly acceptable.
+func WriteRespVals(w http.ResponseWriter, r *http.Request, v interface{}, vals 
map[string]interface{}) {
+       vals["response"] = v
 
 Review comment:
   Yeah. It's not critical, I think. A comment should suffice. A `const map` 
would be better, but alas, it is not an option. :)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to