diff --git a/go.mod b/go.mod index 60bc92abd0..53bb3c3de8 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/netbox-community/go-netbox/v3 go 1.21 require ( + github.com/deepmap/oapi-codegen v1.15.0 github.com/oapi-codegen/runtime v1.0.0 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index c509a9b74a..8af448019c 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvF github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deepmap/oapi-codegen v1.15.0 h1:SQqViaeb4k2vMul8gx12oDOIadEtoRqTdLkxjzqtQ90= +github.com/deepmap/oapi-codegen v1.15.0/go.mod h1:a6KoHV7lMRwsPoEg2C6NDHiXYV3EQfiFocOlJ8dgJQE= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= diff --git a/main.go b/main.go index 7336db9636..99f2655d71 100644 --- a/main.go +++ b/main.go @@ -2,3 +2,31 @@ package netbox //go:generate oapi-codegen -package netbox -generate types -o types.go api/openapi.yaml //go:generate oapi-codegen -package netbox -generate client -o client.go api/openapi.yaml + +import ( + "fmt" + + "github.com/deepmap/oapi-codegen/pkg/securityprovider" +) + +const authHeaderName = "Authorization" +const authHeaderFormat = "Token %v" + +func NewClientWithToken(host string, token string, opts ...ClientOption) (*Client, error) { + provider, err := securityprovider.NewSecurityProviderApiKey( + "header", + authHeaderName, + fmt.Sprintf(authHeaderFormat, token), + ) + + if err != nil { + return nil, err + } + + opts = append( + opts, + WithRequestEditorFn(provider.Intercept), + ) + + return NewClient(host, opts...) +}