Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/tracing/internal/jaegerremote/utils/http_json.go
4096 views
1
// Copyright The OpenTelemetry Authors
2
// Copyright (c) 2021 The Jaeger Authors.
3
// Copyright (c) 2017 Uber Technologies, Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
//nolint:all
18
package utils
19
20
import (
21
"encoding/json"
22
"fmt"
23
"io"
24
"net/http"
25
)
26
27
// GetJSON makes an HTTP call to the specified URL and parses the returned JSON into `out`.
28
func GetJSON(url string, out interface{}) error {
29
resp, err := http.Get(url)
30
if err != nil {
31
return err
32
}
33
return ReadJSON(resp, out)
34
}
35
36
// ReadJSON reads JSON from http.Response and parses it into `out`.
37
func ReadJSON(resp *http.Response, out interface{}) error {
38
defer resp.Body.Close()
39
40
if resp.StatusCode >= 400 {
41
body, err := io.ReadAll(resp.Body)
42
if err != nil {
43
return err
44
}
45
46
return fmt.Errorf("status code: %d, body: %s", resp.StatusCode, body)
47
}
48
49
if out == nil {
50
_, err := io.Copy(io.Discard, resp.Body)
51
if err != nil {
52
return err
53
}
54
return nil
55
}
56
57
decoder := json.NewDecoder(resp.Body)
58
return decoder.Decode(out)
59
}
60
61