blob: 2784872ecc7b26cd5fe3142519f5624948a17f94 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// Copyright 2018 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
15package netbox
16
17import (
18 "fmt"
19
20 "github.com/go-openapi/strfmt"
21 runtimeclient "github.com/go-openapi/runtime/client"
22
23 "github.com/digitalocean/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
28func NewNetboxAt(host string) *client.NetBox {
29 t := client.DefaultTransportConfig().WithHost(host)
30 return client.NewHTTPClientWithConfig(strfmt.Default, t)
31}
32
33const authHeaderName = "Authorization"
34const 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
39func 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}