forked from netbox-community/go-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
114 lines (95 loc) · 2.9 KB
/
client.go
File metadata and controls
114 lines (95 loc) · 2.9 KB
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
// Copyright 2016 The go-netbox Authors.
//
// 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 netbox
import (
"encoding/json"
"net/http"
"net/url"
"path"
)
// A Client is a NetBox client. It can be used to retrieve network and
// datacenter infrastructure information from a NetBox server.
type Client struct {
// DCIM provides access to methods in NetBox's DCIM API.
DCIM *DCIMService
// IPAM provides access to methods in NetBox's IPAM API.
IPAM *IPAMService
u *url.URL
client *http.Client
}
// NewClient returns a new instance of a NetBox client. addr specifies the address
// of the NetBox server, and client specifies an optional HTTP client to use
// for requests.
//
// If client is nil, a default HTTP client will be used.
func NewClient(addr string, client *http.Client) (*Client, error) {
if client == nil {
client = &http.Client{}
}
u, err := url.Parse(addr)
if err != nil {
return nil, err
}
c := &Client{
u: u,
client: client,
}
c.DCIM = &DCIMService{c: c}
c.IPAM = &IPAMService{c: c}
return c, nil
}
// newRequest creates a HTTP request using the input HTTP method, URL
// endpoint, and a valuer which creates URL parameters for the request.
//
// If a nil valuer is specified, no query parameters will be sent with the
// request.
func (c *Client) newRequest(method string, endpoint string, options valuer) (*http.Request, error) {
rel, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
// Allow specifying a base path for API requests, so if a NetBox server
// resides at a path like http://example.com/netbox/, API requests will
// be sent to http://example.com/netbox/api/...
//
// Enables support of: https://github.com/digitalocean/netbox/issues/212.
if c.u.Path != "" {
rel.Path = path.Join(c.u.Path, rel.Path)
}
u := c.u.ResolveReference(rel)
// If no valuer specified, create a request with no query parameters
if options == nil {
return http.NewRequest(method, u.String(), nil)
}
values, err := options.values()
if err != nil {
return nil, err
}
u.RawQuery = values.Encode()
return http.NewRequest(method, u.String(), nil)
}
// do executes an HTTP request and unmarshals result JSON onto v.
func (c *Client) do(req *http.Request, v interface{}) error {
res, err := c.client.Do(req)
if err != nil {
return err
}
defer func() {
_ = res.Body.Close()
}()
if v == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(v)
}