github.com/digitalocean/go-netbox@v0.0.2/netbox/netbox.go (about) 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 16 package netbox 17 18 import ( 19 "fmt" 20 21 runtimeclient "github.com/go-openapi/runtime/client" 22 "github.com/go-openapi/strfmt" 23 24 "github.com/digitalocean/go-netbox/netbox/client" 25 ) 26 27 // NewNetboxAt returns a client which will connect to the given 28 // hostname, which can optionally include a port, e.g. localhost:8000 29 func NewNetboxAt(host string) *client.NetBoxAPI { 30 t := client.DefaultTransportConfig().WithHost(host) 31 return client.NewHTTPClientWithConfig(strfmt.Default, t) 32 } 33 34 const authHeaderName = "Authorization" 35 const authHeaderFormat = "Token %v" 36 37 // NewNetboxWithAPIKey returns a client which will connect to the given 38 // hostname (and optionally port), and will set the expected Authorization 39 // header on each request 40 func NewNetboxWithAPIKey(host string, apiToken string) *client.NetBoxAPI { 41 t := runtimeclient.New(host, client.DefaultBasePath, client.DefaultSchemes) 42 t.DefaultAuthentication = runtimeclient.APIKeyAuth(authHeaderName, "header", fmt.Sprintf(authHeaderFormat, apiToken)) 43 return client.New(t, strfmt.Default) 44 }