From cc909b7da3ec433a290ce15cfd3a39e4d8425f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20D=C3=ADaz=20Marco?= Date: Tue, 17 Oct 2023 19:31:21 +0200 Subject: [PATCH] Add client helper. --- go.mod | 1 + go.sum | 2 ++ main.go | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+) 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..30e9c0eaa7 100644 --- a/main.go +++ b/main.go @@ -2,3 +2,29 @@ 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) (*ClientWithResponses, error) { + provider, err := securityprovider.NewSecurityProviderApiKey( + "header", + authHeaderName, + fmt.Sprintf(authHeaderFormat, token), + ) + + if err != nil { + return nil, err + } + + return NewClientWithResponses( + host, + WithRequestEditorFn(provider.Intercept), + ) +}