netboxapi/client.go

41 lines
976 B
Go
Raw Permalink Normal View History

// 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 {
2023-03-26 15:34:41 +00:00
API client.NetBoxAPI
Token string
URL string
}
// 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{
2023-03-26 15:34:41 +00:00
API: *n,
Token: token,
URL: url,
}
return nb
}