diff --git a/.speakeasy/gen.lock b/.speakeasy/gen.lock index 4afa83e..b7694bd 100755 --- a/.speakeasy/gen.lock +++ b/.speakeasy/gen.lock @@ -1,20 +1,22 @@ lockVersion: 2.0.0 id: 4fc4737a-135f-4840-8e6d-42a2b2a2884b management: - docChecksum: 0785f5659b7521373ececdc3425c7efc + docChecksum: 1adbad72708fe75ac7626a1e7e1dfc13 docVersion: 1.0.0 - speakeasyVersion: 1.330.0 - generationVersion: 2.361.10 - releaseVersion: 0.6.1 - configChecksum: b411f7847c3a11ea34842f8b693e6fd0 + speakeasyVersion: 1.335.2 + generationVersion: 2.372.3 + releaseVersion: 0.7.0 + configChecksum: c299315b1cde1ca427dc047689fa60ff repoURL: https://github.com/unkeyed/unkey-go.git installationURL: https://github.com/unkeyed/unkey-go features: go: additionalDependencies: 0.1.0 constsAndDefaults: 0.1.4 - core: 3.4.14 + core: 3.4.15 + defaultEnabledRetries: 0.2.0 deprecations: 2.81.1 + envVarSecurityUsage: 0.2.0 globalSecurity: 2.82.9 globalSecurityCallbacks: 0.1.0 globalSecurityFlattening: 0.1.0 @@ -62,6 +64,12 @@ generatedFiles: - /models/operations/updatekey.go - /models/operations/updateremaining.go - /models/operations/getverifications.go + - /models/operations/addpermissions.go + - /models/operations/removepermissions.go + - /models/operations/setpermissions.go + - /models/operations/addroles.go + - /models/operations/removeroles.go + - /models/operations/setroles.go - /models/operations/getapi.go - /models/operations/listkeys.go - /models/operations/deletekeys.go @@ -115,6 +123,8 @@ generatedFiles: - docs/models/operations/updatekeyratelimit.md - docs/models/operations/updatekeyinterval.md - docs/models/operations/updatekeyrefill.md + - docs/models/operations/roles.md + - docs/models/operations/permissions.md - docs/models/operations/updatekeyrequestbody.md - docs/models/operations/updatekeyresponsebody.md - docs/models/operations/updatekeyresponse.md @@ -127,6 +137,30 @@ generatedFiles: - docs/models/operations/verifications.md - docs/models/operations/getverificationsresponsebody.md - docs/models/operations/getverificationsresponse.md + - docs/models/operations/addpermissionspermissions.md + - docs/models/operations/addpermissionsrequestbody.md + - docs/models/operations/responsebody.md + - docs/models/operations/addpermissionsresponse.md + - docs/models/operations/removepermissionspermissions.md + - docs/models/operations/removepermissionsrequestbody.md + - docs/models/operations/removepermissionsresponsebody.md + - docs/models/operations/removepermissionsresponse.md + - docs/models/operations/setpermissionspermissions.md + - docs/models/operations/setpermissionsrequestbody.md + - docs/models/operations/setpermissionsresponsebody.md + - docs/models/operations/setpermissionsresponse.md + - docs/models/operations/addrolesroles.md + - docs/models/operations/addrolesrequestbody.md + - docs/models/operations/addrolesresponsebody.md + - docs/models/operations/addrolesresponse.md + - docs/models/operations/removerolesroles.md + - docs/models/operations/removerolesrequestbody.md + - docs/models/operations/removerolesresponsebody.md + - docs/models/operations/removerolesresponse.md + - docs/models/operations/setrolesroles.md + - docs/models/operations/setrolesrequestbody.md + - docs/models/operations/setrolesresponsebody.md + - docs/models/operations/setrolesresponse.md - docs/models/operations/getapirequest.md - docs/models/operations/getapiresponsebody.md - docs/models/operations/getapiresponse.md @@ -168,7 +202,7 @@ generatedFiles: - docs/models/operations/getpermissionrequest.md - docs/models/operations/getpermissionresponsebody.md - docs/models/operations/getpermissionresponse.md - - docs/models/operations/responsebody.md + - docs/models/operations/listpermissionsresponsebody.md - docs/models/operations/listpermissionsresponse.md - docs/models/operations/createrolerequestbody.md - docs/models/operations/createroleresponsebody.md diff --git a/.speakeasy/gen.yaml b/.speakeasy/gen.yaml index f4a67ef..282129f 100755 --- a/.speakeasy/gen.yaml +++ b/.speakeasy/gen.yaml @@ -12,7 +12,7 @@ generation: auth: oAuth2ClientCredentialsEnabled: true go: - version: 0.6.1 + version: 0.7.0 additionalDependencies: {} allowUnknownFieldsInWeakUnions: false clientServerStatusCodesAsErrors: true diff --git a/.speakeasy/workflow.lock b/.speakeasy/workflow.lock index 0645072..a5fa9c4 100644 --- a/.speakeasy/workflow.lock +++ b/.speakeasy/workflow.lock @@ -1,9 +1,9 @@ -speakeasyVersion: 1.330.0 +speakeasyVersion: 1.335.2 sources: openapi.json: sourceNamespace: openapi-json - sourceRevisionDigest: sha256:5c12bae1c724729db89b905ce8cd7d30451db13721149eeb41b5538b0b93c1c7 - sourceBlobDigest: sha256:24b012a381daa90eac68b5fc3ece8c6ec87883249492f438d669096c27465776 + sourceRevisionDigest: sha256:5754c6125ef1a2d0690dd2ba633bc841608de7594320804124517ded211f4f1a + sourceBlobDigest: sha256:f0439adffd2a8e451bd3b624609ededd5e1b0eb06fc998eafa5e2ee5df6613d4 tags: - latest - main @@ -11,8 +11,8 @@ targets: go: source: openapi.json sourceNamespace: openapi-json - sourceRevisionDigest: sha256:5c12bae1c724729db89b905ce8cd7d30451db13721149eeb41b5538b0b93c1c7 - sourceBlobDigest: sha256:24b012a381daa90eac68b5fc3ece8c6ec87883249492f438d669096c27465776 + sourceRevisionDigest: sha256:5754c6125ef1a2d0690dd2ba633bc841608de7594320804124517ded211f4f1a + sourceBlobDigest: sha256:f0439adffd2a8e451bd3b624609ededd5e1b0eb06fc998eafa5e2ee5df6613d4 outLocation: /github/workspace/repo workflow: workflowVersion: 1.0.0 diff --git a/README.md b/README.md index 3d690c9..39a2df9 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,12 @@ import ( unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "log" + "os" ) func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -69,6 +70,12 @@ func main() { * [UpdateKey](docs/sdks/keys/README.md#updatekey) * [UpdateRemaining](docs/sdks/keys/README.md#updateremaining) * [GetVerifications](docs/sdks/keys/README.md#getverifications) +* [AddPermissions](docs/sdks/keys/README.md#addpermissions) +* [RemovePermissions](docs/sdks/keys/README.md#removepermissions) +* [SetPermissions](docs/sdks/keys/README.md#setpermissions) +* [AddRoles](docs/sdks/keys/README.md#addroles) +* [RemoveRoles](docs/sdks/keys/README.md#removeroles) +* [SetRoles](docs/sdks/keys/README.md#setroles) ### [Apis](docs/sdks/apis/README.md) @@ -125,11 +132,12 @@ import ( "github.com/unkeyed/unkey-go/models/operations" "github.com/unkeyed/unkey-go/models/sdkerrors" "log" + "os" ) func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -212,12 +220,13 @@ import ( unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "log" + "os" ) func main() { s := unkeygo.New( unkeygo.WithServerIndex(0), - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -246,12 +255,13 @@ import ( unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "log" + "os" ) func main() { s := unkeygo.New( unkeygo.WithServerURL("https://api.unkey.dev"), - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -318,11 +328,12 @@ import ( unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "log" + "os" ) func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -362,11 +373,12 @@ import ( "github.com/unkeyed/unkey-go/retry" "log" "models/operations" + "os" ) func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -403,6 +415,7 @@ import ( "github.com/unkeyed/unkey-go/models/operations" "github.com/unkeyed/unkey-go/retry" "log" + "os" ) func main() { @@ -418,7 +431,7 @@ func main() { }, RetryConnectionErrors: false, }), - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", diff --git a/RELEASES.md b/RELEASES.md index c9b7a19..0b30be9 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -118,4 +118,14 @@ Based on: ### Generated - [go v0.6.1] . ### Releases -- [Go v0.6.1] https://github.com/unkeyed/unkey-go/releases/tag/v0.6.1 - . \ No newline at end of file +- [Go v0.6.1] https://github.com/unkeyed/unkey-go/releases/tag/v0.6.1 - . + +## 2024-07-15 18:42:59 +### Changes +Based on: +- OpenAPI Doc +- Speakeasy CLI 1.335.2 (2.372.3) https://github.com/speakeasy-api/speakeasy +### Generated +- [go v0.7.0] . +### Releases +- [Go v0.7.0] https://github.com/unkeyed/unkey-go/releases/tag/v0.7.0 - . \ No newline at end of file diff --git a/USAGE.md b/USAGE.md index cce2c5d..e3be90d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -7,11 +7,12 @@ import ( unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "log" + "os" ) func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", diff --git a/apis.go b/apis.go index 561208b..930daf4 100644 --- a/apis.go +++ b/apis.go @@ -39,6 +39,7 @@ func (s *Apis) GetAPI(ctx context.Context, request operations.GetAPIRequest, opt o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,12 +47,24 @@ func (s *Apis) GetAPI(ctx context.Context, request operations.GetAPIRequest, opt return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/apis.getApi") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -70,10 +83,11 @@ func (s *Apis) GetAPI(ctx context.Context, request operations.GetAPIRequest, opt globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -81,31 +95,58 @@ func (s *Apis) GetAPI(ctx context.Context, request operations.GetAPIRequest, opt }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -114,15 +155,19 @@ func (s *Apis) GetAPI(ctx context.Context, request operations.GetAPIRequest, opt } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -260,6 +305,7 @@ func (s *Apis) ListKeys(ctx context.Context, request operations.ListKeysRequest, o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -267,12 +313,24 @@ func (s *Apis) ListKeys(ctx context.Context, request operations.ListKeysRequest, return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/apis.listKeys") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -291,10 +349,11 @@ func (s *Apis) ListKeys(ctx context.Context, request operations.ListKeysRequest, globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -302,31 +361,58 @@ func (s *Apis) ListKeys(ctx context.Context, request operations.ListKeysRequest, }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -335,15 +421,19 @@ func (s *Apis) ListKeys(ctx context.Context, request operations.ListKeysRequest, } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -481,6 +571,7 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -488,6 +579,7 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/apis.deleteKeys") if err != nil { @@ -499,6 +591,17 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -514,10 +617,11 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -525,31 +629,58 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -558,15 +689,19 @@ func (s *Apis) DeleteKeys(ctx context.Context, request operations.DeleteKeysRequ } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } diff --git a/docs/models/operations/addpermissionspermissions.md b/docs/models/operations/addpermissionspermissions.md new file mode 100644 index 0000000..a336f28 --- /dev/null +++ b/docs/models/operations/addpermissionspermissions.md @@ -0,0 +1,10 @@ +# AddPermissionsPermissions + + +## Fields + +| Field | Type | Required | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/addpermissionsrequestbody.md b/docs/models/operations/addpermissionsrequestbody.md new file mode 100644 index 0000000..421b6ea --- /dev/null +++ b/docs/models/operations/addpermissionsrequestbody.md @@ -0,0 +1,9 @@ +# AddPermissionsRequestBody + + +## Fields + +| Field | Type | Required | Description | +| ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | +| `Permissions` | [][operations.AddPermissionsPermissions](../../models/operations/addpermissionspermissions.md) | :heavy_check_mark: | The permissions you want to add to this key | \ No newline at end of file diff --git a/docs/models/operations/addpermissionsresponse.md b/docs/models/operations/addpermissionsresponse.md new file mode 100644 index 0000000..9e4f0be --- /dev/null +++ b/docs/models/operations/addpermissionsresponse.md @@ -0,0 +1,9 @@ +# AddPermissionsResponse + + +## Fields + +| Field | Type | Required | Description | +| -------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------- | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `ResponseBodies` | [][operations.ResponseBody](../../models/operations/responsebody.md) | :heavy_minus_sign: | All currently connected permissions | \ No newline at end of file diff --git a/docs/models/operations/addrolesrequestbody.md b/docs/models/operations/addrolesrequestbody.md new file mode 100644 index 0000000..8e54a45 --- /dev/null +++ b/docs/models/operations/addrolesrequestbody.md @@ -0,0 +1,9 @@ +# AddRolesRequestBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | | +| `Roles` | [][operations.AddRolesRoles](../../models/operations/addrolesroles.md) | :heavy_check_mark: | The roles you want to set for this key. This overwrites all existing roles.
Setting roles requires the `rbac.*.add_role_to_key` permission. | [
{
"id": "role_123"
},
{
"name": "dns.record.create"
},
{
"name": "dns.record.delete",
"create": true
}
] | \ No newline at end of file diff --git a/docs/models/operations/addrolesresponse.md b/docs/models/operations/addrolesresponse.md new file mode 100644 index 0000000..1657ce5 --- /dev/null +++ b/docs/models/operations/addrolesresponse.md @@ -0,0 +1,9 @@ +# AddRolesResponse + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `ResponseBodies` | [][operations.AddRolesResponseBody](../../models/operations/addrolesresponsebody.md) | :heavy_minus_sign: | All currently connected roles | \ No newline at end of file diff --git a/docs/models/operations/addrolesresponsebody.md b/docs/models/operations/addrolesresponsebody.md new file mode 100644 index 0000000..06d53d6 --- /dev/null +++ b/docs/models/operations/addrolesresponsebody.md @@ -0,0 +1,9 @@ +# AddRolesResponseBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | +| `ID` | *string* | :heavy_check_mark: | The id of the role. This is used internally | role_123 | +| `Name` | *string* | :heavy_check_mark: | The name of the role | dns.record.create | \ No newline at end of file diff --git a/docs/models/operations/addrolesroles.md b/docs/models/operations/addrolesroles.md new file mode 100644 index 0000000..4d2c30b --- /dev/null +++ b/docs/models/operations/addrolesroles.md @@ -0,0 +1,10 @@ +# AddRolesRoles + + +## Fields + +| Field | Type | Required | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the role. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/listpermissionsresponse.md b/docs/models/operations/listpermissionsresponse.md index 03f280b..a39dc6a 100644 --- a/docs/models/operations/listpermissionsresponse.md +++ b/docs/models/operations/listpermissionsresponse.md @@ -3,7 +3,7 @@ ## Fields -| Field | Type | Required | Description | -| -------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------- | -| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | -| `ResponseBodies` | [][operations.ResponseBody](../../models/operations/responsebody.md) | :heavy_minus_sign: | The permissions in your workspace | \ No newline at end of file +| Field | Type | Required | Description | +| -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `ResponseBodies` | [][operations.ListPermissionsResponseBody](../../models/operations/listpermissionsresponsebody.md) | :heavy_minus_sign: | The permissions in your workspace | \ No newline at end of file diff --git a/docs/models/operations/listpermissionsresponsebody.md b/docs/models/operations/listpermissionsresponsebody.md new file mode 100644 index 0000000..c2c1e0d --- /dev/null +++ b/docs/models/operations/listpermissionsresponsebody.md @@ -0,0 +1,10 @@ +# ListPermissionsResponseBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | +| `ID` | *string* | :heavy_check_mark: | The id of the permission | perm_123 | +| `Name` | *string* | :heavy_check_mark: | The name of the permission. | domain.record.manager | +| `Description` | **string* | :heavy_minus_sign: | The description of what this permission does. This is just for your team, your users will not see this. | Can manage dns records | \ No newline at end of file diff --git a/docs/models/operations/permissions.md b/docs/models/operations/permissions.md new file mode 100644 index 0000000..90b8d2f --- /dev/null +++ b/docs/models/operations/permissions.md @@ -0,0 +1,10 @@ +# Permissions + + +## Fields + +| Field | Type | Required | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/removepermissionspermissions.md b/docs/models/operations/removepermissionspermissions.md new file mode 100644 index 0000000..02ff4a0 --- /dev/null +++ b/docs/models/operations/removepermissionspermissions.md @@ -0,0 +1,9 @@ +# RemovePermissionsPermissions + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. | \ No newline at end of file diff --git a/docs/models/operations/removepermissionsrequestbody.md b/docs/models/operations/removepermissionsrequestbody.md new file mode 100644 index 0000000..3867d03 --- /dev/null +++ b/docs/models/operations/removepermissionsrequestbody.md @@ -0,0 +1,9 @@ +# RemovePermissionsRequestBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | | +| `Permissions` | [][operations.RemovePermissionsPermissions](../../models/operations/removepermissionspermissions.md) | :heavy_check_mark: | The permissions you want to remove from this key. | [
{
"id": "perm_123"
},
{
"name": "dns.record.create"
}
] | \ No newline at end of file diff --git a/docs/models/operations/removepermissionsresponse.md b/docs/models/operations/removepermissionsresponse.md new file mode 100644 index 0000000..a2c5096 --- /dev/null +++ b/docs/models/operations/removepermissionsresponse.md @@ -0,0 +1,9 @@ +# RemovePermissionsResponse + + +## Fields + +| Field | Type | Required | Description | +| ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `Object` | [*operations.RemovePermissionsResponseBody](../../models/operations/removepermissionsresponsebody.md) | :heavy_minus_sign: | Success | \ No newline at end of file diff --git a/docs/models/operations/removepermissionsresponsebody.md b/docs/models/operations/removepermissionsresponsebody.md new file mode 100644 index 0000000..d09bb43 --- /dev/null +++ b/docs/models/operations/removepermissionsresponsebody.md @@ -0,0 +1,9 @@ +# RemovePermissionsResponseBody + +Success + + +## Fields + +| Field | Type | Required | Description | +| ----------- | ----------- | ----------- | ----------- | \ No newline at end of file diff --git a/docs/models/operations/removerolesrequestbody.md b/docs/models/operations/removerolesrequestbody.md new file mode 100644 index 0000000..3ad3f19 --- /dev/null +++ b/docs/models/operations/removerolesrequestbody.md @@ -0,0 +1,9 @@ +# RemoveRolesRequestBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | | +| `Roles` | [][operations.RemoveRolesRoles](../../models/operations/removerolesroles.md) | :heavy_check_mark: | The roles you want to remove from this key. | [
{
"id": "role_123"
},
{
"name": "dns.record.create"
}
] | \ No newline at end of file diff --git a/docs/models/operations/removerolesresponse.md b/docs/models/operations/removerolesresponse.md new file mode 100644 index 0000000..818e2c2 --- /dev/null +++ b/docs/models/operations/removerolesresponse.md @@ -0,0 +1,9 @@ +# RemoveRolesResponse + + +## Fields + +| Field | Type | Required | Description | +| ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `Object` | [*operations.RemoveRolesResponseBody](../../models/operations/removerolesresponsebody.md) | :heavy_minus_sign: | Success | \ No newline at end of file diff --git a/docs/models/operations/removerolesresponsebody.md b/docs/models/operations/removerolesresponsebody.md new file mode 100644 index 0000000..436775e --- /dev/null +++ b/docs/models/operations/removerolesresponsebody.md @@ -0,0 +1,9 @@ +# RemoveRolesResponseBody + +Success + + +## Fields + +| Field | Type | Required | Description | +| ----------- | ----------- | ----------- | ----------- | \ No newline at end of file diff --git a/docs/models/operations/removerolesroles.md b/docs/models/operations/removerolesroles.md new file mode 100644 index 0000000..093b3f5 --- /dev/null +++ b/docs/models/operations/removerolesroles.md @@ -0,0 +1,9 @@ +# RemoveRolesRoles + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the role. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. | \ No newline at end of file diff --git a/docs/models/operations/responsebody.md b/docs/models/operations/responsebody.md index e876aff..1d0f430 100644 --- a/docs/models/operations/responsebody.md +++ b/docs/models/operations/responsebody.md @@ -3,8 +3,7 @@ ## Fields -| Field | Type | Required | Description | Example | -| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | -| `ID` | *string* | :heavy_check_mark: | The id of the permission | perm_123 | -| `Name` | *string* | :heavy_check_mark: | The name of the permission. | domain.record.manager | -| `Description` | **string* | :heavy_minus_sign: | The description of what this permission does. This is just for your team, your users will not see this. | Can manage dns records | \ No newline at end of file +| Field | Type | Required | Description | Example | +| ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | +| `ID` | *string* | :heavy_check_mark: | The id of the permission. This is used internally | perm_123 | +| `Name` | *string* | :heavy_check_mark: | The name of the permission | dns.record.create | \ No newline at end of file diff --git a/docs/models/operations/roles.md b/docs/models/operations/roles.md new file mode 100644 index 0000000..fbd1bdd --- /dev/null +++ b/docs/models/operations/roles.md @@ -0,0 +1,10 @@ +# Roles + + +## Fields + +| Field | Type | Required | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ID` | **string* | :heavy_minus_sign: | The id of the role. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/setpermissionspermissions.md b/docs/models/operations/setpermissionspermissions.md new file mode 100644 index 0000000..418e719 --- /dev/null +++ b/docs/models/operations/setpermissionspermissions.md @@ -0,0 +1,10 @@ +# SetPermissionsPermissions + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `ID` | **string* | :heavy_minus_sign: | The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/setpermissionsrequestbody.md b/docs/models/operations/setpermissionsrequestbody.md new file mode 100644 index 0000000..88b9d4c --- /dev/null +++ b/docs/models/operations/setpermissionsrequestbody.md @@ -0,0 +1,9 @@ +# SetPermissionsRequestBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | | +| `Permissions` | [][operations.SetPermissionsPermissions](../../models/operations/setpermissionspermissions.md) | :heavy_check_mark: | The permissions you want to set for this key. This overwrites all existing permissions.
Setting permissions requires the `rbac.*.add_permission_to_key` permission. | [
{
"id": "perm_123"
},
{
"name": "dns.record.create"
},
{
"name": "dns.record.delete",
"create": true
}
] | \ No newline at end of file diff --git a/docs/models/operations/setpermissionsresponse.md b/docs/models/operations/setpermissionsresponse.md new file mode 100644 index 0000000..af31651 --- /dev/null +++ b/docs/models/operations/setpermissionsresponse.md @@ -0,0 +1,9 @@ +# SetPermissionsResponse + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `ResponseBodies` | [][operations.SetPermissionsResponseBody](../../models/operations/setpermissionsresponsebody.md) | :heavy_minus_sign: | All currently connected permissions | \ No newline at end of file diff --git a/docs/models/operations/setpermissionsresponsebody.md b/docs/models/operations/setpermissionsresponsebody.md new file mode 100644 index 0000000..d1a94b2 --- /dev/null +++ b/docs/models/operations/setpermissionsresponsebody.md @@ -0,0 +1,9 @@ +# SetPermissionsResponseBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | +| `ID` | *string* | :heavy_check_mark: | The id of the permission. This is used internally | perm_123 | +| `Name` | *string* | :heavy_check_mark: | The name of the permission | dns.record.create | \ No newline at end of file diff --git a/docs/models/operations/setrolesrequestbody.md b/docs/models/operations/setrolesrequestbody.md new file mode 100644 index 0000000..d90227a --- /dev/null +++ b/docs/models/operations/setrolesrequestbody.md @@ -0,0 +1,9 @@ +# SetRolesRequestBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `KeyID` | *string* | :heavy_check_mark: | The id of the key. | | +| `Roles` | [][operations.SetRolesRoles](../../models/operations/setrolesroles.md) | :heavy_check_mark: | The roles you want to set for this key. This overwrites all existing roles.
Setting roles requires the `rbac.*.add_role_to_key` permission. | [
{
"id": "role_123"
},
{
"name": "dns.record.create"
},
{
"name": "dns.record.delete",
"create": true
}
] | \ No newline at end of file diff --git a/docs/models/operations/setrolesresponse.md b/docs/models/operations/setrolesresponse.md new file mode 100644 index 0000000..1efcfac --- /dev/null +++ b/docs/models/operations/setrolesresponse.md @@ -0,0 +1,9 @@ +# SetRolesResponse + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | +| `HTTPMeta` | [components.HTTPMetadata](../../models/components/httpmetadata.md) | :heavy_check_mark: | N/A | +| `ResponseBodies` | [][operations.SetRolesResponseBody](../../models/operations/setrolesresponsebody.md) | :heavy_minus_sign: | All currently connected roles | \ No newline at end of file diff --git a/docs/models/operations/setrolesresponsebody.md b/docs/models/operations/setrolesresponsebody.md new file mode 100644 index 0000000..11a730a --- /dev/null +++ b/docs/models/operations/setrolesresponsebody.md @@ -0,0 +1,9 @@ +# SetRolesResponseBody + + +## Fields + +| Field | Type | Required | Description | Example | +| ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | ------------------------------------------- | +| `ID` | *string* | :heavy_check_mark: | The id of the role. This is used internally | role_123 | +| `Name` | *string* | :heavy_check_mark: | The name of the role | dns.record.create | \ No newline at end of file diff --git a/docs/models/operations/setrolesroles.md b/docs/models/operations/setrolesroles.md new file mode 100644 index 0000000..a6287f0 --- /dev/null +++ b/docs/models/operations/setrolesroles.md @@ -0,0 +1,10 @@ +# SetRolesRoles + + +## Fields + +| Field | Type | Required | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `ID` | **string* | :heavy_minus_sign: | The id of the role. Provide either `id` or `name`. If both are provided `id` is used. | +| `Name` | **string* | :heavy_minus_sign: | Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. | +| `Create` | **bool* | :heavy_minus_sign: | Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`.
Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected | \ No newline at end of file diff --git a/docs/models/operations/updatekeyrequestbody.md b/docs/models/operations/updatekeyrequestbody.md index 6eb18e4..c473db8 100644 --- a/docs/models/operations/updatekeyrequestbody.md +++ b/docs/models/operations/updatekeyrequestbody.md @@ -1,5 +1,8 @@ # UpdateKeyRequestBody +Update a key's configuration. + The `apis..update_key` permission is required. + ## Fields @@ -13,4 +16,6 @@ | `Ratelimit` | [*operations.UpdateKeyRatelimit](../../models/operations/updatekeyratelimit.md) | :heavy_minus_sign: | Unkey comes with per-key ratelimiting out of the box. Set `null` to disable. | {
"type": "fast",
"limit": 10,
"refillRate": 1,
"refillInterval": 60
} | | `Remaining` | **float64* | :heavy_minus_sign: | The number of requests that can be made with this key before it becomes invalid. Set `null` to disable. | 1000 | | `Refill` | [*operations.UpdateKeyRefill](../../models/operations/updatekeyrefill.md) | :heavy_minus_sign: | Unkey enables you to refill verifications for each key at regular intervals. | {
"interval": "daily",
"amount": 100
} | -| `Enabled` | **bool* | :heavy_minus_sign: | Set if key is enabled or disabled. If disabled, the key cannot be used to verify. | true | \ No newline at end of file +| `Enabled` | **bool* | :heavy_minus_sign: | Set if key is enabled or disabled. If disabled, the key cannot be used to verify. | true | +| `Roles` | [][operations.Roles](../../models/operations/roles.md) | :heavy_minus_sign: | The roles you want to set for this key. This overwrites all existing roles.
Setting roles requires the `rbac.*.add_role_to_key` permission. | [
{
"id": "perm_123"
},
{
"name": "dns.record.create"
},
{
"name": "dns.record.delete",
"create": true
}
] | +| `Permissions` | [][operations.Permissions](../../models/operations/permissions.md) | :heavy_minus_sign: | The permissions you want to set for this key. This overwrites all existing permissions.
Setting permissions requires the `rbac.*.add_permission_to_key` permission. | [
{
"id": "perm_123"
},
{
"name": "dns.record.create"
},
{
"name": "dns.record.delete",
"create": true
}
] | \ No newline at end of file diff --git a/docs/sdks/apis/README.md b/docs/sdks/apis/README.md index aad36de..af0b205 100644 --- a/docs/sdks/apis/README.md +++ b/docs/sdks/apis/README.md @@ -15,6 +15,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -23,7 +24,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.GetAPIRequest{ APIID: "api_1234", @@ -70,6 +71,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -78,7 +80,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.ListKeysRequest{ APIID: "api_1234", @@ -126,6 +128,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -134,7 +137,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.DeleteKeysRequestBody{ APIID: "api_1234", diff --git a/docs/sdks/keys/README.md b/docs/sdks/keys/README.md index ee520d9..1dfe43c 100644 --- a/docs/sdks/keys/README.md +++ b/docs/sdks/keys/README.md @@ -10,6 +10,12 @@ * [UpdateKey](#updatekey) * [UpdateRemaining](#updateremaining) * [GetVerifications](#getverifications) +* [AddPermissions](#addpermissions) +* [RemovePermissions](#removepermissions) +* [SetPermissions](#setpermissions) +* [AddRoles](#addroles) +* [RemoveRoles](#removeroles) +* [SetRoles](#setroles) ## GetKey @@ -19,6 +25,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -27,7 +34,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.GetKeyRequest{ KeyID: "key_1234", @@ -74,6 +81,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -82,7 +90,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.DeleteKeyRequestBody{ KeyID: "key_1234", @@ -129,6 +137,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -137,7 +146,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateKeyRequestBody{ APIID: "api_123", @@ -209,6 +218,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/components" "context" @@ -217,7 +227,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := components.V1KeysVerifyKeyRequest{ APIID: unkeygo.String("api_1234"), @@ -270,6 +280,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -278,7 +289,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.UpdateKeyRequestBody{ KeyID: "key_123", @@ -303,6 +314,30 @@ func main() { Amount: 100, }, Enabled: unkeygo.Bool(true), + Roles: []operations.Roles{ + operations.Roles{ + ID: unkeygo.String("perm_123"), + }, + operations.Roles{ + Name: unkeygo.String("dns.record.create"), + }, + operations.Roles{ + Name: unkeygo.String("dns.record.delete"), + Create: unkeygo.Bool(true), + }, + }, + Permissions: []operations.Permissions{ + operations.Permissions{ + ID: unkeygo.String("perm_123"), + }, + operations.Permissions{ + Name: unkeygo.String("dns.record.create"), + }, + operations.Permissions{ + Name: unkeygo.String("dns.record.delete"), + Create: unkeygo.Bool(true), + }, + }, } ctx := context.Background() res, err := s.Keys.UpdateKey(ctx, request) @@ -346,6 +381,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -354,7 +390,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.UpdateRemainingRequestBody{ KeyID: "key_123", @@ -403,6 +439,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -411,7 +448,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.GetVerificationsRequest{ KeyID: unkeygo.String("key_1234"), @@ -453,3 +490,394 @@ func main() { | sdkerrors.ErrTooManyRequests | 429 | application/json | | sdkerrors.ErrInternalServerError | 500 | application/json | | sdkerrors.SDKError | 4xx-5xx | */* | + +## AddPermissions + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.AddPermissionsRequestBody{ + KeyID: "", + Permissions: []operations.AddPermissionsPermissions{ + operations.AddPermissionsPermissions{}, + }, + } + ctx := context.Background() + res, err := s.Keys.AddPermissions(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.ResponseBodies != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.AddPermissionsRequestBody](../../models/operations/addpermissionsrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.AddPermissionsResponse](../../models/operations/addpermissionsresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | + +## RemovePermissions + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.RemovePermissionsRequestBody{ + KeyID: "", + Permissions: []operations.RemovePermissionsPermissions{ + operations.RemovePermissionsPermissions{ + ID: unkeygo.String("perm_123"), + }, + operations.RemovePermissionsPermissions{ + Name: unkeygo.String("dns.record.create"), + }, + }, + } + ctx := context.Background() + res, err := s.Keys.RemovePermissions(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.Object != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.RemovePermissionsRequestBody](../../models/operations/removepermissionsrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.RemovePermissionsResponse](../../models/operations/removepermissionsresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | + +## SetPermissions + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.SetPermissionsRequestBody{ + KeyID: "", + Permissions: []operations.SetPermissionsPermissions{ + operations.SetPermissionsPermissions{ + ID: unkeygo.String("perm_123"), + }, + operations.SetPermissionsPermissions{ + Name: unkeygo.String("dns.record.create"), + }, + operations.SetPermissionsPermissions{ + Name: unkeygo.String("dns.record.delete"), + Create: unkeygo.Bool(true), + }, + }, + } + ctx := context.Background() + res, err := s.Keys.SetPermissions(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.ResponseBodies != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.SetPermissionsRequestBody](../../models/operations/setpermissionsrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.SetPermissionsResponse](../../models/operations/setpermissionsresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | + +## AddRoles + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.AddRolesRequestBody{ + KeyID: "", + Roles: []operations.AddRolesRoles{ + operations.AddRolesRoles{ + ID: unkeygo.String("role_123"), + }, + operations.AddRolesRoles{ + Name: unkeygo.String("dns.record.create"), + }, + operations.AddRolesRoles{ + Name: unkeygo.String("dns.record.delete"), + Create: unkeygo.Bool(true), + }, + }, + } + ctx := context.Background() + res, err := s.Keys.AddRoles(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.ResponseBodies != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.AddRolesRequestBody](../../models/operations/addrolesrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.AddRolesResponse](../../models/operations/addrolesresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | + +## RemoveRoles + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.RemoveRolesRequestBody{ + KeyID: "", + Roles: []operations.RemoveRolesRoles{ + operations.RemoveRolesRoles{ + ID: unkeygo.String("role_123"), + }, + operations.RemoveRolesRoles{ + Name: unkeygo.String("dns.record.create"), + }, + }, + } + ctx := context.Background() + res, err := s.Keys.RemoveRoles(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.Object != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.RemoveRolesRequestBody](../../models/operations/removerolesrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.RemoveRolesResponse](../../models/operations/removerolesresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | + +## SetRoles + +### Example Usage + +```go +package main + +import( + "os" + unkeygo "github.com/unkeyed/unkey-go" + "github.com/unkeyed/unkey-go/models/operations" + "context" + "log" +) + +func main() { + s := unkeygo.New( + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), + ) + request := operations.SetRolesRequestBody{ + KeyID: "", + Roles: []operations.SetRolesRoles{ + operations.SetRolesRoles{ + ID: unkeygo.String("role_123"), + }, + operations.SetRolesRoles{ + Name: unkeygo.String("dns.record.create"), + }, + operations.SetRolesRoles{ + Name: unkeygo.String("dns.record.delete"), + Create: unkeygo.Bool(true), + }, + }, + } + ctx := context.Background() + res, err := s.Keys.SetRoles(ctx, request) + if err != nil { + log.Fatal(err) + } + if res.ResponseBodies != nil { + // handle response + } +} +``` + +### Parameters + +| Parameter | Type | Required | Description | +| -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | +| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | :heavy_check_mark: | The context to use for the request. | +| `request` | [operations.SetRolesRequestBody](../../models/operations/setrolesrequestbody.md) | :heavy_check_mark: | The request object to use for the request. | +| `opts` | [][operations.Option](../../models/operations/option.md) | :heavy_minus_sign: | The options for this request. | + + +### Response + +**[*operations.SetRolesResponse](../../models/operations/setrolesresponse.md), error** +| Error Object | Status Code | Content Type | +| -------------------------------- | -------------------------------- | -------------------------------- | +| sdkerrors.ErrBadRequest | 400 | application/json | +| sdkerrors.ErrUnauthorized | 401 | application/json | +| sdkerrors.ErrForbidden | 403 | application/json | +| sdkerrors.ErrNotFound | 404 | application/json | +| sdkerrors.ErrConflict | 409 | application/json | +| sdkerrors.ErrTooManyRequests | 429 | application/json | +| sdkerrors.ErrInternalServerError | 500 | application/json | +| sdkerrors.SDKError | 4xx-5xx | */* | diff --git a/docs/sdks/liveness/README.md b/docs/sdks/liveness/README.md index 21a4741..5ae295b 100644 --- a/docs/sdks/liveness/README.md +++ b/docs/sdks/liveness/README.md @@ -13,6 +13,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "context" "log" @@ -20,7 +21,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) ctx := context.Background() diff --git a/docs/sdks/migrations/README.md b/docs/sdks/migrations/README.md index 0bfccbc..a9e1ac2 100644 --- a/docs/sdks/migrations/README.md +++ b/docs/sdks/migrations/README.md @@ -14,6 +14,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -22,7 +23,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := []operations.RequestBody{ operations.RequestBody{ @@ -98,6 +99,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -106,7 +108,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.V1MigrationsEnqueueKeysRequestBody{ MigrationID: "", diff --git a/docs/sdks/permissions/README.md b/docs/sdks/permissions/README.md index 4815b22..72c9bde 100644 --- a/docs/sdks/permissions/README.md +++ b/docs/sdks/permissions/README.md @@ -20,6 +20,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -28,7 +29,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreatePermissionRequestBody{ Name: "record.write", @@ -76,6 +77,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -84,7 +86,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.DeletePermissionRequestBody{ PermissionID: "perm_123", @@ -131,6 +133,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -139,7 +142,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.GetPermissionRequest{ PermissionID: "perm_123", @@ -186,6 +189,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "context" "log" @@ -193,7 +197,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) ctx := context.Background() @@ -237,6 +241,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -245,7 +250,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateRoleRequestBody{ Name: "dns.records.manager", @@ -293,6 +298,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -301,7 +307,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.DeleteRoleRequestBody{ RoleID: "role_123", @@ -348,6 +354,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -356,7 +363,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.GetRoleRequest{ RoleID: "role_123", @@ -403,6 +410,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "context" "log" @@ -410,7 +418,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) ctx := context.Background() diff --git a/docs/sdks/ratelimits/README.md b/docs/sdks/ratelimits/README.md index 2b91bea..2fbfc0d 100644 --- a/docs/sdks/ratelimits/README.md +++ b/docs/sdks/ratelimits/README.md @@ -13,6 +13,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -21,7 +22,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.LimitRequestBody{ Namespace: unkeygo.String("email.outbound"), diff --git a/docs/sdks/unkey/README.md b/docs/sdks/unkey/README.md index 14e6c88..f64aa7c 100644 --- a/docs/sdks/unkey/README.md +++ b/docs/sdks/unkey/README.md @@ -16,6 +16,7 @@ package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -24,7 +25,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.CreateAPIRequestBody{ Name: "my-api", @@ -71,6 +72,7 @@ func main() { package main import( + "os" unkeygo "github.com/unkeyed/unkey-go" "github.com/unkeyed/unkey-go/models/operations" "context" @@ -79,7 +81,7 @@ import( func main() { s := unkeygo.New( - unkeygo.WithSecurity(""), + unkeygo.WithSecurity(os.Getenv("BEARER_AUTH")), ) request := operations.DeleteAPIRequestBody{ APIID: "api_1234", diff --git a/internal/utils/queryparams.go b/internal/utils/queryparams.go index e2c5b9d..b02d3b1 100644 --- a/internal/utils/queryparams.go +++ b/internal/utils/queryparams.go @@ -6,9 +6,15 @@ import ( "context" "encoding/json" "fmt" + "math/big" "net/http" "net/url" "reflect" + "time" + + "github.com/ericlagergren/decimal" + + "github.com/unkeyed/unkey-go/types" ) func PopulateQueryParams(_ context.Context, req *http.Request, queryParams interface{}, globals interface{}) error { @@ -132,54 +138,97 @@ func populateDeepObjectParams(tag *paramTag, objType reflect.Type, objValue refl return values } - if objType.Kind() == reflect.Pointer { - objType = objType.Elem() + if objValue.Kind() == reflect.Pointer { objValue = objValue.Elem() } - switch objType.Kind() { + switch objValue.Kind() { + case reflect.Map: + populateDeepObjectParamsMap(values, tag.ParamName, objValue) case reflect.Struct: - for i := 0; i < objType.NumField(); i++ { - fieldType := objType.Field(i) - valType := objValue.Field(i) + populateDeepObjectParamsStruct(values, tag.ParamName, objValue) + } - if isNil(fieldType.Type, valType) { - continue - } + return values +} - if fieldType.Type.Kind() == reflect.Pointer { - valType = valType.Elem() - } +func populateDeepObjectParamsArray(qsValues url.Values, priorScope string, value reflect.Value) { + if value.Kind() != reflect.Array && value.Kind() != reflect.Slice { + return + } - qpTag := parseQueryParamTag(fieldType) - if qpTag == nil { - continue - } + for i := 0; i < value.Len(); i++ { + qsValues.Add(priorScope, valToString(value.Index(i).Interface())) + } +} - switch valType.Kind() { - case reflect.Array, reflect.Slice: - for i := 0; i < valType.Len(); i++ { - values.Add(fmt.Sprintf("%s[%s]", tag.ParamName, qpTag.ParamName), valToString(valType.Index(i).Interface())) - } - default: - values.Add(fmt.Sprintf("%s[%s]", tag.ParamName, qpTag.ParamName), valToString(valType.Interface())) - } +func populateDeepObjectParamsMap(qsValues url.Values, priorScope string, mapValue reflect.Value) { + if mapValue.Kind() != reflect.Map { + return + } + + iter := mapValue.MapRange() + + for iter.Next() { + scope := priorScope + "[" + iter.Key().String() + "]" + iterValue := iter.Value() + + switch iterValue.Kind() { + case reflect.Array, reflect.Slice: + populateDeepObjectParamsArray(qsValues, scope, iterValue) + case reflect.Map: + populateDeepObjectParamsMap(qsValues, scope, iterValue) + default: + qsValues.Add(scope, valToString(iterValue.Interface())) } - case reflect.Map: - iter := objValue.MapRange() - for iter.Next() { - switch iter.Value().Kind() { - case reflect.Array, reflect.Slice: - for i := 0; i < iter.Value().Len(); i++ { - values.Add(fmt.Sprintf("%s[%s]", tag.ParamName, iter.Key().String()), valToString(iter.Value().Index(i).Interface())) - } - default: - values.Add(fmt.Sprintf("%s[%s]", tag.ParamName, iter.Key().String()), valToString(iter.Value().Interface())) + } +} + +func populateDeepObjectParamsStruct(qsValues url.Values, priorScope string, structValue reflect.Value) { + if structValue.Kind() != reflect.Struct { + return + } + + structType := structValue.Type() + + for i := 0; i < structType.NumField(); i++ { + field := structType.Field(i) + fieldValue := structValue.Field(i) + + if isNil(field.Type, fieldValue) { + continue + } + + if fieldValue.Kind() == reflect.Pointer { + fieldValue = fieldValue.Elem() + } + + qpTag := parseQueryParamTag(field) + + if qpTag == nil { + continue + } + + scope := priorScope + "[" + qpTag.ParamName + "]" + + switch fieldValue.Kind() { + case reflect.Array, reflect.Slice: + populateDeepObjectParamsArray(qsValues, scope, fieldValue) + case reflect.Map: + populateDeepObjectParamsMap(qsValues, scope, fieldValue) + case reflect.Struct: + switch fieldValue.Type() { + case reflect.TypeOf(big.Int{}), reflect.TypeOf(decimal.Big{}), reflect.TypeOf(time.Time{}), reflect.TypeOf(types.Date{}): + qsValues.Add(scope, valToString(fieldValue.Interface())) + + continue } + + populateDeepObjectParamsStruct(qsValues, scope, fieldValue) + default: + qsValues.Add(scope, valToString(fieldValue.Interface())) } } - - return values } func populateFormParams(tag *paramTag, objType reflect.Type, objValue reflect.Value, delimiter string) url.Values { diff --git a/keys.go b/keys.go index 3d93d6c..7ec3d09 100644 --- a/keys.go +++ b/keys.go @@ -39,6 +39,7 @@ func (s *Keys) GetKey(ctx context.Context, request operations.GetKeyRequest, opt o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,12 +47,24 @@ func (s *Keys) GetKey(ctx context.Context, request operations.GetKeyRequest, opt return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.getKey") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -70,10 +83,11 @@ func (s *Keys) GetKey(ctx context.Context, request operations.GetKeyRequest, opt globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -81,31 +95,58 @@ func (s *Keys) GetKey(ctx context.Context, request operations.GetKeyRequest, opt }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -114,15 +155,19 @@ func (s *Keys) GetKey(ctx context.Context, request operations.GetKeyRequest, opt } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -260,6 +305,7 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -267,6 +313,7 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.deleteKey") if err != nil { @@ -278,6 +325,17 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -293,10 +351,11 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -304,31 +363,58 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -337,15 +423,19 @@ func (s *Keys) DeleteKey(ctx context.Context, request operations.DeleteKeyReques } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -483,6 +573,7 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -490,6 +581,7 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.createKey") if err != nil { @@ -501,6 +593,17 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -516,10 +619,11 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -527,31 +631,58 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -560,15 +691,19 @@ func (s *Keys) CreateKey(ctx context.Context, request operations.CreateKeyReques } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -706,6 +841,7 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -713,6 +849,7 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.verifyKey") if err != nil { @@ -724,6 +861,17 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -739,10 +887,11 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -750,31 +899,58 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -783,15 +959,19 @@ func (s *Keys) VerifyKey(ctx context.Context, request components.V1KeysVerifyKey } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -929,6 +1109,7 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -936,6 +1117,7 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.updateKey") if err != nil { @@ -947,6 +1129,17 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -962,10 +1155,11 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -973,31 +1167,58 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1006,15 +1227,19 @@ func (s *Keys) UpdateKey(ctx context.Context, request operations.UpdateKeyReques } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1152,6 +1377,7 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -1159,6 +1385,7 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.updateRemaining") if err != nil { @@ -1170,6 +1397,17 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -1185,10 +1423,11 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -1196,31 +1435,58 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1229,15 +1495,19 @@ func (s *Keys) UpdateRemaining(ctx context.Context, request operations.UpdateRem } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1375,6 +1645,7 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -1382,12 +1653,24 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/keys.getVerifications") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -1406,10 +1689,11 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -1417,31 +1701,58 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1450,15 +1761,19 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1584,3 +1899,1611 @@ func (s *Keys) GetVerifications(ctx context.Context, request operations.GetVerif return res, nil } + +func (s *Keys) AddPermissions(ctx context.Context, request operations.AddPermissionsRequestBody, opts ...operations.Option) (*operations.AddPermissionsResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "addPermissions", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.addPermissions") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.AddPermissionsResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out []operations.ResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.ResponseBodies = out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} + +func (s *Keys) RemovePermissions(ctx context.Context, request operations.RemovePermissionsRequestBody, opts ...operations.Option) (*operations.RemovePermissionsResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "removePermissions", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.removePermissions") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.RemovePermissionsResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out operations.RemovePermissionsResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.Object = &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} + +func (s *Keys) SetPermissions(ctx context.Context, request operations.SetPermissionsRequestBody, opts ...operations.Option) (*operations.SetPermissionsResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "setPermissions", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.setPermissions") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.SetPermissionsResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out []operations.SetPermissionsResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.ResponseBodies = out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} + +func (s *Keys) AddRoles(ctx context.Context, request operations.AddRolesRequestBody, opts ...operations.Option) (*operations.AddRolesResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "addRoles", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.addRoles") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.AddRolesResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out []operations.AddRolesResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.ResponseBodies = out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} + +func (s *Keys) RemoveRoles(ctx context.Context, request operations.RemoveRolesRequestBody, opts ...operations.Option) (*operations.RemoveRolesResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "removeRoles", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.removeRoles") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.RemoveRolesResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out operations.RemoveRolesResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.Object = &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} + +func (s *Keys) SetRoles(ctx context.Context, request operations.SetRolesRequestBody, opts ...operations.Option) (*operations.SetRolesResponse, error) { + hookCtx := hooks.HookContext{ + Context: ctx, + OperationID: "setRoles", + OAuth2Scopes: []string{}, + SecuritySource: s.sdkConfiguration.Security, + } + + o := operations.Options{} + supportedOptions := []string{ + operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, + } + + for _, opt := range opts { + if err := opt(&o, supportedOptions...); err != nil { + return nil, fmt.Errorf("error applying option: %w", err) + } + } + + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) + opURL, err := url.JoinPath(baseURL, "/v1/keys.setRoles") + if err != nil { + return nil, fmt.Errorf("error generating URL: %w", err) + } + + bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`) + if err != nil { + return nil, err + } + + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", s.sdkConfiguration.UserAgent) + req.Header.Set("Content-Type", reqContentType) + + if err := utils.PopulateSecurity(ctx, req, s.sdkConfiguration.Security); err != nil { + return nil, err + } + + globalRetryConfig := s.sdkConfiguration.RetryConfig + retryConfig := o.Retries + if retryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { + retryConfig = &retry.Config{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ + InitialInterval: 50, + MaxInterval: 1000, + Exponent: 1.5, + MaxElapsedTime: 30000, + }, + RetryConnectionErrors: true, + } + } + } + + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } else { + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, err + } + + httpRes, err = s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } + } + } + + res := &operations.SetRolesResponse{ + HTTPMeta: components.HTTPMetadata{ + Request: req, + Response: httpRes, + }, + } + + rawBody, err := io.ReadAll(httpRes.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + httpRes.Body.Close() + httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody)) + + switch { + case httpRes.StatusCode == 200: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out []operations.SetRolesResponseBody + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + res.ResponseBodies = out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 400: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrBadRequest + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 401: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrUnauthorized + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 403: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrForbidden + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 404: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrNotFound + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 409: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrConflict + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 429: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrTooManyRequests + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode == 500: + switch { + case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): + var out sdkerrors.ErrInternalServerError + if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { + return nil, err + } + + return nil, &out + default: + return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", httpRes.Header.Get("Content-Type")), httpRes.StatusCode, string(rawBody), httpRes) + } + case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500: + fallthrough + case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600: + return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes) + default: + return nil, sdkerrors.NewSDKError("unknown status code returned", httpRes.StatusCode, string(rawBody), httpRes) + } + + return res, nil + +} diff --git a/liveness.go b/liveness.go index 368fe05..65ee104 100644 --- a/liveness.go +++ b/liveness.go @@ -39,6 +39,7 @@ func (s *Liveness) V1Liveness(ctx context.Context, opts ...operations.Option) (* o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,12 +47,24 @@ func (s *Liveness) V1Liveness(ctx context.Context, opts ...operations.Option) (* return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/liveness") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -66,10 +79,11 @@ func (s *Liveness) V1Liveness(ctx context.Context, opts ...operations.Option) (* globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -77,31 +91,58 @@ func (s *Liveness) V1Liveness(ctx context.Context, opts ...operations.Option) (* }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -110,15 +151,19 @@ func (s *Liveness) V1Liveness(ctx context.Context, opts ...operations.Option) (* } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } diff --git a/migrations.go b/migrations.go index 180bbfd..ede37f7 100644 --- a/migrations.go +++ b/migrations.go @@ -39,6 +39,7 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,6 +47,7 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/migrations.createKeys") if err != nil { @@ -57,6 +59,17 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -72,10 +85,11 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -83,31 +97,58 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -116,15 +157,19 @@ func (s *Migrations) V1MigrationsCreateKeys(ctx context.Context, request []opera } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -262,6 +307,7 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -269,6 +315,7 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/migrations.enqueueKeys") if err != nil { @@ -280,6 +327,17 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -295,10 +353,11 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -306,31 +365,58 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -339,15 +425,19 @@ func (s *Migrations) V1MigrationsEnqueueKeys(ctx context.Context, request operat } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } diff --git a/models/operations/addpermissions.go b/models/operations/addpermissions.go new file mode 100644 index 0000000..4b58ada --- /dev/null +++ b/models/operations/addpermissions.go @@ -0,0 +1,100 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type AddPermissionsPermissions struct { + // The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *AddPermissionsPermissions) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *AddPermissionsPermissions) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *AddPermissionsPermissions) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +type AddPermissionsRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The permissions you want to add to this key + Permissions []AddPermissionsPermissions `json:"permissions"` +} + +func (o *AddPermissionsRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *AddPermissionsRequestBody) GetPermissions() []AddPermissionsPermissions { + if o == nil { + return []AddPermissionsPermissions{} + } + return o.Permissions +} + +type ResponseBody struct { + // The id of the permission. This is used internally + ID string `json:"id"` + // The name of the permission + Name string `json:"name"` +} + +func (o *ResponseBody) GetID() string { + if o == nil { + return "" + } + return o.ID +} + +func (o *ResponseBody) GetName() string { + if o == nil { + return "" + } + return o.Name +} + +type AddPermissionsResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // All currently connected permissions + ResponseBodies []ResponseBody +} + +func (o *AddPermissionsResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *AddPermissionsResponse) GetResponseBodies() []ResponseBody { + if o == nil { + return nil + } + return o.ResponseBodies +} diff --git a/models/operations/addroles.go b/models/operations/addroles.go new file mode 100644 index 0000000..99e2cf4 --- /dev/null +++ b/models/operations/addroles.go @@ -0,0 +1,101 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type AddRolesRoles struct { + // The id of the role. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *AddRolesRoles) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *AddRolesRoles) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *AddRolesRoles) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +type AddRolesRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The roles you want to set for this key. This overwrites all existing roles. + // Setting roles requires the `rbac.*.add_role_to_key` permission. + Roles []AddRolesRoles `json:"roles"` +} + +func (o *AddRolesRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *AddRolesRequestBody) GetRoles() []AddRolesRoles { + if o == nil { + return []AddRolesRoles{} + } + return o.Roles +} + +type AddRolesResponseBody struct { + // The id of the role. This is used internally + ID string `json:"id"` + // The name of the role + Name string `json:"name"` +} + +func (o *AddRolesResponseBody) GetID() string { + if o == nil { + return "" + } + return o.ID +} + +func (o *AddRolesResponseBody) GetName() string { + if o == nil { + return "" + } + return o.Name +} + +type AddRolesResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // All currently connected roles + ResponseBodies []AddRolesResponseBody +} + +func (o *AddRolesResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *AddRolesResponse) GetResponseBodies() []AddRolesResponseBody { + if o == nil { + return nil + } + return o.ResponseBodies +} diff --git a/models/operations/listpermissions.go b/models/operations/listpermissions.go index 5f00e63..e32016a 100644 --- a/models/operations/listpermissions.go +++ b/models/operations/listpermissions.go @@ -6,7 +6,7 @@ import ( "github.com/unkeyed/unkey-go/models/components" ) -type ResponseBody struct { +type ListPermissionsResponseBody struct { // The id of the permission ID string `json:"id"` // The name of the permission. @@ -15,21 +15,21 @@ type ResponseBody struct { Description *string `json:"description,omitempty"` } -func (o *ResponseBody) GetID() string { +func (o *ListPermissionsResponseBody) GetID() string { if o == nil { return "" } return o.ID } -func (o *ResponseBody) GetName() string { +func (o *ListPermissionsResponseBody) GetName() string { if o == nil { return "" } return o.Name } -func (o *ResponseBody) GetDescription() *string { +func (o *ListPermissionsResponseBody) GetDescription() *string { if o == nil { return nil } @@ -39,7 +39,7 @@ func (o *ResponseBody) GetDescription() *string { type ListPermissionsResponse struct { HTTPMeta components.HTTPMetadata `json:"-"` // The permissions in your workspace - ResponseBodies []ResponseBody + ResponseBodies []ListPermissionsResponseBody } func (o *ListPermissionsResponse) GetHTTPMeta() components.HTTPMetadata { @@ -49,7 +49,7 @@ func (o *ListPermissionsResponse) GetHTTPMeta() components.HTTPMetadata { return o.HTTPMeta } -func (o *ListPermissionsResponse) GetResponseBodies() []ResponseBody { +func (o *ListPermissionsResponse) GetResponseBodies() []ListPermissionsResponseBody { if o == nil { return nil } diff --git a/models/operations/options.go b/models/operations/options.go index 78fb08f..98d7c51 100644 --- a/models/operations/options.go +++ b/models/operations/options.go @@ -6,6 +6,7 @@ import ( "errors" "github.com/unkeyed/unkey-go/internal/utils" "github.com/unkeyed/unkey-go/retry" + "time" ) var ErrUnsupportedOption = errors.New("unsupported option") @@ -13,12 +14,14 @@ var ErrUnsupportedOption = errors.New("unsupported option") const ( SupportedOptionServerURL = "serverURL" SupportedOptionRetries = "retries" + SupportedOptionTimeout = "timeout" SupportedOptionAcceptHeaderOverride = "acceptHeaderOverride" ) type Options struct { ServerURL *string Retries *retry.Config + Timeout *time.Duration } type Option func(*Options, ...string) error @@ -62,3 +65,15 @@ func WithRetries(config retry.Config) Option { return nil } } + +// WithOperationTimeout allows setting the request timeout applied for an operation. +func WithOperationTimeout(timeout time.Duration) Option { + return func(opts *Options, supportedOptions ...string) error { + if !utils.Contains(supportedOptions, SupportedOptionRetries) { + return ErrUnsupportedOption + } + + opts.Timeout = &timeout + return nil + } +} diff --git a/models/operations/removepermissions.go b/models/operations/removepermissions.go new file mode 100644 index 0000000..074de52 --- /dev/null +++ b/models/operations/removepermissions.go @@ -0,0 +1,73 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type RemovePermissionsPermissions struct { + // The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` +} + +func (o *RemovePermissionsPermissions) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *RemovePermissionsPermissions) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +type RemovePermissionsRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The permissions you want to remove from this key. + Permissions []RemovePermissionsPermissions `json:"permissions"` +} + +func (o *RemovePermissionsRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *RemovePermissionsRequestBody) GetPermissions() []RemovePermissionsPermissions { + if o == nil { + return []RemovePermissionsPermissions{} + } + return o.Permissions +} + +// RemovePermissionsResponseBody - Success +type RemovePermissionsResponseBody struct { +} + +type RemovePermissionsResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // Success + Object *RemovePermissionsResponseBody +} + +func (o *RemovePermissionsResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *RemovePermissionsResponse) GetObject() *RemovePermissionsResponseBody { + if o == nil { + return nil + } + return o.Object +} diff --git a/models/operations/removeroles.go b/models/operations/removeroles.go new file mode 100644 index 0000000..f10bae9 --- /dev/null +++ b/models/operations/removeroles.go @@ -0,0 +1,73 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type RemoveRolesRoles struct { + // The id of the role. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` +} + +func (o *RemoveRolesRoles) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *RemoveRolesRoles) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +type RemoveRolesRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The roles you want to remove from this key. + Roles []RemoveRolesRoles `json:"roles"` +} + +func (o *RemoveRolesRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *RemoveRolesRequestBody) GetRoles() []RemoveRolesRoles { + if o == nil { + return []RemoveRolesRoles{} + } + return o.Roles +} + +// RemoveRolesResponseBody - Success +type RemoveRolesResponseBody struct { +} + +type RemoveRolesResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // Success + Object *RemoveRolesResponseBody +} + +func (o *RemoveRolesResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *RemoveRolesResponse) GetObject() *RemoveRolesResponseBody { + if o == nil { + return nil + } + return o.Object +} diff --git a/models/operations/setpermissions.go b/models/operations/setpermissions.go new file mode 100644 index 0000000..b1a50c6 --- /dev/null +++ b/models/operations/setpermissions.go @@ -0,0 +1,101 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type SetPermissionsPermissions struct { + // The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *SetPermissionsPermissions) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *SetPermissionsPermissions) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *SetPermissionsPermissions) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +type SetPermissionsRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The permissions you want to set for this key. This overwrites all existing permissions. + // Setting permissions requires the `rbac.*.add_permission_to_key` permission. + Permissions []SetPermissionsPermissions `json:"permissions"` +} + +func (o *SetPermissionsRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *SetPermissionsRequestBody) GetPermissions() []SetPermissionsPermissions { + if o == nil { + return []SetPermissionsPermissions{} + } + return o.Permissions +} + +type SetPermissionsResponseBody struct { + // The id of the permission. This is used internally + ID string `json:"id"` + // The name of the permission + Name string `json:"name"` +} + +func (o *SetPermissionsResponseBody) GetID() string { + if o == nil { + return "" + } + return o.ID +} + +func (o *SetPermissionsResponseBody) GetName() string { + if o == nil { + return "" + } + return o.Name +} + +type SetPermissionsResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // All currently connected permissions + ResponseBodies []SetPermissionsResponseBody +} + +func (o *SetPermissionsResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *SetPermissionsResponse) GetResponseBodies() []SetPermissionsResponseBody { + if o == nil { + return nil + } + return o.ResponseBodies +} diff --git a/models/operations/setroles.go b/models/operations/setroles.go new file mode 100644 index 0000000..16a9d86 --- /dev/null +++ b/models/operations/setroles.go @@ -0,0 +1,101 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package operations + +import ( + "github.com/unkeyed/unkey-go/models/components" +) + +type SetRolesRoles struct { + // The id of the role. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *SetRolesRoles) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *SetRolesRoles) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *SetRolesRoles) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +type SetRolesRequestBody struct { + // The id of the key. + KeyID string `json:"keyId"` + // The roles you want to set for this key. This overwrites all existing roles. + // Setting roles requires the `rbac.*.add_role_to_key` permission. + Roles []SetRolesRoles `json:"roles"` +} + +func (o *SetRolesRequestBody) GetKeyID() string { + if o == nil { + return "" + } + return o.KeyID +} + +func (o *SetRolesRequestBody) GetRoles() []SetRolesRoles { + if o == nil { + return []SetRolesRoles{} + } + return o.Roles +} + +type SetRolesResponseBody struct { + // The id of the role. This is used internally + ID string `json:"id"` + // The name of the role + Name string `json:"name"` +} + +func (o *SetRolesResponseBody) GetID() string { + if o == nil { + return "" + } + return o.ID +} + +func (o *SetRolesResponseBody) GetName() string { + if o == nil { + return "" + } + return o.Name +} + +type SetRolesResponse struct { + HTTPMeta components.HTTPMetadata `json:"-"` + // All currently connected roles + ResponseBodies []SetRolesResponseBody +} + +func (o *SetRolesResponse) GetHTTPMeta() components.HTTPMetadata { + if o == nil { + return components.HTTPMetadata{} + } + return o.HTTPMeta +} + +func (o *SetRolesResponse) GetResponseBodies() []SetRolesResponseBody { + if o == nil { + return nil + } + return o.ResponseBodies +} diff --git a/models/operations/updatekey.go b/models/operations/updatekey.go index 23987a8..f420c02 100644 --- a/models/operations/updatekey.go +++ b/models/operations/updatekey.go @@ -169,6 +169,71 @@ func (o *UpdateKeyRefill) GetAmount() int64 { return o.Amount } +type Roles struct { + // The id of the role. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the role via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating roles requires your root key to have the `rbac.*.create_role` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *Roles) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *Roles) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *Roles) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +type Permissions struct { + // The id of the permission. Provide either `id` or `name`. If both are provided `id` is used. + ID *string `json:"id,omitempty"` + // Identify the permission via its name. Provide either `id` or `name`. If both are provided `id` is used. + Name *string `json:"name,omitempty"` + // Set to true to automatically create the permissions they do not exist yet. Only works when specifying `name`. + // Autocreating permissions requires your root key to have the `rbac.*.create_permission` permission, otherwise the request will get rejected + Create *bool `json:"create,omitempty"` +} + +func (o *Permissions) GetID() *string { + if o == nil { + return nil + } + return o.ID +} + +func (o *Permissions) GetName() *string { + if o == nil { + return nil + } + return o.Name +} + +func (o *Permissions) GetCreate() *bool { + if o == nil { + return nil + } + return o.Create +} + +// UpdateKeyRequestBody - Update a key's configuration. +// +// The `apis..update_key` permission is required. type UpdateKeyRequestBody struct { // The id of the key you want to modify KeyID string `json:"keyId"` @@ -188,6 +253,12 @@ type UpdateKeyRequestBody struct { Refill *UpdateKeyRefill `json:"refill,omitempty"` // Set if key is enabled or disabled. If disabled, the key cannot be used to verify. Enabled *bool `json:"enabled,omitempty"` + // The roles you want to set for this key. This overwrites all existing roles. + // Setting roles requires the `rbac.*.add_role_to_key` permission. + Roles []Roles `json:"roles,omitempty"` + // The permissions you want to set for this key. This overwrites all existing permissions. + // Setting permissions requires the `rbac.*.add_permission_to_key` permission. + Permissions []Permissions `json:"permissions,omitempty"` } func (o *UpdateKeyRequestBody) GetKeyID() string { @@ -253,6 +324,20 @@ func (o *UpdateKeyRequestBody) GetEnabled() *bool { return o.Enabled } +func (o *UpdateKeyRequestBody) GetRoles() []Roles { + if o == nil { + return nil + } + return o.Roles +} + +func (o *UpdateKeyRequestBody) GetPermissions() []Permissions { + if o == nil { + return nil + } + return o.Permissions +} + // UpdateKeyResponseBody - The key was successfully updated, it may take up to 30s for this to take effect in all regions type UpdateKeyResponseBody struct { } diff --git a/permissions.go b/permissions.go index 33a8b07..96b41e8 100644 --- a/permissions.go +++ b/permissions.go @@ -39,6 +39,7 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,6 +47,7 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.createPermission") if err != nil { @@ -57,6 +59,17 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -72,10 +85,11 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -83,31 +97,58 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -116,15 +157,19 @@ func (s *Permissions) CreatePermission(ctx context.Context, request operations.C } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -262,6 +307,7 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -269,6 +315,7 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.deletePermission") if err != nil { @@ -280,6 +327,17 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -295,10 +353,11 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -306,31 +365,58 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -339,15 +425,19 @@ func (s *Permissions) DeletePermission(ctx context.Context, request operations.D } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -485,6 +575,7 @@ func (s *Permissions) GetPermission(ctx context.Context, request operations.GetP o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -492,12 +583,24 @@ func (s *Permissions) GetPermission(ctx context.Context, request operations.GetP return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.getPermission") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -516,10 +619,11 @@ func (s *Permissions) GetPermission(ctx context.Context, request operations.GetP globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -527,31 +631,58 @@ func (s *Permissions) GetPermission(ctx context.Context, request operations.GetP }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -560,15 +691,19 @@ func (s *Permissions) GetPermission(ctx context.Context, request operations.GetP } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -706,6 +841,7 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -713,12 +849,24 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.listPermissions") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -733,10 +881,11 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -744,31 +893,58 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -777,15 +953,19 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -807,7 +987,7 @@ func (s *Permissions) ListPermissions(ctx context.Context, opts ...operations.Op case httpRes.StatusCode == 200: switch { case utils.MatchContentType(httpRes.Header.Get("Content-Type"), `application/json`): - var out []operations.ResponseBody + var out []operations.ListPermissionsResponseBody if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil { return nil, err } @@ -923,6 +1103,7 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -930,6 +1111,7 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.createRole") if err != nil { @@ -941,6 +1123,17 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -956,10 +1149,11 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -967,31 +1161,58 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1000,15 +1221,19 @@ func (s *Permissions) CreateRole(ctx context.Context, request operations.CreateR } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1146,6 +1371,7 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -1153,6 +1379,7 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.deleteRole") if err != nil { @@ -1164,6 +1391,17 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -1179,10 +1417,11 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -1190,31 +1429,58 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1223,15 +1489,19 @@ func (s *Permissions) DeleteRole(ctx context.Context, request operations.DeleteR } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1369,6 +1639,7 @@ func (s *Permissions) GetRole(ctx context.Context, request operations.GetRoleReq o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -1376,12 +1647,24 @@ func (s *Permissions) GetRole(ctx context.Context, request operations.GetRoleReq return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.getRole") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -1400,10 +1683,11 @@ func (s *Permissions) GetRole(ctx context.Context, request operations.GetRoleReq globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -1411,31 +1695,58 @@ func (s *Permissions) GetRole(ctx context.Context, request operations.GetRoleReq }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1444,15 +1755,19 @@ func (s *Permissions) GetRole(ctx context.Context, request operations.GetRoleReq } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -1590,6 +1905,7 @@ func (s *Permissions) ListRoles(ctx context.Context, opts ...operations.Option) o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -1597,12 +1913,24 @@ func (s *Permissions) ListRoles(ctx context.Context, opts ...operations.Option) return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/permissions.listRoles") if err != nil { return nil, fmt.Errorf("error generating URL: %w", err) } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "GET", opURL, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -1617,10 +1945,11 @@ func (s *Permissions) ListRoles(ctx context.Context, opts ...operations.Option) globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -1628,31 +1957,58 @@ func (s *Permissions) ListRoles(ctx context.Context, opts ...operations.Option) }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -1661,15 +2017,19 @@ func (s *Permissions) ListRoles(ctx context.Context, opts ...operations.Option) } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } diff --git a/ratelimits.go b/ratelimits.go index dfd9e09..ba54162 100644 --- a/ratelimits.go +++ b/ratelimits.go @@ -39,6 +39,7 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -46,6 +47,7 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/ratelimits.limit") if err != nil { @@ -57,6 +59,17 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -72,10 +85,11 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -83,31 +97,58 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -116,15 +157,19 @@ func (s *Ratelimits) Limit(ctx context.Context, request operations.LimitRequestB } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } diff --git a/unkey.go b/unkey.go index 52b3b14..4fe693e 100644 --- a/unkey.go +++ b/unkey.go @@ -60,6 +60,7 @@ type sdkConfiguration struct { UserAgent string RetryConfig *retry.Config Hooks *hooks.Hooks + Timeout *time.Duration } func (c *sdkConfiguration) GetServerDetails() (string, map[string]string) { @@ -142,15 +143,22 @@ func WithRetryConfig(retryConfig retry.Config) SDKOption { } } +// WithTimeout Optional request timeout applied to each operation +func WithTimeout(timeout time.Duration) SDKOption { + return func(sdk *Unkey) { + sdk.sdkConfiguration.Timeout = &timeout + } +} + // New creates a new instance of the SDK with the provided options func New(opts ...SDKOption) *Unkey { sdk := &Unkey{ sdkConfiguration: sdkConfiguration{ Language: "go", OpenAPIDocVersion: "1.0.0", - SDKVersion: "0.6.1", - GenVersion: "2.361.10", - UserAgent: "speakeasy-sdk/go 0.6.1 2.361.10 1.0.0 github.com/unkeyed/unkey-go", + SDKVersion: "0.7.0", + GenVersion: "2.372.3", + UserAgent: "speakeasy-sdk/go 0.7.0 2.372.3 1.0.0 github.com/unkeyed/unkey-go", Hooks: hooks.New(), }, } @@ -196,6 +204,7 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -203,6 +212,7 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/apis.createApi") if err != nil { @@ -214,6 +224,17 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -229,10 +250,11 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -240,31 +262,58 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -273,15 +322,19 @@ func (s *Unkey) CreateAPI(ctx context.Context, request operations.CreateAPIReque } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } } @@ -419,6 +472,7 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque o := operations.Options{} supportedOptions := []string{ operations.SupportedOptionRetries, + operations.SupportedOptionTimeout, } for _, opt := range opts { @@ -426,6 +480,7 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque return nil, fmt.Errorf("error applying option: %w", err) } } + baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) opURL, err := url.JoinPath(baseURL, "/v1/apis.deleteApi") if err != nil { @@ -437,6 +492,17 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque return nil, err } + timeout := o.Timeout + if timeout == nil { + timeout = s.sdkConfiguration.Timeout + } + + if timeout != nil { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, *timeout) + defer cancel() + } + req, err := http.NewRequestWithContext(ctx, "POST", opURL, bodyReader) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) @@ -452,10 +518,11 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque globalRetryConfig := s.sdkConfiguration.RetryConfig retryConfig := o.Retries if retryConfig == nil { - if globalRetryConfig == nil { + if globalRetryConfig != nil { + retryConfig = globalRetryConfig + } else { retryConfig = &retry.Config{ - Strategy: "backoff", - Backoff: &retry.BackoffStrategy{ + Strategy: "backoff", Backoff: &retry.BackoffStrategy{ InitialInterval: 50, MaxInterval: 1000, Exponent: 1.5, @@ -463,31 +530,58 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque }, RetryConnectionErrors: true, } - } else { - retryConfig = globalRetryConfig } } - httpRes, err := utils.Retry(ctx, utils.Retries{ - Config: retryConfig, - StatusCodes: []string{ - "5XX", - }, - }, func() (*http.Response, error) { - if req.Body != nil { - copyBody, err := req.GetBody() + var httpRes *http.Response + if retryConfig != nil { + httpRes, err = utils.Retry(ctx, utils.Retries{ + Config: retryConfig, + StatusCodes: []string{ + "5XX", + }, + }, func() (*http.Response, error) { + if req.Body != nil { + copyBody, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = copyBody + } + + req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) + if err != nil { + return nil, backoff.Permanent(err) + } + + httpRes, err := s.sdkConfiguration.Client.Do(req) + if err != nil || httpRes == nil { + if err != nil { + err = fmt.Errorf("error sending request: %w", err) + } else { + err = fmt.Errorf("error sending request: no response") + } + + _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) + } + return httpRes, err + }) + + if err != nil { + return nil, err + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) if err != nil { return nil, err } - req.Body = copyBody } - + } else { req, err = s.sdkConfiguration.Hooks.BeforeRequest(hooks.BeforeRequestContext{HookContext: hookCtx}, req) if err != nil { - return nil, backoff.Permanent(err) + return nil, err } - httpRes, err := s.sdkConfiguration.Client.Do(req) + httpRes, err = s.sdkConfiguration.Client.Do(req) if err != nil || httpRes == nil { if err != nil { err = fmt.Errorf("error sending request: %w", err) @@ -496,15 +590,19 @@ func (s *Unkey) DeleteAPI(ctx context.Context, request operations.DeleteAPIReque } _, err = s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, nil, err) - } - return httpRes, err - }) - if err != nil { - return nil, err - } else { - httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) - if err != nil { return nil, err + } else if utils.MatchStatusCodes([]string{"400", "401", "403", "404", "409", "429", "4XX", "500", "5XX"}, httpRes.StatusCode) { + _httpRes, err := s.sdkConfiguration.Hooks.AfterError(hooks.AfterErrorContext{HookContext: hookCtx}, httpRes, nil) + if err != nil { + return nil, err + } else if _httpRes != nil { + httpRes = _httpRes + } + } else { + httpRes, err = s.sdkConfiguration.Hooks.AfterSuccess(hooks.AfterSuccessContext{HookContext: hookCtx}, httpRes) + if err != nil { + return nil, err + } } }