netboxapi/client.go

41 lines
1.1 KiB
Go

// Package netboxapi provides custom functions for the default
// go-netbox package. It mostly handles pagination.
package netboxapi
import (
"fmt"
transport "github.com/go-openapi/runtime/client"
"github.com/netbox-community/go-netbox/v3/netbox/client"
)
// NetBoxAPI is the type we use to add custom functions
type NetBoxAPI struct {
client.NetBoxAPI // API of NetBox
Token string // Token used to access the NetBox API
URL string // URL of this NetBox instance
}
// NewNetBoxClient returns a client to NetBox at the given url,
// using the provided token
func NewNetBoxClient(token string, url string) NetBoxAPI {
// Construct the API basis and configure authentication
t := transport.New(url, client.DefaultBasePath, []string{"https"})
t.DefaultAuthentication = transport.APIKeyAuth(
"Authorization",
"header",
fmt.Sprintf("Token %v", token),
)
// create NetBox api variable
n := client.New(t, nil)
// create a new variable for our custom functions
nb := NetBoxAPI{
NetBoxAPI: *n,
Token: token,
URL: url,
}
return nb
}