Path: blob/main/pkg/flow/tracing/internal/jaegerremote/utils/http_json.go
4096 views
// Copyright The OpenTelemetry Authors1// Copyright (c) 2021 The Jaeger Authors.2// Copyright (c) 2017 Uber Technologies, Inc.3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516//nolint:all17package utils1819import (20"encoding/json"21"fmt"22"io"23"net/http"24)2526// GetJSON makes an HTTP call to the specified URL and parses the returned JSON into `out`.27func GetJSON(url string, out interface{}) error {28resp, err := http.Get(url)29if err != nil {30return err31}32return ReadJSON(resp, out)33}3435// ReadJSON reads JSON from http.Response and parses it into `out`.36func ReadJSON(resp *http.Response, out interface{}) error {37defer resp.Body.Close()3839if resp.StatusCode >= 400 {40body, err := io.ReadAll(resp.Body)41if err != nil {42return err43}4445return fmt.Errorf("status code: %d, body: %s", resp.StatusCode, body)46}4748if out == nil {49_, err := io.Copy(io.Discard, resp.Body)50if err != nil {51return err52}53return nil54}5556decoder := json.NewDecoder(resp.Body)57return decoder.Decode(out)58}596061