Skip to content

Commit 2ada75e

Browse files
committed
Fix netbox-community#60: netbox package
1 parent db98ca4 commit 2ada75e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

netbox/netbox.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020 The go-netbox Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package netbox
16+
17+
import (
18+
"fmt"
19+
20+
runtimeclient "github.com/go-openapi/runtime/client"
21+
"github.com/go-openapi/strfmt"
22+
23+
"github.com/netbox-community/go-netbox/netbox/client"
24+
)
25+
26+
// NewNetboxAt returns a client which will connect to the given
27+
// hostname, which can optionally include a port, e.g. localhost:8000
28+
func NewNetboxAt(host string) *client.NetBox {
29+
t := client.DefaultTransportConfig().WithHost(host)
30+
return client.NewHTTPClientWithConfig(strfmt.Default, t)
31+
}
32+
33+
const authHeaderName = "Authorization"
34+
const authHeaderFormat = "Token %v"
35+
36+
// NewNetboxWithAPIKey returna client which will connect to the given
37+
// hostname (and optionally port), and will set the expected Authorization
38+
// header on each request
39+
func NewNetboxWithAPIKey(host string, apiToken string) *client.NetBox {
40+
t := runtimeclient.New(host, client.DefaultBasePath, client.DefaultSchemes)
41+
t.DefaultAuthentication = runtimeclient.APIKeyAuth(authHeaderName, "header", fmt.Sprintf(authHeaderFormat, apiToken))
42+
return client.New(t, strfmt.Default)
43+
}

0 commit comments

Comments
 (0)