aboutsummaryrefslogtreecommitdiff
path: root/clients/netbox/http.go
blob: 21a2b4bd855620cec6edff8b9ea5adbd07938dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package netbox

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

// NetboxHttpClient is an HTTP client for the Netbox API. It is very
// low-level and should not be consumed by most clients. Instead use a
// client implementing NetboxClient.
type NetboxHttpClient struct {
	ApiKey   string
	Endpoint url.URL
}

func MustNewNetboxHttpClient(apiKey, endpoint string) *NetboxHttpClient {
	u, err := url.Parse(endpoint)
	if err != nil {
		panic(err)
	}
	return &NetboxHttpClient{apiKey, *u}
}

func (c *NetboxHttpClient) Do(ctx context.Context, r NetboxRequest, out any) error {
	u := c.Endpoint // Store a local copy, requests may mutate it
	req, err := r.BuildRequest(ctx, &u)
	if err != nil {
		return err
	}

	req.Header.Add("Authorization", fmt.Sprintf("Token %s", c.ApiKey))

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusOK {
		apiError := &ApiError{Status: res.StatusCode}
		if err = json.NewDecoder(res.Body).Decode(apiError); err != nil {
			return fmt.Errorf("Netbox JSON decode error while parsing error with status %d: %w", res.StatusCode, err)
		}
		return apiError
	}

	if out != nil {
		return json.NewDecoder(res.Body).Decode(out)
	}

	return nil
}

type NetboxRequest interface {
	BuildRequest(context.Context, *url.URL) (*http.Request, error)
}

type NetboxGraphQLRequest struct {
	Query string
}

var _ NetboxRequest = (*NetboxGraphQLRequest)(nil)

func (r *NetboxGraphQLRequest) BuildRequest(ctx context.Context, host *url.URL) (*http.Request, error) {
	q, err := json.Marshal(map[string]string{"query": r.Query})
	if err != nil {
		return nil, err
	}

	host.Path = "/graphql/"

	req, err := http.NewRequestWithContext(
		ctx,
		http.MethodPost,
		host.String(),
		bytes.NewBuffer(q),
	)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")

	return req, nil
}

type NetboxGetRequest struct {
	url.Values
	Path string
}

var _ NetboxRequest = (*NetboxGetRequest)(nil)

func NewNetboxGetRequest(path string) *NetboxGetRequest {
	return &NetboxGetRequest{url.Values{}, path}
}

func (r *NetboxGetRequest) BuildRequest(ctx context.Context, host *url.URL) (*http.Request, error) {
	host.Path = r.Path
	host.RawQuery = r.Encode()

	req, err := http.NewRequestWithContext(
		ctx,
		http.MethodGet,
		host.String(),
		nil,
	)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Accept", "application/json")

	return req, nil
}

type NetboxRawGet struct {
	Url string
}

var _ NetboxRequest = (*NetboxRawGet)(nil)

func (r *NetboxRawGet) BuildRequest(ctx context.Context, host *url.URL) (*http.Request, error) {
	req, err := http.NewRequestWithContext(
		ctx,
		http.MethodGet,
		r.Url,
		nil,
	)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Accept", "application/json")

	return req, nil
}

type NetboxJsonRequest struct {
	Path   string
	Method string
	Body   any
}

var _ NetboxRequest = (*NetboxJsonRequest)(nil)

func (r *NetboxJsonRequest) BuildRequest(ctx context.Context, host *url.URL) (*http.Request, error) {
	host.Path = r.Path

	body, err := json.Marshal(r.Body)
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequestWithContext(
		ctx,
		r.Method,
		host.String(),
		bytes.NewBuffer(body),
	)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")

	return req, nil
}