diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.CreateGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.CreateGuard.g.cs index cdeb281..f8282f7 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.CreateGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.CreateGuard.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -44,11 +45,13 @@ partial void ProcessCreateGuardResponseContent( /// Creates a Guard /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task CreateGuardAsync( global::Guardrails.Guard request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { request = request ?? throw new global::System.ArgumentNullException(nameof(request)); @@ -65,22 +68,43 @@ partial void ProcessCreateGuardResponseContent( securityRequirements: s_CreateGuardSecurityRequirements, operationName: "CreateGuardAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: "/guards", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Post, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: "/guards", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Post, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -90,156 +114,315 @@ partial void ProcessCreateGuardResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } - var __httpRequestContentBody = request.ToJson(JsonSerializerContext); - var __httpRequestContent = new global::System.Net.Http.StringContent( - content: __httpRequestContentBody, - encoding: global::System.Text.Encoding.UTF8, - mediaType: "application/json"); - __httpRequest.Content = __httpRequestContent; - - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareCreateGuardRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - request: request); + var __httpRequestContentBody = request.ToJson(JsonSerializerContext); + var __httpRequestContent = new global::System.Net.Http.StringContent( + content: __httpRequestContentBody, + encoding: global::System.Text.Encoding.UTF8, + mediaType: "application/json"); + __httpRequest.Content = __httpRequestContent; + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareCreateGuardRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); - ProcessResponse( - client: HttpClient, - response: __response); - ProcessCreateGuardResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateGuard", + methodName: "CreateGuardAsync", + pathTemplate: "\"/guards\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateGuard", + methodName: "CreateGuardAsync", + pathTemplate: "\"/guards\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateGuard", + methodName: "CreateGuardAsync", + pathTemplate: "\"/guards\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessCreateGuardResponseContent( + response: __response); + ProcessCreateGuardResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateGuard", + methodName: "CreateGuardAsync", + pathTemplate: "\"/guards\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateGuard", + methodName: "CreateGuardAsync", + pathTemplate: "\"/guards\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessCreateGuardResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } /// /// Creates a Guard @@ -254,6 +437,7 @@ partial void ProcessCreateGuardResponseContent( /// A description that concisely states the expected behaviour or purpose of the Guard. /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task CreateGuardAsync( @@ -261,6 +445,7 @@ partial void ProcessCreateGuardResponseContent( string name, string? description = default, global::System.Collections.Generic.IList? validators = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { var __request = new global::Guardrails.Guard @@ -273,6 +458,7 @@ partial void ProcessCreateGuardResponseContent( return await CreateGuardAsync( request: __request, + requestOptions: requestOptions, cancellationToken: cancellationToken).ConfigureAwait(false); } } diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.DeleteGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.DeleteGuard.g.cs index f19f70c..b5fca83 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.DeleteGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.DeleteGuard.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -44,10 +45,12 @@ partial void ProcessDeleteGuardResponseContent( /// Deletes a Guard /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task DeleteGuardAsync( string guardName, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { PrepareArguments( @@ -62,22 +65,43 @@ partial void ProcessDeleteGuardResponseContent( securityRequirements: s_DeleteGuardSecurityRequirements, operationName: "DeleteGuardAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Delete, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Delete, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -87,150 +111,309 @@ partial void ProcessDeleteGuardResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareDeleteGuardRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareDeleteGuardRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + return __httpRequest; + } - ProcessResponse( - client: HttpClient, - response: __response); - ProcessDeleteGuardResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "DeleteGuard", + methodName: "DeleteGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "DELETE", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "DeleteGuard", + methodName: "DeleteGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "DELETE", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "DeleteGuard", + methodName: "DeleteGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "DELETE", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessDeleteGuardResponseContent( + response: __response); + ProcessDeleteGuardResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "DeleteGuard", + methodName: "DeleteGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "DELETE", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "DeleteGuard", + methodName: "DeleteGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "DELETE", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessDeleteGuardResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuard.g.cs index 5f9a012..aacdd25 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuard.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -47,11 +48,13 @@ partial void ProcessGetGuardResponseContent( /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task GetGuardAsync( string guardName, global::System.DateTime? asOf = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { PrepareArguments( @@ -67,25 +70,46 @@ partial void ProcessGetGuardResponseContent( securityRequirements: s_GetGuardSecurityRequirements, operationName: "GetGuardAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}", - baseUri: HttpClient.BaseAddress); - __pathBuilder - .AddOptionalParameter("asOf", asOf?.ToString("yyyy-MM-ddTHH:mm:ssZ")) - ; - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Get, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}", + baseUri: HttpClient.BaseAddress); + __pathBuilder + .AddOptionalParameter("asOf", asOf?.ToString("yyyy-MM-ddTHH:mm:ssZ")) + ; + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -95,151 +119,310 @@ partial void ProcessGetGuardResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareGetGuardRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName, - asOf: asOf); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareGetGuardRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName, + asOf: asOf); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + return __httpRequest; + } - ProcessResponse( - client: HttpClient, - response: __response); - ProcessGetGuardResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuard", + methodName: "GetGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuard", + methodName: "GetGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuard", + methodName: "GetGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessGetGuardResponseContent( + response: __response); + ProcessGetGuardResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuard", + methodName: "GetGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuard", + methodName: "GetGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessGetGuardResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuardHistory.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuardHistory.g.cs index a85a92d..7d63b43 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuardHistory.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuardHistory.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -47,11 +48,13 @@ partial void ProcessGetGuardHistoryResponseContent( /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task> GetGuardHistoryAsync( string guardName, string callId, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { PrepareArguments( @@ -67,22 +70,43 @@ partial void ProcessGetGuardHistoryResponseContent( securityRequirements: s_GetGuardHistorySecurityRequirements, operationName: "GetGuardHistoryAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}/history/{callId}", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Get, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}/history/{callId}", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -92,151 +116,310 @@ partial void ProcessGetGuardHistoryResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareGetGuardHistoryRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName, - callId: callId); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareGetGuardHistoryRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName, + callId: callId); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + return __httpRequest; + } - ProcessResponse( - client: HttpClient, - response: __response); - ProcessGetGuardHistoryResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuardHistory", + methodName: "GetGuardHistoryAsync", + pathTemplate: "$\"/guards/{guardName}/history/{callId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuardHistory", + methodName: "GetGuardHistoryAsync", + pathTemplate: "$\"/guards/{guardName}/history/{callId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuardHistory", + methodName: "GetGuardHistoryAsync", + pathTemplate: "$\"/guards/{guardName}/history/{callId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessGetGuardHistoryResponseContent( + response: __response); + ProcessGetGuardHistoryResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - (global::System.Collections.Generic.IList?)global::System.Text.Json.JsonSerializer.Deserialize(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuardHistory", + methodName: "GetGuardHistoryAsync", + pathTemplate: "$\"/guards/{guardName}/history/{callId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuardHistory", + methodName: "GetGuardHistoryAsync", + pathTemplate: "$\"/guards/{guardName}/history/{callId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - (global::System.Collections.Generic.IList?)await global::System.Text.Json.JsonSerializer.DeserializeAsync(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessGetGuardHistoryResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + (global::System.Collections.Generic.IList?)global::System.Text.Json.JsonSerializer.Deserialize(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + (global::System.Collections.Generic.IList?)await global::System.Text.Json.JsonSerializer.DeserializeAsync(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuards.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuards.g.cs index 00ebb1c..1e0b4b1 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuards.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.GetGuards.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -41,9 +42,11 @@ partial void ProcessGetGuardsResponseContent( /// /// Fetches the configuration for all Guards the user has access to. /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task> GetGuardsAsync( + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { PrepareArguments( @@ -57,22 +60,43 @@ partial void ProcessGetGuardsResponseContent( securityRequirements: s_GetGuardsSecurityRequirements, operationName: "GetGuardsAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: "/guards", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Get, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: "/guards", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -82,149 +106,308 @@ partial void ProcessGetGuardsResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareGetGuardsRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareGetGuardsRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); - - ProcessResponse( - client: HttpClient, - response: __response); - ProcessGetGuardsResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuards", + methodName: "GetGuardsAsync", + pathTemplate: "\"/guards\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuards", + methodName: "GetGuardsAsync", + pathTemplate: "\"/guards\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuards", + methodName: "GetGuardsAsync", + pathTemplate: "\"/guards\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessGetGuardsResponseContent( + response: __response); + ProcessGetGuardsResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - (global::System.Collections.Generic.IList?)global::System.Text.Json.JsonSerializer.Deserialize(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuards", + methodName: "GetGuardsAsync", + pathTemplate: "\"/guards\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetGuards", + methodName: "GetGuardsAsync", + pathTemplate: "\"/guards\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - (global::System.Collections.Generic.IList?)await global::System.Text.Json.JsonSerializer.DeserializeAsync(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessGetGuardsResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + (global::System.Collections.Generic.IList?)global::System.Text.Json.JsonSerializer.Deserialize(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + (global::System.Collections.Generic.IList?)await global::System.Text.Json.JsonSerializer.DeserializeAsync(__content, typeof(global::System.Collections.Generic.IList), JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.UpdateGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.UpdateGuard.g.cs index 678c9f2..e15b944 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.UpdateGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.UpdateGuard.g.cs @@ -14,6 +14,7 @@ public partial class GuardClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -47,12 +48,14 @@ partial void ProcessUpdateGuardResponseContent( /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task UpdateGuardAsync( string guardName, global::Guardrails.Guard request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { request = request ?? throw new global::System.ArgumentNullException(nameof(request)); @@ -70,22 +73,43 @@ partial void ProcessUpdateGuardResponseContent( securityRequirements: s_UpdateGuardSecurityRequirements, operationName: "UpdateGuardAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Put, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Put, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -95,157 +119,316 @@ partial void ProcessUpdateGuardResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } - var __httpRequestContentBody = request.ToJson(JsonSerializerContext); - var __httpRequestContent = new global::System.Net.Http.StringContent( - content: __httpRequestContentBody, - encoding: global::System.Text.Encoding.UTF8, - mediaType: "application/json"); - __httpRequest.Content = __httpRequestContent; - - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareUpdateGuardRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName, - request: request); + var __httpRequestContentBody = request.ToJson(JsonSerializerContext); + var __httpRequestContent = new global::System.Net.Http.StringContent( + content: __httpRequestContentBody, + encoding: global::System.Text.Encoding.UTF8, + mediaType: "application/json"); + __httpRequest.Content = __httpRequestContent; + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareUpdateGuardRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName, + request: request); - ProcessResponse( - client: HttpClient, - response: __response); - ProcessUpdateGuardResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "UpdateGuard", + methodName: "UpdateGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "PUT", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "UpdateGuard", + methodName: "UpdateGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "PUT", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "UpdateGuard", + methodName: "UpdateGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "PUT", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessUpdateGuardResponseContent( + response: __response); + ProcessUpdateGuardResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "UpdateGuard", + methodName: "UpdateGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "PUT", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "UpdateGuard", + methodName: "UpdateGuardAsync", + pathTemplate: "$\"/guards/{guardName}\"", + httpMethod: "PUT", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessUpdateGuardResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.Guard.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.Guard.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } /// /// Updates a Guard @@ -261,6 +444,7 @@ partial void ProcessUpdateGuardResponseContent( /// A description that concisely states the expected behaviour or purpose of the Guard. /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task UpdateGuardAsync( @@ -269,6 +453,7 @@ partial void ProcessUpdateGuardResponseContent( string name, string? description = default, global::System.Collections.Generic.IList? validators = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { var __request = new global::Guardrails.Guard @@ -282,6 +467,7 @@ partial void ProcessUpdateGuardResponseContent( return await UpdateGuardAsync( guardName: guardName, request: __request, + requestOptions: requestOptions, cancellationToken: cancellationToken).ConfigureAwait(false); } } diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardClient.g.cs index 2f9eca8..b13649b 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardClient.g.cs @@ -30,6 +30,9 @@ public sealed partial class GuardClient : global::Guardrails.IGuardClient, globa #if DEBUG = true; #endif + + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } /// /// /// @@ -49,11 +52,37 @@ public GuardClient( global::System.Net.Http.HttpClient? httpClient = null, global::System.Uri? baseUri = null, global::System.Collections.Generic.List? authorizations = null, + bool disposeHttpClient = true) : this( + httpClient, + baseUri, + authorizations, + options: null, + disposeHttpClient: disposeHttpClient) + { + } + + /// + /// Creates a new instance of the GuardClient. + /// If no httpClient is provided, a new one will be created. + /// If no baseUri is provided, the default baseUri from OpenAPI spec will be used. + /// + /// The HttpClient instance. If not provided, a new one will be created. + /// The base URL for the API. If not provided, the default baseUri from OpenAPI spec will be used. + /// The authorizations to use for the requests. + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// Dispose the HttpClient when the instance is disposed. True by default. + public GuardClient( + global::System.Net.Http.HttpClient? httpClient = null, + global::System.Uri? baseUri = null, + global::System.Collections.Generic.List? authorizations = null, + global::Guardrails.AutoSDKClientOptions? options = null, bool disposeHttpClient = true) { + HttpClient = httpClient ?? new global::System.Net.Http.HttpClient(); HttpClient.BaseAddress ??= baseUri ?? new global::System.Uri(DefaultBaseUrl); Authorizations = authorizations ?? new global::System.Collections.Generic.List(); + Options = options ?? new global::Guardrails.AutoSDKClientOptions(); _disposeHttpClient = disposeHttpClient; Initialized(HttpClient); diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.Authorizations.Bearer.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.Authorizations.Bearer.g.cs index b4c12e4..90bd01c 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.Authorizations.Bearer.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.Authorizations.Bearer.g.cs @@ -25,6 +25,7 @@ public void AuthorizeUsingBearer( Authorizations.Add(new global::Guardrails.EndPointAuthorization { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", Value = apiKey, diff --git a/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.g.cs index 77e4d7d..4d206ad 100644 --- a/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.GuardrailsClient.g.cs @@ -31,6 +31,9 @@ public sealed partial class GuardrailsClient : global::Guardrails.IGuardrailsCli #if DEBUG = true; #endif + + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } /// /// /// @@ -40,7 +43,7 @@ public sealed partial class GuardrailsClient : global::Guardrails.IGuardrailsCli /// /// /// - public GuardClient Guard => new GuardClient(HttpClient, authorizations: Authorizations) + public GuardClient Guard => new GuardClient(HttpClient, authorizations: Authorizations, options: Options) { ReadResponseAsString = ReadResponseAsString, JsonSerializerContext = JsonSerializerContext, @@ -49,7 +52,7 @@ public sealed partial class GuardrailsClient : global::Guardrails.IGuardrailsCli /// /// /// - public OpenaiClient Openai => new OpenaiClient(HttpClient, authorizations: Authorizations) + public OpenaiClient Openai => new OpenaiClient(HttpClient, authorizations: Authorizations, options: Options) { ReadResponseAsString = ReadResponseAsString, JsonSerializerContext = JsonSerializerContext, @@ -58,7 +61,7 @@ public sealed partial class GuardrailsClient : global::Guardrails.IGuardrailsCli /// /// /// - public ServiceHealthClient ServiceHealth => new ServiceHealthClient(HttpClient, authorizations: Authorizations) + public ServiceHealthClient ServiceHealth => new ServiceHealthClient(HttpClient, authorizations: Authorizations, options: Options) { ReadResponseAsString = ReadResponseAsString, JsonSerializerContext = JsonSerializerContext, @@ -67,7 +70,7 @@ public sealed partial class GuardrailsClient : global::Guardrails.IGuardrailsCli /// /// /// - public ValidateClient Validate => new ValidateClient(HttpClient, authorizations: Authorizations) + public ValidateClient Validate => new ValidateClient(HttpClient, authorizations: Authorizations, options: Options) { ReadResponseAsString = ReadResponseAsString, JsonSerializerContext = JsonSerializerContext, @@ -86,11 +89,37 @@ public GuardrailsClient( global::System.Net.Http.HttpClient? httpClient = null, global::System.Uri? baseUri = null, global::System.Collections.Generic.List? authorizations = null, + bool disposeHttpClient = true) : this( + httpClient, + baseUri, + authorizations, + options: null, + disposeHttpClient: disposeHttpClient) + { + } + + /// + /// Creates a new instance of the GuardrailsClient. + /// If no httpClient is provided, a new one will be created. + /// If no baseUri is provided, the default baseUri from OpenAPI spec will be used. + /// + /// The HttpClient instance. If not provided, a new one will be created. + /// The base URL for the API. If not provided, the default baseUri from OpenAPI spec will be used. + /// The authorizations to use for the requests. + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// Dispose the HttpClient when the instance is disposed. True by default. + public GuardrailsClient( + global::System.Net.Http.HttpClient? httpClient = null, + global::System.Uri? baseUri = null, + global::System.Collections.Generic.List? authorizations = null, + global::Guardrails.AutoSDKClientOptions? options = null, bool disposeHttpClient = true) { + HttpClient = httpClient ?? new global::System.Net.Http.HttpClient(); HttpClient.BaseAddress ??= baseUri ?? new global::System.Uri(DefaultBaseUrl); Authorizations = authorizations ?? new global::System.Collections.Generic.List(); + Options = options ?? new global::Guardrails.AutoSDKClientOptions(); _disposeHttpClient = disposeHttpClient; Initialized(HttpClient); diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.CreateGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.CreateGuard.g.cs index 048d169..849c1d8 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.CreateGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.CreateGuard.g.cs @@ -8,11 +8,13 @@ public partial interface IGuardClient /// Creates a Guard /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task CreateGuardAsync( global::Guardrails.Guard request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); /// /// Creates a Guard @@ -27,6 +29,7 @@ public partial interface IGuardClient /// A description that concisely states the expected behaviour or purpose of the Guard. /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task CreateGuardAsync( @@ -34,6 +37,7 @@ public partial interface IGuardClient string name, string? description = default, global::System.Collections.Generic.IList? validators = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.DeleteGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.DeleteGuard.g.cs index f0bc281..8b75db2 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.DeleteGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.DeleteGuard.g.cs @@ -8,10 +8,12 @@ public partial interface IGuardClient /// Deletes a Guard /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task DeleteGuardAsync( string guardName, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuard.g.cs index a161eb1..25eb938 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuard.g.cs @@ -9,11 +9,13 @@ public partial interface IGuardClient /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task GetGuardAsync( string guardName, global::System.DateTime? asOf = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuardHistory.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuardHistory.g.cs index cd315c6..d549d9d 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuardHistory.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuardHistory.g.cs @@ -9,11 +9,13 @@ public partial interface IGuardClient /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task> GetGuardHistoryAsync( string guardName, string callId, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuards.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuards.g.cs index 699b890..f749448 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuards.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.GetGuards.g.cs @@ -7,9 +7,11 @@ public partial interface IGuardClient /// /// Fetches the configuration for all Guards the user has access to. /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task> GetGuardsAsync( + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.UpdateGuard.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.UpdateGuard.g.cs index 296f05a..d949e96 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.UpdateGuard.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.UpdateGuard.g.cs @@ -9,12 +9,14 @@ public partial interface IGuardClient /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task UpdateGuardAsync( string guardName, global::Guardrails.Guard request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); /// /// Updates a Guard @@ -30,6 +32,7 @@ public partial interface IGuardClient /// A description that concisely states the expected behaviour or purpose of the Guard. /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task UpdateGuardAsync( @@ -38,6 +41,7 @@ public partial interface IGuardClient string name, string? description = default, global::System.Collections.Generic.IList? validators = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.g.cs index 35204e9..d465c26 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardClient.g.cs @@ -33,6 +33,11 @@ public partial interface IGuardClient : global::System.IDisposable /// public bool ReadResponseAsString { get; set; } + /// + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.IGuardrailsClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.IGuardrailsClient.g.cs index 1a9d4a9..475fdd3 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IGuardrailsClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IGuardrailsClient.g.cs @@ -34,6 +34,11 @@ public partial interface IGuardrailsClient : global::System.IDisposable /// public bool ReadResponseAsString { get; set; } + /// + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.OpenaiChatCompletion.g.cs b/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.OpenaiChatCompletion.g.cs index db2df16..120324d 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.OpenaiChatCompletion.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.OpenaiChatCompletion.g.cs @@ -9,12 +9,14 @@ public partial interface IOpenaiClient /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task OpenaiChatCompletionAsync( string guardName, global::Guardrails.OpenAIChatCompletionPayload request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); /// /// OpenAI SDK compatible endpoint for Chat Completions @@ -33,6 +35,7 @@ public partial interface IOpenaiClient /// /// The sampling temperature /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task OpenaiChatCompletionAsync( @@ -41,6 +44,7 @@ public partial interface IOpenaiClient global::System.Collections.Generic.IList? messages = default, int? maxTokens = default, double? temperature = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.g.cs index 5057803..e2ff756 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IOpenaiClient.g.cs @@ -33,6 +33,11 @@ public partial interface IOpenaiClient : global::System.IDisposable /// public bool ReadResponseAsString { get; set; } + /// + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.HealthCheck.g.cs b/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.HealthCheck.g.cs index 251856f..963bb15 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.HealthCheck.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.HealthCheck.g.cs @@ -7,9 +7,11 @@ public partial interface IServiceHealthClient /// /// Returns the status of the server /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task HealthCheckAsync( + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.g.cs index 2c0f5a8..cbea179 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IServiceHealthClient.g.cs @@ -33,6 +33,11 @@ public partial interface IServiceHealthClient : global::System.IDisposable /// public bool ReadResponseAsString { get; set; } + /// + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.IValidateClient.Validate.g.cs b/src/libs/Guardrails/Generated/Guardrails.IValidateClient.Validate.g.cs index 5d3f94f..60d161a 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IValidateClient.Validate.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IValidateClient.Validate.g.cs @@ -10,6 +10,7 @@ public partial interface IValidateClient /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task ValidateAsync( @@ -17,6 +18,7 @@ public partial interface IValidateClient global::Guardrails.ValidatePayload request, string? xOpenaiApiKey = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); /// /// Runs the validations specified in a Guard @@ -36,6 +38,7 @@ public partial interface IValidateClient /// /// The LLM API to use for validation /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// global::System.Threading.Tasks.Task ValidateAsync( @@ -45,6 +48,7 @@ public partial interface IValidateClient int? numReasks = default, object? promptParams = default, global::Guardrails.ValidatePayloadLlmApi? llmApi = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.IValidateClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.IValidateClient.g.cs index 25370ed..084370f 100644 --- a/src/libs/Guardrails/Generated/Guardrails.IValidateClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.IValidateClient.g.cs @@ -33,6 +33,11 @@ public partial interface IValidateClient : global::System.IDisposable /// public bool ReadResponseAsString { get; set; } + /// + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.OpenaiChatCompletion.g.cs b/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.OpenaiChatCompletion.g.cs index de25581..297b8bc 100644 --- a/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.OpenaiChatCompletion.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.OpenaiChatCompletion.g.cs @@ -14,6 +14,7 @@ public partial class OpenaiClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -47,12 +48,14 @@ partial void ProcessOpenaiChatCompletionResponseContent( /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task OpenaiChatCompletionAsync( string guardName, global::Guardrails.OpenAIChatCompletionPayload request, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { request = request ?? throw new global::System.ArgumentNullException(nameof(request)); @@ -70,22 +73,43 @@ partial void ProcessOpenaiChatCompletionResponseContent( securityRequirements: s_OpenaiChatCompletionSecurityRequirements, operationName: "OpenaiChatCompletionAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}/openai/v1/chat/completions", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Post, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}/openai/v1/chat/completions", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Post, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -95,157 +119,316 @@ partial void ProcessOpenaiChatCompletionResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } - var __httpRequestContentBody = request.ToJson(JsonSerializerContext); - var __httpRequestContent = new global::System.Net.Http.StringContent( - content: __httpRequestContentBody, - encoding: global::System.Text.Encoding.UTF8, - mediaType: "application/json"); - __httpRequest.Content = __httpRequestContent; - - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareOpenaiChatCompletionRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName, - request: request); + var __httpRequestContentBody = request.ToJson(JsonSerializerContext); + var __httpRequestContent = new global::System.Net.Http.StringContent( + content: __httpRequestContentBody, + encoding: global::System.Text.Encoding.UTF8, + mediaType: "application/json"); + __httpRequest.Content = __httpRequestContent; + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareOpenaiChatCompletionRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName, + request: request); - ProcessResponse( - client: HttpClient, - response: __response); - ProcessOpenaiChatCompletionResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "OpenaiChatCompletion", + methodName: "OpenaiChatCompletionAsync", + pathTemplate: "$\"/guards/{guardName}/openai/v1/chat/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "OpenaiChatCompletion", + methodName: "OpenaiChatCompletionAsync", + pathTemplate: "$\"/guards/{guardName}/openai/v1/chat/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "OpenaiChatCompletion", + methodName: "OpenaiChatCompletionAsync", + pathTemplate: "$\"/guards/{guardName}/openai/v1/chat/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessOpenaiChatCompletionResponseContent( + response: __response); + ProcessOpenaiChatCompletionResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.OpenAIChatCompletion.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "OpenaiChatCompletion", + methodName: "OpenaiChatCompletionAsync", + pathTemplate: "$\"/guards/{guardName}/openai/v1/chat/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "OpenaiChatCompletion", + methodName: "OpenaiChatCompletionAsync", + pathTemplate: "$\"/guards/{guardName}/openai/v1/chat/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.OpenAIChatCompletion.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessOpenaiChatCompletionResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.OpenAIChatCompletion.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.OpenAIChatCompletion.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } /// /// OpenAI SDK compatible endpoint for Chat Completions @@ -264,6 +447,7 @@ partial void ProcessOpenaiChatCompletionResponseContent( /// /// The sampling temperature /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task OpenaiChatCompletionAsync( @@ -272,6 +456,7 @@ partial void ProcessOpenaiChatCompletionResponseContent( global::System.Collections.Generic.IList? messages = default, int? maxTokens = default, double? temperature = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { var __request = new global::Guardrails.OpenAIChatCompletionPayload @@ -285,6 +470,7 @@ partial void ProcessOpenaiChatCompletionResponseContent( return await OpenaiChatCompletionAsync( guardName: guardName, request: __request, + requestOptions: requestOptions, cancellationToken: cancellationToken).ConfigureAwait(false); } } diff --git a/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.g.cs index f1dfd08..d5a5698 100644 --- a/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.OpenaiClient.g.cs @@ -30,6 +30,9 @@ public sealed partial class OpenaiClient : global::Guardrails.IOpenaiClient, glo #if DEBUG = true; #endif + + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } /// /// /// @@ -49,11 +52,37 @@ public OpenaiClient( global::System.Net.Http.HttpClient? httpClient = null, global::System.Uri? baseUri = null, global::System.Collections.Generic.List? authorizations = null, + bool disposeHttpClient = true) : this( + httpClient, + baseUri, + authorizations, + options: null, + disposeHttpClient: disposeHttpClient) + { + } + + /// + /// Creates a new instance of the OpenaiClient. + /// If no httpClient is provided, a new one will be created. + /// If no baseUri is provided, the default baseUri from OpenAPI spec will be used. + /// + /// The HttpClient instance. If not provided, a new one will be created. + /// The base URL for the API. If not provided, the default baseUri from OpenAPI spec will be used. + /// The authorizations to use for the requests. + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// Dispose the HttpClient when the instance is disposed. True by default. + public OpenaiClient( + global::System.Net.Http.HttpClient? httpClient = null, + global::System.Uri? baseUri = null, + global::System.Collections.Generic.List? authorizations = null, + global::Guardrails.AutoSDKClientOptions? options = null, bool disposeHttpClient = true) { + HttpClient = httpClient ?? new global::System.Net.Http.HttpClient(); HttpClient.BaseAddress ??= baseUri ?? new global::System.Uri(DefaultBaseUrl); Authorizations = authorizations ?? new global::System.Collections.Generic.List(); + Options = options ?? new global::Guardrails.AutoSDKClientOptions(); _disposeHttpClient = disposeHttpClient; Initialized(HttpClient); diff --git a/src/libs/Guardrails/Generated/Guardrails.OptionsSupport.g.cs b/src/libs/Guardrails/Generated/Guardrails.OptionsSupport.g.cs new file mode 100644 index 0000000..7433275 --- /dev/null +++ b/src/libs/Guardrails/Generated/Guardrails.OptionsSupport.g.cs @@ -0,0 +1,460 @@ + +#nullable enable + +namespace Guardrails +{ + /// + /// Global defaults applied to generated SDK requests. + /// + public sealed class AutoSDKClientOptions + { + /// + /// Additional headers applied to every request after generated headers are set. + /// Entries with the same key overwrite earlier header values. + /// + public global::System.Collections.Generic.Dictionary Headers { get; } = + new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase); + + /// + /// Additional query parameters appended to every request. + /// Request-level entries with the same key are appended after client defaults. + /// + public global::System.Collections.Generic.Dictionary QueryParameters { get; } = + new global::System.Collections.Generic.Dictionary(global::System.StringComparer.Ordinal); + + /// + /// Optional timeout applied to the full request execution. + /// + public global::System.TimeSpan? Timeout { get; set; } + + /// + /// Default retry behavior for generated HTTP requests. + /// + public global::Guardrails.AutoSDKRetryOptions Retry { get; set; } = new global::Guardrails.AutoSDKRetryOptions(); + + /// + /// Overrides the client-wide response buffering mode when set. + /// + public bool? ReadResponseAsString { get; set; } + + /// + /// Reusable hooks invoked for every generated SDK request. + /// + public global::System.Collections.Generic.List Hooks { get; } = + new global::System.Collections.Generic.List(); + + /// + /// Registers a hook for all requests issued by this client. + /// + /// + /// The current options instance. + public global::Guardrails.AutoSDKClientOptions AddHook( + global::Guardrails.IAutoSDKHook hook) + { + Hooks.Add(hook ?? throw new global::System.ArgumentNullException(nameof(hook))); + return this; + } + } + + /// + /// Per-request overrides applied on top of . + /// + public sealed class AutoSDKRequestOptions + { + /// + /// Additional headers applied after generated and client-level headers. + /// + public global::System.Collections.Generic.Dictionary Headers { get; } = + new global::System.Collections.Generic.Dictionary(global::System.StringComparer.OrdinalIgnoreCase); + + /// + /// Additional query parameters appended after generated and client-level query parameters. + /// + public global::System.Collections.Generic.Dictionary QueryParameters { get; } = + new global::System.Collections.Generic.Dictionary(global::System.StringComparer.Ordinal); + + /// + /// Optional timeout override for this request. + /// + public global::System.TimeSpan? Timeout { get; set; } + + /// + /// Optional retry override for this request. + /// + public global::Guardrails.AutoSDKRetryOptions? Retry { get; set; } + + /// + /// Overrides response buffering for this request when set. + /// + public bool? ReadResponseAsString { get; set; } + } + + /// + /// Retry settings for generated HTTP requests. + /// + public sealed class AutoSDKRetryOptions + { + /// + /// Total number of attempts, including the initial request. + /// Values less than 1 are normalized to 1. + /// + public int MaxAttempts { get; set; } = 1; + + /// + /// Optional fixed delay between retry attempts. + /// + public global::System.TimeSpan? Delay { get; set; } + } + + /// + /// Runtime hook interface for generated SDK lifecycle events. + /// + public interface IAutoSDKHook + { + /// + /// Runs before a request is sent. + /// + /// + global::System.Threading.Tasks.Task OnBeforeRequestAsync( + global::Guardrails.AutoSDKHookContext context); + + /// + /// Runs after a successful HTTP response is received. + /// + /// + global::System.Threading.Tasks.Task OnAfterSuccessAsync( + global::Guardrails.AutoSDKHookContext context); + + /// + /// Runs after an error response or transport failure is observed. + /// + /// + global::System.Threading.Tasks.Task OnAfterErrorAsync( + global::Guardrails.AutoSDKHookContext context); + } + + /// + /// Convenience base type for request hooks with no-op defaults. + /// + public abstract class AutoSDKHook : global::Guardrails.IAutoSDKHook + { + /// + public virtual global::System.Threading.Tasks.Task OnBeforeRequestAsync( + global::Guardrails.AutoSDKHookContext context) + { + return global::System.Threading.Tasks.Task.CompletedTask; + } + + /// + public virtual global::System.Threading.Tasks.Task OnAfterSuccessAsync( + global::Guardrails.AutoSDKHookContext context) + { + return global::System.Threading.Tasks.Task.CompletedTask; + } + + /// + public virtual global::System.Threading.Tasks.Task OnAfterErrorAsync( + global::Guardrails.AutoSDKHookContext context) + { + return global::System.Threading.Tasks.Task.CompletedTask; + } + } + + /// + /// Runtime metadata passed to generated SDK hooks. + /// + public sealed class AutoSDKHookContext + { + /// + /// The source OpenAPI operation id or generated fallback id. + /// + public string OperationId { get; set; } = string.Empty; + + /// + /// The generated C# method name. + /// + public string MethodName { get; set; } = string.Empty; + + /// + /// The OpenAPI path template for the operation. + /// + public string PathTemplate { get; set; } = string.Empty; + + /// + /// The HTTP method used for the request. + /// + public string HttpMethod { get; set; } = string.Empty; + + /// + /// The client's resolved base URI. + /// + public global::System.Uri? BaseUri { get; set; } + + /// + /// The outgoing HTTP request for the current attempt. + /// + public global::System.Net.Http.HttpRequestMessage Request { get; set; } = null!; + + /// + /// The HTTP response when one was received. + /// + public global::System.Net.Http.HttpResponseMessage? Response { get; set; } + + /// + /// The transport or processing exception when one was observed. + /// + public global::System.Exception? Exception { get; set; } + + /// + /// The client-wide runtime options. + /// + public global::Guardrails.AutoSDKClientOptions ClientOptions { get; set; } = null!; + + /// + /// The per-request runtime options. + /// + public global::Guardrails.AutoSDKRequestOptions? RequestOptions { get; set; } + + /// + /// The current attempt number, starting at 1. + /// + public int Attempt { get; set; } + + /// + /// The total number of attempts allowed for this request. + /// + public int MaxAttempts { get; set; } + + /// + /// Indicates whether the generated client will retry after this hook invocation. + /// + public bool WillRetry { get; set; } + + /// + /// The effective cancellation token for the current request attempt. + /// + public global::System.Threading.CancellationToken CancellationToken { get; set; } + } + + internal static class AutoSDKRequestOptionsSupport + { + internal static global::Guardrails.AutoSDKHookContext CreateHookContext( + string operationId, + string methodName, + string pathTemplate, + string httpMethod, + global::System.Uri? baseUri, + global::System.Net.Http.HttpRequestMessage request, + global::System.Net.Http.HttpResponseMessage? response, + global::System.Exception? exception, + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKRequestOptions? requestOptions, + int attempt, + int maxAttempts, + bool willRetry, + global::System.Threading.CancellationToken cancellationToken) + { + return new global::Guardrails.AutoSDKHookContext + { + OperationId = operationId ?? string.Empty, + MethodName = methodName ?? string.Empty, + PathTemplate = pathTemplate ?? string.Empty, + HttpMethod = httpMethod ?? string.Empty, + BaseUri = baseUri, + Request = request, + Response = response, + Exception = exception, + ClientOptions = clientOptions, + RequestOptions = requestOptions, + Attempt = attempt, + MaxAttempts = maxAttempts, + WillRetry = willRetry, + CancellationToken = cancellationToken, + }; + } + + internal static global::System.Threading.Tasks.Task OnBeforeRequestAsync( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKHookContext context) + { + return InvokeHooksAsync(clientOptions, static (hook, hookContext) => hook.OnBeforeRequestAsync(hookContext), context); + } + + internal static global::System.Threading.Tasks.Task OnAfterSuccessAsync( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKHookContext context) + { + return InvokeHooksAsync(clientOptions, static (hook, hookContext) => hook.OnAfterSuccessAsync(hookContext), context); + } + + internal static global::System.Threading.Tasks.Task OnAfterErrorAsync( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKHookContext context) + { + return InvokeHooksAsync(clientOptions, static (hook, hookContext) => hook.OnAfterErrorAsync(hookContext), context); + } + + internal static bool GetReadResponseAsString( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKRequestOptions? requestOptions, + bool fallbackValue) + { + return requestOptions?.ReadResponseAsString ?? + clientOptions.ReadResponseAsString ?? + fallbackValue; + } + + internal static global::System.Threading.CancellationTokenSource? CreateTimeoutCancellationTokenSource( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKRequestOptions? requestOptions, + global::System.Threading.CancellationToken cancellationToken) + { + var timeout = requestOptions?.Timeout ?? clientOptions.Timeout; + if (!timeout.HasValue || timeout.Value <= global::System.TimeSpan.Zero) + { + return null; + } + + var cancellationTokenSource = global::System.Threading.CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + cancellationTokenSource.CancelAfter(timeout.Value); + return cancellationTokenSource; + } + + internal static int GetMaxAttempts( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKRequestOptions? requestOptions, + bool supportsRetry) + { + if (!supportsRetry) + { + return 1; + } + + var maxAttempts = requestOptions?.Retry?.MaxAttempts ?? + clientOptions.Retry?.MaxAttempts ?? + 1; + return maxAttempts < 1 ? 1 : maxAttempts; + } + + internal static async global::System.Threading.Tasks.Task DelayBeforeRetryAsync( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::Guardrails.AutoSDKRequestOptions? requestOptions, + global::System.Threading.CancellationToken cancellationToken) + { + var delay = requestOptions?.Retry?.Delay ?? + clientOptions.Retry?.Delay; + if (!delay.HasValue || delay.Value <= global::System.TimeSpan.Zero) + { + return; + } + + await global::System.Threading.Tasks.Task.Delay(delay.Value, cancellationToken).ConfigureAwait(false); + } + + internal static bool ShouldRetryStatusCode( + global::System.Net.HttpStatusCode statusCode) + { + return (int)statusCode switch + { + 408 => true, + 429 => true, + 500 => true, + 502 => true, + 503 => true, + 504 => true, + _ => false, + }; + } + + internal static string AppendQueryParameters( + string path, + global::System.Collections.Generic.Dictionary clientParameters, + global::System.Collections.Generic.Dictionary? requestParameters) + { + var hasClientParameters = clientParameters != null && clientParameters.Count > 0; + var hasRequestParameters = requestParameters != null && requestParameters.Count > 0; + if (!hasClientParameters && !hasRequestParameters) + { + return path; + } + + var builder = new global::System.Text.StringBuilder(path ?? string.Empty); + var hasQuery = builder.ToString().Contains("?", global::System.StringComparison.Ordinal); + AppendParameters(builder, clientParameters, ref hasQuery); + AppendParameters(builder, requestParameters, ref hasQuery); + return builder.ToString(); + } + + internal static void ApplyHeaders( + global::System.Net.Http.HttpRequestMessage request, + global::System.Collections.Generic.Dictionary clientHeaders, + global::System.Collections.Generic.Dictionary? requestHeaders) + { + ApplyHeadersCore(request, clientHeaders); + ApplyHeadersCore(request, requestHeaders); + } + + private static void AppendParameters( + global::System.Text.StringBuilder builder, + global::System.Collections.Generic.Dictionary? parameters, + ref bool hasQuery) + { + if (parameters == null || parameters.Count == 0) + { + return; + } + + foreach (var parameter in parameters) + { + builder.Append(hasQuery ? '&' : '?'); + builder.Append(global::System.Uri.EscapeDataString(parameter.Key)); + builder.Append('='); + builder.Append(global::System.Uri.EscapeDataString(parameter.Value ?? string.Empty)); + hasQuery = true; + } + } + + private static void ApplyHeadersCore( + global::System.Net.Http.HttpRequestMessage request, + global::System.Collections.Generic.Dictionary? headers) + { + if (headers == null || headers.Count == 0) + { + return; + } + + foreach (var header in headers) + { + request.Headers.Remove(header.Key); + request.Content?.Headers.Remove(header.Key); + + if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value ?? string.Empty) && + request.Content != null) + { + request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value ?? string.Empty); + } + } + } + + private static async global::System.Threading.Tasks.Task InvokeHooksAsync( + global::Guardrails.AutoSDKClientOptions clientOptions, + global::System.Func callback, + global::Guardrails.AutoSDKHookContext context) + { + if (clientOptions.Hooks == null || clientOptions.Hooks.Count == 0) + { + return; + } + + foreach (var hook in clientOptions.Hooks) + { + if (hook == null) + { + continue; + } + + await callback(hook, context).ConfigureAwait(false); + } + } + } +} \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.PathBuilder.g.cs b/src/libs/Guardrails/Generated/Guardrails.PathBuilder.g.cs index 7502e58..b3776d8 100644 --- a/src/libs/Guardrails/Generated/Guardrails.PathBuilder.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.PathBuilder.g.cs @@ -275,6 +275,11 @@ public class EndPointAuthorization /// public string Type { get; set; } = string.Empty; + /// + /// + /// + public string SchemeId { get; set; } = string.Empty; + /// /// /// diff --git a/src/libs/Guardrails/Generated/Guardrails.Security.g.cs b/src/libs/Guardrails/Generated/Guardrails.Security.g.cs index 9f85b04..757a4d0 100644 --- a/src/libs/Guardrails/Generated/Guardrails.Security.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.Security.g.cs @@ -6,6 +6,8 @@ internal sealed class EndPointAuthorizationRequirement { internal string Type { get; set; } = string.Empty; + internal string SchemeId { get; set; } = string.Empty; + internal string Location { get; set; } = string.Empty; internal string Name { get; set; } = string.Empty; @@ -97,7 +99,18 @@ private static bool Matches( return requiredAuthorization.Type switch { - "OAuth2" => true, + "OAuth2" => string.Equals( + availableAuthorization.SchemeId, + requiredAuthorization.SchemeId, + global::System.StringComparison.Ordinal), + "OpenIdConnect" => string.Equals( + availableAuthorization.SchemeId, + requiredAuthorization.SchemeId, + global::System.StringComparison.Ordinal), + "MutualTLS" => string.Equals( + availableAuthorization.SchemeId, + requiredAuthorization.SchemeId, + global::System.StringComparison.Ordinal), "Http" => string.Equals( availableAuthorization.Name, requiredAuthorization.Name, diff --git a/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.HealthCheck.g.cs b/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.HealthCheck.g.cs index 8106873..1ac84df 100644 --- a/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.HealthCheck.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.HealthCheck.g.cs @@ -14,6 +14,7 @@ public partial class ServiceHealthClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -41,9 +42,11 @@ partial void ProcessHealthCheckResponseContent( /// /// Returns the status of the server /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task HealthCheckAsync( + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { PrepareArguments( @@ -57,22 +60,43 @@ partial void ProcessHealthCheckResponseContent( securityRequirements: s_HealthCheckSecurityRequirements, operationName: "HealthCheckAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: "/health-check", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Get, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: "/health-check", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -82,149 +106,308 @@ partial void ProcessHealthCheckResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareHealthCheckRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareHealthCheckRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); - - ProcessResponse( - client: HttpClient, - response: __response); - ProcessHealthCheckResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "HealthCheck", + methodName: "HealthCheckAsync", + pathTemplate: "\"/health-check\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "HealthCheck", + methodName: "HealthCheckAsync", + pathTemplate: "\"/health-check\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "HealthCheck", + methodName: "HealthCheckAsync", + pathTemplate: "\"/health-check\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessHealthCheckResponseContent( + response: __response); + ProcessHealthCheckResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.HealthCheck.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "HealthCheck", + methodName: "HealthCheckAsync", + pathTemplate: "\"/health-check\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "HealthCheck", + methodName: "HealthCheckAsync", + pathTemplate: "\"/health-check\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.HealthCheck.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessHealthCheckResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.HealthCheck.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.HealthCheck.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } } } \ No newline at end of file diff --git a/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.g.cs index f7b9277..791cecc 100644 --- a/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.ServiceHealthClient.g.cs @@ -30,6 +30,9 @@ public sealed partial class ServiceHealthClient : global::Guardrails.IServiceHea #if DEBUG = true; #endif + + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } /// /// /// @@ -49,11 +52,37 @@ public ServiceHealthClient( global::System.Net.Http.HttpClient? httpClient = null, global::System.Uri? baseUri = null, global::System.Collections.Generic.List? authorizations = null, + bool disposeHttpClient = true) : this( + httpClient, + baseUri, + authorizations, + options: null, + disposeHttpClient: disposeHttpClient) + { + } + + /// + /// Creates a new instance of the ServiceHealthClient. + /// If no httpClient is provided, a new one will be created. + /// If no baseUri is provided, the default baseUri from OpenAPI spec will be used. + /// + /// The HttpClient instance. If not provided, a new one will be created. + /// The base URL for the API. If not provided, the default baseUri from OpenAPI spec will be used. + /// The authorizations to use for the requests. + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// Dispose the HttpClient when the instance is disposed. True by default. + public ServiceHealthClient( + global::System.Net.Http.HttpClient? httpClient = null, + global::System.Uri? baseUri = null, + global::System.Collections.Generic.List? authorizations = null, + global::Guardrails.AutoSDKClientOptions? options = null, bool disposeHttpClient = true) { + HttpClient = httpClient ?? new global::System.Net.Http.HttpClient(); HttpClient.BaseAddress ??= baseUri ?? new global::System.Uri(DefaultBaseUrl); Authorizations = authorizations ?? new global::System.Collections.Generic.List(); + Options = options ?? new global::Guardrails.AutoSDKClientOptions(); _disposeHttpClient = disposeHttpClient; Initialized(HttpClient); diff --git a/src/libs/Guardrails/Generated/Guardrails.ValidateClient.Validate.g.cs b/src/libs/Guardrails/Generated/Guardrails.ValidateClient.Validate.g.cs index 4a9b951..b28b1c4 100644 --- a/src/libs/Guardrails/Generated/Guardrails.ValidateClient.Validate.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.ValidateClient.Validate.g.cs @@ -14,6 +14,7 @@ public partial class ValidateClient { new global::Guardrails.EndPointAuthorizationRequirement { Type = "Http", + SchemeId = "BearerAuth", Location = "Header", Name = "Bearer", FriendlyName = "Bearer", @@ -50,6 +51,7 @@ partial void ProcessValidateResponseContent( /// /// /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task ValidateAsync( @@ -57,6 +59,7 @@ partial void ProcessValidateResponseContent( global::Guardrails.ValidatePayload request, string? xOpenaiApiKey = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { request = request ?? throw new global::System.ArgumentNullException(nameof(request)); @@ -75,22 +78,43 @@ partial void ProcessValidateResponseContent( securityRequirements: s_ValidateSecurityRequirements, operationName: "ValidateAsync"); - var __pathBuilder = new global::Guardrails.PathBuilder( - path: $"/guards/{guardName}/validate", - baseUri: HttpClient.BaseAddress); - var __path = __pathBuilder.ToString(); - using var __httpRequest = new global::System.Net.Http.HttpRequestMessage( - method: global::System.Net.Http.HttpMethod.Post, - requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); + using var __timeoutCancellationTokenSource = global::Guardrails.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Guardrails.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Guardrails.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + var __pathBuilder = new global::Guardrails.PathBuilder( + path: $"/guards/{guardName}/validate", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::Guardrails.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Post, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); #if NET6_0_OR_GREATER - __httpRequest.Version = global::System.Net.HttpVersion.Version11; - __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || - __authorization.Type == "OAuth2") + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") { __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( scheme: __authorization.Name, @@ -100,164 +124,323 @@ partial void ProcessValidateResponseContent( __authorization.Location == "Header") { __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); - } + } } - if (xOpenaiApiKey != default) - { - __httpRequest.Headers.TryAddWithoutValidation("x-openai-api-key", xOpenaiApiKey.ToString()); - } + if (xOpenaiApiKey != default) + { + __httpRequest.Headers.TryAddWithoutValidation("x-openai-api-key", xOpenaiApiKey.ToString()); + } - var __httpRequestContentBody = request.ToJson(JsonSerializerContext); - var __httpRequestContent = new global::System.Net.Http.StringContent( - content: __httpRequestContentBody, - encoding: global::System.Text.Encoding.UTF8, - mediaType: "application/json"); - __httpRequest.Content = __httpRequestContent; - - PrepareRequest( - client: HttpClient, - request: __httpRequest); - PrepareValidateRequest( - httpClient: HttpClient, - httpRequestMessage: __httpRequest, - guardName: guardName, - xOpenaiApiKey: xOpenaiApiKey, - request: request); + var __httpRequestContentBody = request.ToJson(JsonSerializerContext); + var __httpRequestContent = new global::System.Net.Http.StringContent( + content: __httpRequestContentBody, + encoding: global::System.Text.Encoding.UTF8, + mediaType: "application/json"); + __httpRequest.Content = __httpRequestContent; + global::Guardrails.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); - using var __response = await HttpClient.SendAsync( - request: __httpRequest, - completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, - cancellationToken: cancellationToken).ConfigureAwait(false); + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareValidateRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + guardName: guardName, + xOpenaiApiKey: xOpenaiApiKey, + request: request); - ProcessResponse( - client: HttpClient, - response: __response); - ProcessValidateResponse( - httpClient: HttpClient, - httpResponseMessage: __response); - // Unexpected error - if (!__response.IsSuccessStatusCode) + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try { - string? __content_default = null; - global::System.Exception? __exception_default = null; - global::Guardrails.HttpError? __value_default = null; - try + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) { - if (ReadResponseAsString) + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "Validate", + methodName: "ValidateAsync", + pathTemplate: "$\"/guards/{guardName}/validate\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); } - else + catch (global::System.Net.Http.HttpRequestException __exception) { - __content_default = await __response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "Validate", + methodName: "ValidateAsync", + pathTemplate: "$\"/guards/{guardName}/validate\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } - __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + if (__response != null && + __attempt < __maxAttempts && + global::Guardrails.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "Validate", + methodName: "ValidateAsync", + pathTemplate: "$\"/guards/{guardName}/validate\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Guardrails.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; } + + break; } - catch (global::System.Exception __ex) + + if (__response == null) { - __exception_default = __ex; + throw new global::System.InvalidOperationException("No response received."); } - throw new global::Guardrails.ApiException( - message: __content_default ?? __response.ReasonPhrase ?? string.Empty, - innerException: __exception_default, - statusCode: __response.StatusCode) + using (__response) { - ResponseBody = __content_default, - ResponseObject = __value_default, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; - } - if (ReadResponseAsString) - { - var __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - - ProcessResponseContent( + ProcessResponse( client: HttpClient, - response: __response, - content: ref __content); - ProcessValidateResponseContent( + response: __response); + ProcessValidateResponse( httpClient: HttpClient, - httpResponseMessage: __response, - content: ref __content); - - try + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) { - __response.EnsureSuccessStatusCode(); - - return - global::Guardrails.ValidationOutcome.FromJson(__content, JsonSerializerContext) ?? - throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "Validate", + methodName: "ValidateAsync", + pathTemplate: "$\"/guards/{guardName}/validate\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - catch (global::System.Exception __ex) + else { - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; + await global::Guardrails.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Guardrails.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "Validate", + methodName: "ValidateAsync", + pathTemplate: "$\"/guards/{guardName}/validate\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); } - } - else - { - try - { - __response.EnsureSuccessStatusCode(); - using var __content = await __response.Content.ReadAsStreamAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); + // Unexpected error + if (!__response.IsSuccessStatusCode) + { + string? __content_default = null; + global::System.Exception? __exception_default = null; + global::Guardrails.HttpError? __value_default = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + else + { + __content_default = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); - return - await global::Guardrails.ValidationOutcome.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? - throw new global::System.InvalidOperationException("Response deserialization failed."); - } - catch (global::System.Exception __ex) - { - string? __content = null; - try - { - __content = await __response.Content.ReadAsStringAsync( -#if NET5_0_OR_GREATER - cancellationToken -#endif - ).ConfigureAwait(false); - } - catch (global::System.Exception) - { - } + __value_default = global::Guardrails.HttpError.FromJson(__content_default, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_default = __ex; + } + + throw new global::Guardrails.ApiException( + message: __content_default ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_default, + statusCode: __response.StatusCode) + { + ResponseBody = __content_default, + ResponseObject = __value_default, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessResponseContent( + client: HttpClient, + response: __response, + content: ref __content); + ProcessValidateResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return + global::Guardrails.ValidationOutcome.FromJson(__content, JsonSerializerContext) ?? + throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" "); + } + catch (global::System.Exception __ex) + { + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + using var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return + await global::Guardrails.ValidationOutcome.FromJsonStreamAsync(__content, JsonSerializerContext).ConfigureAwait(false) ?? + throw new global::System.InvalidOperationException("Response deserialization failed."); + } + catch (global::System.Exception __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw new global::Guardrails.ApiException( + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + statusCode: __response.StatusCode) + { + ResponseBody = __content, + ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value), + }; + } + } - throw new global::Guardrails.ApiException( - message: __content ?? __response.ReasonPhrase ?? string.Empty, - innerException: __ex, - statusCode: __response.StatusCode) - { - ResponseBody = __content, - ResponseHeaders = global::System.Linq.Enumerable.ToDictionary( - __response.Headers, - h => h.Key, - h => h.Value), - }; } } + finally + { + __httpRequest?.Dispose(); + } } /// /// Runs the validations specified in a Guard @@ -277,6 +460,7 @@ partial void ProcessValidateResponseContent( /// /// The LLM API to use for validation /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. /// The token to cancel the operation with /// public async global::System.Threading.Tasks.Task ValidateAsync( @@ -286,6 +470,7 @@ partial void ProcessValidateResponseContent( int? numReasks = default, object? promptParams = default, global::Guardrails.ValidatePayloadLlmApi? llmApi = default, + global::Guardrails.AutoSDKRequestOptions? requestOptions = default, global::System.Threading.CancellationToken cancellationToken = default) { var __request = new global::Guardrails.ValidatePayload @@ -300,6 +485,7 @@ partial void ProcessValidateResponseContent( guardName: guardName, xOpenaiApiKey: xOpenaiApiKey, request: __request, + requestOptions: requestOptions, cancellationToken: cancellationToken).ConfigureAwait(false); } } diff --git a/src/libs/Guardrails/Generated/Guardrails.ValidateClient.g.cs b/src/libs/Guardrails/Generated/Guardrails.ValidateClient.g.cs index 69e8fb0..a54bab4 100644 --- a/src/libs/Guardrails/Generated/Guardrails.ValidateClient.g.cs +++ b/src/libs/Guardrails/Generated/Guardrails.ValidateClient.g.cs @@ -30,6 +30,9 @@ public sealed partial class ValidateClient : global::Guardrails.IValidateClient, #if DEBUG = true; #endif + + /// + public global::Guardrails.AutoSDKClientOptions Options { get; } /// /// /// @@ -49,11 +52,37 @@ public ValidateClient( global::System.Net.Http.HttpClient? httpClient = null, global::System.Uri? baseUri = null, global::System.Collections.Generic.List? authorizations = null, + bool disposeHttpClient = true) : this( + httpClient, + baseUri, + authorizations, + options: null, + disposeHttpClient: disposeHttpClient) + { + } + + /// + /// Creates a new instance of the ValidateClient. + /// If no httpClient is provided, a new one will be created. + /// If no baseUri is provided, the default baseUri from OpenAPI spec will be used. + /// + /// The HttpClient instance. If not provided, a new one will be created. + /// The base URL for the API. If not provided, the default baseUri from OpenAPI spec will be used. + /// The authorizations to use for the requests. + /// Client-wide request defaults such as headers, query parameters, retries, and timeout. + /// Dispose the HttpClient when the instance is disposed. True by default. + public ValidateClient( + global::System.Net.Http.HttpClient? httpClient = null, + global::System.Uri? baseUri = null, + global::System.Collections.Generic.List? authorizations = null, + global::Guardrails.AutoSDKClientOptions? options = null, bool disposeHttpClient = true) { + HttpClient = httpClient ?? new global::System.Net.Http.HttpClient(); HttpClient.BaseAddress ??= baseUri ?? new global::System.Uri(DefaultBaseUrl); Authorizations = authorizations ?? new global::System.Collections.Generic.List(); + Options = options ?? new global::Guardrails.AutoSDKClientOptions(); _disposeHttpClient = disposeHttpClient; Initialized(HttpClient);