alficles commented on a change in pull request #2365: Add TO Go api helpers URL: https://github.com/apache/incubator-trafficcontrol/pull/2365#discussion_r194738662
########## 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 + respBts, err := json.Marshal(vals) + if err != nil { + log.Errorf("marshalling JSON 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(respBts) +} + +// HandleErr handles an API error, writing the given statusCode and userErr to the user, and logging the sysErr. If userErr is nil, the text of the HTTP statusCode is written. +// This is a helper for the common case; not using this in unusual cases is perfectly acceptable. +func HandleErr(w http.ResponseWriter, r *http.Request, statusCode int, userErr error, sysErr error) { + if sysErr != nil { + log.Errorln(r.RemoteAddr + " " + sysErr.Error()) + } + if userErr == nil { + userErr = errors.New(http.StatusText(statusCode)) + } + respBts, err := json.Marshal(tc.CreateErrorAlerts(userErr)) + if err != nil { + log.Errorln("marshalling error: " + err.Error()) + *r = *r.WithContext(context.WithValue(r.Context(), tc.StatusKey, http.StatusInternalServerError)) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + *r = *r.WithContext(context.WithValue(r.Context(), tc.StatusKey, statusCode)) + w.Header().Set(tc.ContentType, tc.ApplicationJson) + w.Write(respBts) +} + +// RespWriter is a helper to allow a one-line response, for endpoints with a function that returns the object that needs to be written and an error. +// This is a helper for the common case; not using this in unusual cases is perfectly acceptable. +func RespWriter(w http.ResponseWriter, r *http.Request) func(v interface{}, err error) { + return func(v interface{}, err error) { + if err != nil { + HandleErr(w, r, http.StatusInternalServerError, nil, err) + return + } + WriteResp(w, r, v) + } +} + +// RespWriterVals is like RespWriter, 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 RespWriterVals(w http.ResponseWriter, r *http.Request, vals map[string]interface{}) func(v interface{}, err error) { + return func(v interface{}, err error) { + if err != nil { + HandleErr(w, r, http.StatusInternalServerError, nil, err) + return + } + WriteRespVals(w, r, v, vals) + } +} + +// WriteRespAlert creates an alert, 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 WriteRespAlert(w http.ResponseWriter, r *http.Request, level tc.AlertLevel, msg string) { + resp := struct{ tc.Alerts }{tc.CreateAlerts(level, msg)} + respBts, err := json.Marshal(resp) Review comment: This looks like it should produce JSON with a capital key: `{ "Alerts" : { ...`. Is that correct? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
