From 26d8c55843d9838c1314ad89b461dbcfb778a160 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 25 Aug 2026 01:00:33 +0000 Subject: [PATCH] feat: Updated OpenAPI spec --- ...otoroomClient.RemoveBackgroundAsBytes.g.cs | 236 ++ ...room.PhotoroomClient.RemoveBackground.g.cs | 12 + ...otoroomClient.RemoveBackgroundAsBytes.g.cs | 2985 +++++++++++++++++ 3 files changed, 3233 insertions(+) create mode 100644 src/libs/Photoroom/Generated/Photoroom.IPhotoroomClient.RemoveBackgroundAsBytes.g.cs create mode 100644 src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackgroundAsBytes.g.cs diff --git a/src/libs/Photoroom/Generated/Photoroom.IPhotoroomClient.RemoveBackgroundAsBytes.g.cs b/src/libs/Photoroom/Generated/Photoroom.IPhotoroomClient.RemoveBackgroundAsBytes.g.cs new file mode 100644 index 0000000..2df1eec --- /dev/null +++ b/src/libs/Photoroom/Generated/Photoroom.IPhotoroomClient.RemoveBackgroundAsBytes.g.cs @@ -0,0 +1,236 @@ +#nullable enable + +namespace Photoroom +{ + public partial interface IPhotoroomClient + { + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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 RemoveBackgroundAsBytesAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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 RemoveBackgroundAsBytesAsStreamAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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> RemoveBackgroundAsBytesAsResponseAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsync( + byte[] imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsStreamAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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> RemoveBackgroundAsBytesAsResponseAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackground.g.cs b/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackground.g.cs index 46f1f1c..c098e77 100644 --- a/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackground.g.cs +++ b/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackground.g.cs @@ -135,6 +135,10 @@ partial void ProcessRemoveBackgroundResponseContent( __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || @@ -808,6 +812,10 @@ request.ImageFilename is null __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || @@ -1407,6 +1415,10 @@ request.ImageFilename is null __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || diff --git a/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackgroundAsBytes.g.cs b/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackgroundAsBytes.g.cs new file mode 100644 index 0000000..f0630e2 --- /dev/null +++ b/src/libs/Photoroom/Generated/Photoroom.PhotoroomClient.RemoveBackgroundAsBytes.g.cs @@ -0,0 +1,2985 @@ + +#nullable enable + +namespace Photoroom +{ + public partial class PhotoroomClient + { + + private static readonly global::Photoroom.AutoSDKServer[] s_RemoveBackgroundAsBytesServers = new global::Photoroom.AutoSDKServer[] + { new global::Photoroom.AutoSDKServer( + id: "https-sdk-photoroom-com", + name: "Production server (Basic plan)", + url: "https://sdk.photoroom.com/", + description: "Production server (Basic plan)"), + }; + + + private static readonly global::Photoroom.EndPointSecurityRequirement s_RemoveBackgroundAsBytesSecurityRequirement0 = + new global::Photoroom.EndPointSecurityRequirement + { + Authorizations = new global::Photoroom.EndPointAuthorizationRequirement[] + { new global::Photoroom.EndPointAuthorizationRequirement + { + Type = "ApiKey", + SchemeId = "ApikeyXApiKey", + Location = "Header", + Name = "x-api-key", + FriendlyName = "ApiKeyInHeader", + }, + }, + }; + private static readonly global::Photoroom.EndPointSecurityRequirement[] s_RemoveBackgroundAsBytesSecurityRequirements = + new global::Photoroom.EndPointSecurityRequirement[] + { s_RemoveBackgroundAsBytesSecurityRequirement0, + }; + partial void PrepareRemoveBackgroundAsBytesArguments( + global::System.Net.Http.HttpClient httpClient, + global::Photoroom.RemoveBackgroundPostParams request); + partial void PrepareRemoveBackgroundAsBytesRequest( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpRequestMessage httpRequestMessage, + global::Photoroom.RemoveBackgroundPostParams request); + partial void ProcessRemoveBackgroundAsBytesResponse( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpResponseMessage httpResponseMessage); + + partial void ProcessRemoveBackgroundAsBytesResponseContent( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpResponseMessage httpResponseMessage, + ref byte[] content); + + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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 RemoveBackgroundAsBytesAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + var __response = await RemoveBackgroundAsBytesAsResponseAsync( + + request: request, + requestOptions: requestOptions, + cancellationToken: cancellationToken + ).ConfigureAwait(false); + + return __response.Body; + } + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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 RemoveBackgroundAsBytesAsStreamAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + request = request ?? throw new global::System.ArgumentNullException(nameof(request)); + + PrepareArguments( + client: HttpClient); + PrepareRemoveBackgroundAsBytesArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::Photoroom.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_RemoveBackgroundAsBytesSecurityRequirements, + operationName: "RemoveBackgroundAsBytesAsync"); + + using var __timeoutCancellationTokenSource = global::Photoroom.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Photoroom.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Photoroom.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::Photoroom.PathBuilder( + path: "/v1/segment", + baseUri: ResolveBaseUri( + servers: s_RemoveBackgroundAsBytesServers, + defaultBaseUrl: "https://sdk.photoroom.com/")); + var __path = __pathBuilder.ToString(); + __path = global::Photoroom.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; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "image/png"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent(); + var __contentImageFile = new global::System.Net.Http.ByteArrayContent(request.ImageFile ?? global::System.Array.Empty()); + __contentImageFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue( + request.ImageFilename is null + ? "application/octet-stream" + : (global::System.IO.Path.GetExtension(request.ImageFilename) ?? string.Empty).ToLowerInvariant() switch + { + ".aac" => "audio/aac", + ".flac" => "audio/flac", + ".gif" => "image/gif", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".json" => "application/json", + ".m4a" => "audio/mp4", + ".mp3" => "audio/mpeg", + ".mp4" => "video/mp4", + ".mpeg" => "audio/mpeg", + ".mpga" => "audio/mpeg", + ".oga" => "audio/ogg", + ".ogg" => "audio/ogg", + ".opus" => "audio/ogg", + ".pdf" => "application/pdf", + ".png" => "image/png", + ".txt" => "text/plain", + ".wav" => "audio/wav", + ".weba" => "audio/webm", + ".webm" => "video/webm", + ".webp" => "image/webp", + _ => "application/octet-stream", + }); + __httpRequestContent.Add( + content: __contentImageFile, + name: "\"image_file\"", + fileName: request.ImageFilename != null ? $"\"{request.ImageFilename}\"" : string.Empty); + if (__contentImageFile.Headers.ContentDisposition != null) + { + __contentImageFile.Headers.ContentDisposition.FileNameStar = null; + } + + if (request.Format != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Format).HasValue ? (request.Format).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"format\""); + + } + if (request.Channels != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Channels).HasValue ? (request.Channels).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"channels\""); + + } + if (request.BgColor != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent(request.BgColor ?? string.Empty), + name: "\"bg_color\""); + + } + if (request.Size != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Size).HasValue ? (request.Size).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"size\""); + + } + if (request.Crop != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Crop).HasValue ? (request.Crop).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"crop\""); + + } + if (request.Despill != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Despill).HasValue ? (request.Despill).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"despill\""); + + } + + __httpRequest.Content = __httpRequestContent; + + global::Photoroom.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareRemoveBackgroundAsBytesRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseHeadersRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Photoroom.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + try + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessRemoveBackgroundAsBytesResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + // Bad Request + if ((int)__response.StatusCode == 400) + { + string? __content_400 = null; + global::System.Exception? __exception_400 = null; + global::Photoroom.RemoveBackgroundResponse2? __value_400 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + else + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_400 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_400 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_400, + responseBody: __content_400, + responseObject: __value_400, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // Payment Required + if ((int)__response.StatusCode == 402) + { + string? __content_402 = null; + global::System.Exception? __exception_402 = null; + global::Photoroom.RemoveBackgroundResponse3? __value_402 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + else + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_402 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_402 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_402, + responseBody: __content_402, + responseObject: __value_402, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // + if ((int)__response.StatusCode == 403) + { + string? __content_403 = null; + global::System.Exception? __exception_403 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + else + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + } + catch (global::System.Exception __ex) + { + __exception_403 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_403 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_403, + responseBody: __content_403, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + try + { + __response.EnsureSuccessStatusCode(); + + var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return new global::Photoroom.ResponseStream(__response, __content); + } + 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 global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + } + catch + { + __response.Dispose(); + throw; + } + } + finally + { + __httpRequest?.Dispose(); + } + } + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// 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> RemoveBackgroundAsBytesAsResponseAsync( + + global::Photoroom.RemoveBackgroundPostParams request, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + request = request ?? throw new global::System.ArgumentNullException(nameof(request)); + + PrepareArguments( + client: HttpClient); + PrepareRemoveBackgroundAsBytesArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::Photoroom.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_RemoveBackgroundAsBytesSecurityRequirements, + operationName: "RemoveBackgroundAsBytesAsync"); + + using var __timeoutCancellationTokenSource = global::Photoroom.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Photoroom.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Photoroom.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::Photoroom.PathBuilder( + path: "/v1/segment", + baseUri: ResolveBaseUri( + servers: s_RemoveBackgroundAsBytesServers, + defaultBaseUrl: "https://sdk.photoroom.com/")); + var __path = __pathBuilder.ToString(); + __path = global::Photoroom.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; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "image/png"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent(); + var __contentImageFile = new global::System.Net.Http.ByteArrayContent(request.ImageFile ?? global::System.Array.Empty()); + __contentImageFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue( + request.ImageFilename is null + ? "application/octet-stream" + : (global::System.IO.Path.GetExtension(request.ImageFilename) ?? string.Empty).ToLowerInvariant() switch + { + ".aac" => "audio/aac", + ".flac" => "audio/flac", + ".gif" => "image/gif", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".json" => "application/json", + ".m4a" => "audio/mp4", + ".mp3" => "audio/mpeg", + ".mp4" => "video/mp4", + ".mpeg" => "audio/mpeg", + ".mpga" => "audio/mpeg", + ".oga" => "audio/ogg", + ".ogg" => "audio/ogg", + ".opus" => "audio/ogg", + ".pdf" => "application/pdf", + ".png" => "image/png", + ".txt" => "text/plain", + ".wav" => "audio/wav", + ".weba" => "audio/webm", + ".webm" => "video/webm", + ".webp" => "image/webp", + _ => "application/octet-stream", + }); + __httpRequestContent.Add( + content: __contentImageFile, + name: "\"image_file\"", + fileName: request.ImageFilename != null ? $"\"{request.ImageFilename}\"" : string.Empty); + if (__contentImageFile.Headers.ContentDisposition != null) + { + __contentImageFile.Headers.ContentDisposition.FileNameStar = null; + } + + if (request.Format != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Format).HasValue ? (request.Format).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"format\""); + + } + if (request.Channels != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Channels).HasValue ? (request.Channels).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"channels\""); + + } + if (request.BgColor != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent(request.BgColor ?? string.Empty), + name: "\"bg_color\""); + + } + if (request.Size != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Size).HasValue ? (request.Size).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"size\""); + + } + if (request.Crop != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Crop).HasValue ? (request.Crop).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"crop\""); + + } + if (request.Despill != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Despill).HasValue ? (request.Despill).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"despill\""); + + } + + __httpRequest.Content = __httpRequestContent; + + global::Photoroom.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareRemoveBackgroundAsBytesRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Photoroom.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessRemoveBackgroundAsBytesResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + // Bad Request + if ((int)__response.StatusCode == 400) + { + string? __content_400 = null; + global::System.Exception? __exception_400 = null; + global::Photoroom.RemoveBackgroundResponse2? __value_400 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + else + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_400 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_400 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_400, + responseBody: __content_400, + responseObject: __value_400, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // Payment Required + if ((int)__response.StatusCode == 402) + { + string? __content_402 = null; + global::System.Exception? __exception_402 = null; + global::Photoroom.RemoveBackgroundResponse3? __value_402 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + else + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_402 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_402 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_402, + responseBody: __content_402, + responseObject: __value_402, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // + if ((int)__response.StatusCode == 403) + { + string? __content_403 = null; + global::System.Exception? __exception_403 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + else + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + } + catch (global::System.Exception __ex) + { + __exception_403 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_403 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_403, + responseBody: __content_403, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessRemoveBackgroundAsBytesResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return new global::Photoroom.AutoSDKHttpResponse( + statusCode: __response.StatusCode, + headers: global::Photoroom.AutoSDKHttpResponse.CreateHeaders(__response), + requestUri: __response.RequestMessage?.RequestUri, + body: __content); + } + catch (global::System.Exception __ex) + { + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: null, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return new global::Photoroom.AutoSDKHttpResponse( + statusCode: __response.StatusCode, + headers: global::Photoroom.AutoSDKHttpResponse.CreateHeaders(__response), + requestUri: __response.RequestMessage?.RequestUri, + body: __content); + } + 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 global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + } + + } + } + finally + { + __httpRequest?.Dispose(); + } + } + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsync( + byte[] imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + var __request = new global::Photoroom.RemoveBackgroundPostParams + { + ImageFile = imageFile, + ImageFilename = imageFilename, + Format = format, + Channels = channels, + BgColor = bgColor, + Size = size, + Crop = crop, + Despill = despill, + }; + + return await RemoveBackgroundAsBytesAsync( + request: __request, + requestOptions: requestOptions, + cancellationToken: cancellationToken).ConfigureAwait(false); + } + + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + + imageFile = imageFile ?? throw new global::System.ArgumentNullException(nameof(imageFile)); + var request = new global::Photoroom.RemoveBackgroundPostParams + { + ImageFile = global::System.Array.Empty(), + ImageFilename = imageFilename, + Format = format, + Channels = channels, + BgColor = bgColor, + Size = size, + Crop = crop, + Despill = despill, + }; + PrepareArguments( + client: HttpClient); + PrepareRemoveBackgroundAsBytesArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::Photoroom.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_RemoveBackgroundAsBytesSecurityRequirements, + operationName: "RemoveBackgroundAsBytesAsync"); + + using var __timeoutCancellationTokenSource = global::Photoroom.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Photoroom.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Photoroom.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::Photoroom.PathBuilder( + path: "/v1/segment", + baseUri: ResolveBaseUri( + servers: s_RemoveBackgroundAsBytesServers, + defaultBaseUrl: "https://sdk.photoroom.com/")); + var __path = __pathBuilder.ToString(); + __path = global::Photoroom.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; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "image/png"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent(); + var __contentImageFile = new global::System.Net.Http.StreamContent(imageFile); + __contentImageFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue( + request.ImageFilename is null + ? "application/octet-stream" + : (global::System.IO.Path.GetExtension(request.ImageFilename) ?? string.Empty).ToLowerInvariant() switch + { + ".aac" => "audio/aac", + ".flac" => "audio/flac", + ".gif" => "image/gif", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".json" => "application/json", + ".m4a" => "audio/mp4", + ".mp3" => "audio/mpeg", + ".mp4" => "video/mp4", + ".mpeg" => "audio/mpeg", + ".mpga" => "audio/mpeg", + ".oga" => "audio/ogg", + ".ogg" => "audio/ogg", + ".opus" => "audio/ogg", + ".pdf" => "application/pdf", + ".png" => "image/png", + ".txt" => "text/plain", + ".wav" => "audio/wav", + ".weba" => "audio/webm", + ".webm" => "video/webm", + ".webp" => "image/webp", + _ => "application/octet-stream", + }); + __httpRequestContent.Add( + content: __contentImageFile, + name: "\"image_file\"", + fileName: request.ImageFilename != null ? $"\"{request.ImageFilename}\"" : string.Empty); + if (__contentImageFile.Headers.ContentDisposition != null) + { + __contentImageFile.Headers.ContentDisposition.FileNameStar = null; + } + + if (request.Format != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Format).HasValue ? (request.Format).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"format\""); + + } + if (request.Channels != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Channels).HasValue ? (request.Channels).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"channels\""); + + } + if (request.BgColor != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent(request.BgColor ?? string.Empty), + name: "\"bg_color\""); + + } + if (request.Size != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Size).HasValue ? (request.Size).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"size\""); + + } + if (request.Crop != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Crop).HasValue ? (request.Crop).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"crop\""); + + } + if (request.Despill != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Despill).HasValue ? (request.Despill).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"despill\""); + + } + + __httpRequest.Content = __httpRequestContent; + + global::Photoroom.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareRemoveBackgroundAsBytesRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Photoroom.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessRemoveBackgroundAsBytesResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + // Bad Request + if ((int)__response.StatusCode == 400) + { + string? __content_400 = null; + global::System.Exception? __exception_400 = null; + global::Photoroom.RemoveBackgroundResponse2? __value_400 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + else + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_400 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_400 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_400, + responseBody: __content_400, + responseObject: __value_400, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // Payment Required + if ((int)__response.StatusCode == 402) + { + string? __content_402 = null; + global::System.Exception? __exception_402 = null; + global::Photoroom.RemoveBackgroundResponse3? __value_402 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + else + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_402 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_402 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_402, + responseBody: __content_402, + responseObject: __value_402, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // + if ((int)__response.StatusCode == 403) + { + string? __content_403 = null; + global::System.Exception? __exception_403 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + else + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + } + catch (global::System.Exception __ex) + { + __exception_403 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_403 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_403, + responseBody: __content_403, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessRemoveBackgroundAsBytesResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return __content; + } + catch (global::System.Exception __ex) + { + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: null, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return __content; + } + 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 global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + } + + } + } + finally + { + __httpRequest?.Dispose(); + } + } + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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 RemoveBackgroundAsBytesAsStreamAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + + imageFile = imageFile ?? throw new global::System.ArgumentNullException(nameof(imageFile)); + var request = new global::Photoroom.RemoveBackgroundPostParams + { + ImageFile = global::System.Array.Empty(), + ImageFilename = imageFilename, + Format = format, + Channels = channels, + BgColor = bgColor, + Size = size, + Crop = crop, + Despill = despill, + }; + PrepareArguments( + client: HttpClient); + PrepareRemoveBackgroundAsBytesArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::Photoroom.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_RemoveBackgroundAsBytesSecurityRequirements, + operationName: "RemoveBackgroundAsBytesAsync"); + + using var __timeoutCancellationTokenSource = global::Photoroom.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Photoroom.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Photoroom.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::Photoroom.PathBuilder( + path: "/v1/segment", + baseUri: ResolveBaseUri( + servers: s_RemoveBackgroundAsBytesServers, + defaultBaseUrl: "https://sdk.photoroom.com/")); + var __path = __pathBuilder.ToString(); + __path = global::Photoroom.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; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "image/png"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent(); + var __contentImageFile = new global::System.Net.Http.StreamContent(imageFile); + __contentImageFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue( + request.ImageFilename is null + ? "application/octet-stream" + : (global::System.IO.Path.GetExtension(request.ImageFilename) ?? string.Empty).ToLowerInvariant() switch + { + ".aac" => "audio/aac", + ".flac" => "audio/flac", + ".gif" => "image/gif", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".json" => "application/json", + ".m4a" => "audio/mp4", + ".mp3" => "audio/mpeg", + ".mp4" => "video/mp4", + ".mpeg" => "audio/mpeg", + ".mpga" => "audio/mpeg", + ".oga" => "audio/ogg", + ".ogg" => "audio/ogg", + ".opus" => "audio/ogg", + ".pdf" => "application/pdf", + ".png" => "image/png", + ".txt" => "text/plain", + ".wav" => "audio/wav", + ".weba" => "audio/webm", + ".webm" => "video/webm", + ".webp" => "image/webp", + _ => "application/octet-stream", + }); + __httpRequestContent.Add( + content: __contentImageFile, + name: "\"image_file\"", + fileName: request.ImageFilename != null ? $"\"{request.ImageFilename}\"" : string.Empty); + if (__contentImageFile.Headers.ContentDisposition != null) + { + __contentImageFile.Headers.ContentDisposition.FileNameStar = null; + } + + if (request.Format != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Format).HasValue ? (request.Format).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"format\""); + + } + if (request.Channels != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Channels).HasValue ? (request.Channels).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"channels\""); + + } + if (request.BgColor != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent(request.BgColor ?? string.Empty), + name: "\"bg_color\""); + + } + if (request.Size != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Size).HasValue ? (request.Size).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"size\""); + + } + if (request.Crop != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Crop).HasValue ? (request.Crop).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"crop\""); + + } + if (request.Despill != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Despill).HasValue ? (request.Despill).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"despill\""); + + } + + __httpRequest.Content = __httpRequestContent; + + global::Photoroom.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareRemoveBackgroundAsBytesRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseHeadersRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Photoroom.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + try + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessRemoveBackgroundAsBytesResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + // Bad Request + if ((int)__response.StatusCode == 400) + { + string? __content_400 = null; + global::System.Exception? __exception_400 = null; + global::Photoroom.RemoveBackgroundResponse2? __value_400 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + else + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_400 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_400 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_400, + responseBody: __content_400, + responseObject: __value_400, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // Payment Required + if ((int)__response.StatusCode == 402) + { + string? __content_402 = null; + global::System.Exception? __exception_402 = null; + global::Photoroom.RemoveBackgroundResponse3? __value_402 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + else + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_402 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_402 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_402, + responseBody: __content_402, + responseObject: __value_402, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // + if ((int)__response.StatusCode == 403) + { + string? __content_403 = null; + global::System.Exception? __exception_403 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + else + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + } + catch (global::System.Exception __ex) + { + __exception_403 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_403 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_403, + responseBody: __content_403, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + try + { + __response.EnsureSuccessStatusCode(); + + var __content = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return new global::Photoroom.ResponseStream(__response, __content); + } + 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 global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + } + catch + { + __response.Dispose(); + throw; + } + } + finally + { + __httpRequest?.Dispose(); + } + } + /// + /// Remove Background (Basic plan)
+ /// Remove background from an image + ///
+ /// + /// The image file to render + /// + /// + /// The image file to render + /// + /// + /// The format of the resulting image
+ /// Default Value: png + /// + /// + /// The channels of the resulting image
+ /// Default Value: rgba + /// + /// + /// The background color of the resulting image. Can be a hex code (`#FF00FF`) or a HTML color (`red`, `green`, etc.) + /// + /// + /// Will resize the output to the specified size. Can be `preview` (0.25 Megapixels), `medium` (1.5 MP), `hd` (4 MP) or `full` (36 MP, can be slower for large images). Useful for mobile apps that need smaller images.
+ /// Default Value: full + /// + /// + /// If `true`, the image returned is cropped to the cutout border. Transparent pixels are removed from the border
+ /// Default Value: false + /// + /// + /// If `true`, automatically removes colored reflections that have been left on the main subject by a green background.
+ /// Default Value: false + /// + /// 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> RemoveBackgroundAsBytesAsResponseAsync( + global::System.IO.Stream imageFile, + string imageFilename, + global::Photoroom.RemoveBackgroundPostParamsFormat? format = default, + global::Photoroom.RemoveBackgroundPostParamsChannels? channels = default, + string? bgColor = default, + global::Photoroom.RemoveBackgroundPostParamsSize? size = default, + global::Photoroom.RemoveBackgroundPostParamsCrop? crop = default, + global::Photoroom.RemoveBackgroundPostParamsDespill? despill = default, + global::Photoroom.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default) + { + + imageFile = imageFile ?? throw new global::System.ArgumentNullException(nameof(imageFile)); + var request = new global::Photoroom.RemoveBackgroundPostParams + { + ImageFile = global::System.Array.Empty(), + ImageFilename = imageFilename, + Format = format, + Channels = channels, + BgColor = bgColor, + Size = size, + Crop = crop, + Despill = despill, + }; + PrepareArguments( + client: HttpClient); + PrepareRemoveBackgroundAsBytesArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::Photoroom.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_RemoveBackgroundAsBytesSecurityRequirements, + operationName: "RemoveBackgroundAsBytesAsync"); + + using var __timeoutCancellationTokenSource = global::Photoroom.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::Photoroom.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::Photoroom.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::Photoroom.PathBuilder( + path: "/v1/segment", + baseUri: ResolveBaseUri( + servers: s_RemoveBackgroundAsBytesServers, + defaultBaseUrl: "https://sdk.photoroom.com/")); + var __path = __pathBuilder.ToString(); + __path = global::Photoroom.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; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "image/png"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent(); + var __contentImageFile = new global::System.Net.Http.StreamContent(imageFile); + __contentImageFile.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue( + request.ImageFilename is null + ? "application/octet-stream" + : (global::System.IO.Path.GetExtension(request.ImageFilename) ?? string.Empty).ToLowerInvariant() switch + { + ".aac" => "audio/aac", + ".flac" => "audio/flac", + ".gif" => "image/gif", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".json" => "application/json", + ".m4a" => "audio/mp4", + ".mp3" => "audio/mpeg", + ".mp4" => "video/mp4", + ".mpeg" => "audio/mpeg", + ".mpga" => "audio/mpeg", + ".oga" => "audio/ogg", + ".ogg" => "audio/ogg", + ".opus" => "audio/ogg", + ".pdf" => "application/pdf", + ".png" => "image/png", + ".txt" => "text/plain", + ".wav" => "audio/wav", + ".weba" => "audio/webm", + ".webm" => "video/webm", + ".webp" => "image/webp", + _ => "application/octet-stream", + }); + __httpRequestContent.Add( + content: __contentImageFile, + name: "\"image_file\"", + fileName: request.ImageFilename != null ? $"\"{request.ImageFilename}\"" : string.Empty); + if (__contentImageFile.Headers.ContentDisposition != null) + { + __contentImageFile.Headers.ContentDisposition.FileNameStar = null; + } + + if (request.Format != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Format).HasValue ? (request.Format).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"format\""); + + } + if (request.Channels != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Channels).HasValue ? (request.Channels).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"channels\""); + + } + if (request.BgColor != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent(request.BgColor ?? string.Empty), + name: "\"bg_color\""); + + } + if (request.Size != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Size).HasValue ? (request.Size).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"size\""); + + } + if (request.Crop != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Crop).HasValue ? (request.Crop).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"crop\""); + + } + if (request.Despill != default) + { + + __httpRequestContent.Add( + content: new global::System.Net.Http.StringContent((request.Despill).HasValue ? (request.Despill).GetValueOrDefault().ToValueString() : string.Empty), + name: "\"despill\""); + + } + + __httpRequest.Content = __httpRequestContent; + + global::Photoroom.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareRemoveBackgroundAsBytesRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseContentRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::Photoroom.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::Photoroom.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::Photoroom.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessRemoveBackgroundAsBytesResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::Photoroom.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::Photoroom.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "RemoveBackgroundAsBytes", + methodName: "RemoveBackgroundAsBytesAsync", + pathTemplate: "\"/v1/segment\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest!, + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + // Bad Request + if ((int)__response.StatusCode == 400) + { + string? __content_400 = null; + global::System.Exception? __exception_400 = null; + global::Photoroom.RemoveBackgroundResponse2? __value_400 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + else + { + __content_400 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_400 = global::Photoroom.RemoveBackgroundResponse2.FromJson(__content_400, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_400 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_400 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_400, + responseBody: __content_400, + responseObject: __value_400, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // Payment Required + if ((int)__response.StatusCode == 402) + { + string? __content_402 = null; + global::System.Exception? __exception_402 = null; + global::Photoroom.RemoveBackgroundResponse3? __value_402 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + else + { + __content_402 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + + __value_402 = global::Photoroom.RemoveBackgroundResponse3.FromJson(__content_402, JsonSerializerContext); + } + } + catch (global::System.Exception __ex) + { + __exception_402 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_402 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_402, + responseBody: __content_402, + responseObject: __value_402, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + // + if ((int)__response.StatusCode == 403) + { + string? __content_403 = null; + global::System.Exception? __exception_403 = null; + try + { + if (__effectiveReadResponseAsString) + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + else + { + __content_403 = await __response.Content.ReadAsStringAsync(__effectiveCancellationToken).ConfigureAwait(false); + } + } + catch (global::System.Exception __ex) + { + __exception_403 = __ex; + } + + + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content_403 ?? __response.ReasonPhrase ?? string.Empty, + innerException: __exception_403, + responseBody: __content_403, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + if (__effectiveReadResponseAsString) + { + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + ProcessRemoveBackgroundAsBytesResponseContent( + httpClient: HttpClient, + httpResponseMessage: __response, + content: ref __content); + + try + { + __response.EnsureSuccessStatusCode(); + + return new global::Photoroom.AutoSDKHttpResponse( + statusCode: __response.StatusCode, + headers: global::Photoroom.AutoSDKHttpResponse.CreateHeaders(__response), + requestUri: __response.RequestMessage?.RequestUri, + body: __content); + } + catch (global::System.Exception __ex) + { + throw global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: null, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + } + else + { + try + { + __response.EnsureSuccessStatusCode(); + var __content = await __response.Content.ReadAsByteArrayAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + return new global::Photoroom.AutoSDKHttpResponse( + statusCode: __response.StatusCode, + headers: global::Photoroom.AutoSDKHttpResponse.CreateHeaders(__response), + requestUri: __response.RequestMessage?.RequestUri, + body: __content); + } + 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 global::Photoroom.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + 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