mhoppa commented on a change in pull request #3966: Add server capabilities API URL: https://github.com/apache/trafficcontrol/pull/3966#discussion_r333540596
########## File path: traffic_ops/client/servercapability.go ########## @@ -0,0 +1,103 @@ +/* + + Licensed 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. +*/ + +package client + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + + "github.com/apache/trafficcontrol/lib/go-tc" +) + +const ( + APIServerCapabilities = apiBase + "/server_capabilities" +) + +// CreateServerCapability creates a server capability and returns the response. +func (to *Session) CreateServerCapability(sc tc.ServerCapability) (*tc.ServerCapabilityDetailResponse, ReqInf, error) { + var remoteAddr net.Addr + reqBody, err := json.Marshal(sc) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, APIServerCapabilities, reqBody) + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + var scResp tc.ServerCapabilityDetailResponse + if err = json.NewDecoder(resp.Body).Decode(&scResp); err != nil { + return nil, reqInf, err + } + return &scResp, reqInf, nil +} + +// GetServerCapabilities returns all the server capabilities. +func (to *Session) GetServerCapabilities() ([]tc.ServerCapability, ReqInf, error) { + resp, remoteAddr, err := to.request(http.MethodGet, APIServerCapabilities, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.ServerCapabilitiesResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GetServerCapability returns the given server capability by name. +func (to *Session) GetServerCapability(name string) (*tc.ServerCapability, ReqInf, error) { + url := fmt.Sprintf("%s?name=%s", APIServerCapabilities, name) Review comment: Non blocking as we dont do it anywhere but for here and below, on query parameters, we should use the net/url package to build them out so they are correctly url encoded. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to 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
