Skip to content

Commit

Permalink
fix: Use ApiException instead System.InvalidOperationException in met…
Browse files Browse the repository at this point in the history
…hods.
  • Loading branch information
HavenDV committed Nov 12, 2024
1 parent e56b911 commit 9624480
Show file tree
Hide file tree
Showing 9,795 changed files with 149,437 additions and 18,824 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
10 changes: 6 additions & 4 deletions src/libs/AutoSDK/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ public static EndPoint FromSchema(OperationContext operation)
Path: preparedPath,
RequestMediaType: requestMediaType,
Parameters: parameters.ToImmutableArray(),
SuccessResponse: responses.Any(x => x.IsSuccess)
? responses.First(x => x.IsSuccess)
: EndPointResponse.Default,
ErrorResponses: responses.Where(x => !x.IsSuccess).ToImmutableArray(),
SuccessResponse: responses.Any(x => x.Is2XX)
? responses.First(x => x.Is2XX)
: responses.Any(x => x.IsDefault)
? responses.First(x => x.IsDefault)
: EndPointResponse.Default,
ErrorResponses: responses.Where(x => !x.Is2XX).ToImmutableArray(),
QueryParameters: queryParameters.ToImmutableArray(),
Authorizations: authorizations,
HttpMethod: operation.OperationType,
Expand Down
17 changes: 10 additions & 7 deletions src/libs/AutoSDK/Models/EndPointResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
namespace AutoSDK.Models;

public readonly record struct EndPointResponse(
bool IsSuccess,
bool IsDefault,
string StatusCode,
string Description,
string MimeType,
ContentType ContentType,
TypeData Type
)
{
public bool Is1XX => StatusCode.StartsWith("1", StringComparison.OrdinalIgnoreCase);
public bool Is2XX => StatusCode.StartsWith("2", StringComparison.OrdinalIgnoreCase);
public bool Is3XX => StatusCode.StartsWith("3", StringComparison.OrdinalIgnoreCase);
public bool Is4XX => StatusCode.StartsWith("4", StringComparison.OrdinalIgnoreCase);
public bool Is5XX => StatusCode.StartsWith("5", StringComparison.OrdinalIgnoreCase);
public bool IsDefault => StatusCode == "default";

public static EndPointResponse Default => new(
IsSuccess: true,
IsDefault: false,
StatusCode: "200",
Description: string.Empty,
MimeType: string.Empty,
ContentType: ContentType.String,
Type: TypeData.Default);
Expand All @@ -35,7 +40,6 @@ public static EndPointResponse FromResponse(KeyValuePair<string, OpenApiResponse
{
return Default with
{
IsSuccess = responseWithStatusCode.Key.StartsWith("2", StringComparison.OrdinalIgnoreCase),
StatusCode = responseWithStatusCode.Key,
};
}
Expand Down Expand Up @@ -65,9 +69,8 @@ public static EndPointResponse FromResponse(KeyValuePair<string, OpenApiResponse
}

var endPoint = new EndPointResponse(
IsSuccess: responseWithStatusCode.Key.StartsWith("2", StringComparison.OrdinalIgnoreCase),
IsDefault: responseWithStatusCode.Key == "default",
StatusCode: responseWithStatusCode.Key,
Description: response.Response.Description,
MimeType: response.MimeType,
ContentType: contentType,
Type: responseType ?? TypeData.Default);
Expand Down
86 changes: 73 additions & 13 deletions src/libs/AutoSDK/Sources/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static string GenerateMethod(
{(string.IsNullOrWhiteSpace(endPoint.RequestType.CSharpType) ? " " : @"
/// <param name=""request""></param>")}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
/// <exception cref=""global::{endPoint.Settings.Namespace}.ApiException""></exception>
{(endPoint.IsDeprecated ? "[global::System.Obsolete(\"This method marked as deprecated.\")]" : " ")}
{(endPoint.Settings.UseExperimentalAttributes is SdkFeatureUsage.Always or SdkFeatureUsage.InSupportedTargetFrameworks &&
!string.IsNullOrWhiteSpace(endPoint.ExperimentalStage)
Expand Down Expand Up @@ -324,8 +324,24 @@ public static string GenerateResponse(
{
if (string.IsNullOrWhiteSpace(endPoint.SuccessResponse.Type.CSharpType))
{
return @"
__response.EnsureSuccessStatusCode();
return $@"
try
{{
__response.EnsureSuccessStatusCode();
}}
catch (global::System.Net.Http.HttpRequestException __ex)
{{
throw new global::{endPoint.Settings.Namespace}.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
}};
}}
";
}

Expand All @@ -337,7 +353,23 @@ public static string GenerateResponse(
if (endPoint.Stream)
{
return $@"
__response.EnsureSuccessStatusCode();
try
{{
__response.EnsureSuccessStatusCode();
}}
catch (global::System.Net.Http.HttpRequestException __ex)
{{
throw new global::{endPoint.Settings.Namespace}.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
}};
}}
using var __stream = await __response.Content.ReadAsStreamAsync({cancellationTokenInsideReadAsync}).ConfigureAwait(false);
using var __reader = new global::System.IO.StreamReader(__stream);
Expand All @@ -361,6 +393,7 @@ public static string GenerateResponse(
};

var errors = endPoint.Settings.GenerateExceptions ? endPoint.ErrorResponses.Select(x => $@"
// {x.Description.Replace('\n', ' ').Replace('\r', ' ')}
{(x.IsDefault ? @"
if (!__response.IsSuccessStatusCode)" : @$"
if ((int)__response.StatusCode == {x.StatusCode})")}
Expand Down Expand Up @@ -413,16 +446,27 @@ public static string GenerateResponse(
httpResponseMessage: __response,
content: ref __content);
{(endPoint.ContentType == ContentType.String ? @"
try
{
{{
__response.EnsureSuccessStatusCode();
}
}}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::System.InvalidOperationException(__content, __ex);
}" : @"
__response.EnsureSuccessStatusCode();")}
{{
throw new global::{endPoint.Settings.Namespace}.ApiException(
{(endPoint.ContentType == ContentType.String ? $@"
message: __content ?? __response.ReasonPhrase ?? string.Empty," : @"
message: __response.ReasonPhrase ?? string.Empty,")}
innerException: __ex,
statusCode: __response.StatusCode)
{{
{(endPoint.ContentType == ContentType.String ? $@"
ResponseBody = __content," : " ")}
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
}};
}}
{(endPoint.ContentType == ContentType.String && endPoint.SuccessResponse.Type.CSharpTypeWithoutNullability is not "string" ? $@"
return
Expand All @@ -432,8 +476,24 @@ public static string GenerateResponse(
}}
else
{{
__response.EnsureSuccessStatusCode();
try
{{
__response.EnsureSuccessStatusCode();
}}
catch (global::System.Net.Http.HttpRequestException __ex)
{{
throw new global::{endPoint.Settings.Namespace}.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
}};
}}
using var __responseStream = await __response.Content.ReadAsStreamAsync({cancellationTokenInsideReadAsync}).ConfigureAwait(false);
var __responseValue = {jsonSerializer.GenerateDeserializeFromStreamCall("__responseStream", endPoint.SuccessResponse.Type, endPoint.Settings.JsonSerializerContext)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ partial void ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponseConte
/// </param>
/// <param name="request"></param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
/// <exception cref="global::G.ApiException"></exception>
public async global::System.Threading.Tasks.Task<string> ConvertDocumentFileStudioV1ChatFilesConvertPostAsync(
global::G.BodyConvertDocumentFileStudioV1ChatFilesConvertPost request,
int? requestStartTime = default,
Expand Down Expand Up @@ -89,6 +89,7 @@ partial void ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponseConte
ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponse(
httpClient: HttpClient,
httpResponseMessage: __response);
// Validation Error
if ((int)__response.StatusCode == 422)
{
string? __content_422 = null;
Expand Down Expand Up @@ -136,14 +137,41 @@ partial void ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponseConte
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::System.InvalidOperationException(__content, __ex);
throw new global::G.ApiException(
message: __content ?? __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseBody = __content,
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

return __content;
}
else
{
__response.EnsureSuccessStatusCode();
try
{
__response.EnsureSuccessStatusCode();
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::G.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

using var __responseStream = await __response.Content.ReadAsStreamAsync().ConfigureAwait(false);

var __responseValue = global::Newtonsoft.Json.JsonSerializer.Create(JsonSerializerOptions).Deserialize<string?>(new global::Newtonsoft.Json.JsonTextReader(new global::System.IO.StreamReader(__responseStream)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ partial void ProcessGenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganization
/// Default Value: 1730898830008
/// </param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
/// <exception cref="global::G.ApiException"></exception>
public async global::System.Threading.Tasks.Task<global::G.ConnectorsToken> GenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganizationIdTokensPostAsync(
string organizationId,
int? requestStartTime = default,
Expand Down Expand Up @@ -76,6 +76,7 @@ partial void ProcessGenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganization
ProcessGenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganizationIdTokensPostResponse(
httpClient: HttpClient,
httpResponseMessage: __response);
// Validation Error
if ((int)__response.StatusCode == 422)
{
string? __content_422 = null;
Expand Down Expand Up @@ -123,7 +124,17 @@ partial void ProcessGenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganization
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::System.InvalidOperationException(__content, __ex);
throw new global::G.ApiException(
message: __content ?? __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseBody = __content,
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

return
Expand All @@ -132,7 +143,24 @@ partial void ProcessGenerateOrgTokenStudioV1ConnectorsConnectedUsersOrganization
}
else
{
__response.EnsureSuccessStatusCode();
try
{
__response.EnsureSuccessStatusCode();
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::G.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

using var __responseStream = await __response.Content.ReadAsStreamAsync().ConfigureAwait(false);

var __responseValue = await global::G.ConnectorsToken.FromJsonStreamAsync(__responseStream, JsonSerializerOptions).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ partial void ProcessGetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizat
/// Default Value: 1730898830008
/// </param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
/// <exception cref="global::System.InvalidOperationException"></exception>
/// <exception cref="global::G.ApiException"></exception>
public async global::System.Threading.Tasks.Task<global::G.ConnectorsStatus> GetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizationIdDataSourcesDataSourceStatusGetAsync(
string organizationId,
string dataSource,
Expand Down Expand Up @@ -82,6 +82,7 @@ partial void ProcessGetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizat
ProcessGetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizationIdDataSourcesDataSourceStatusGetResponse(
httpClient: HttpClient,
httpResponseMessage: __response);
// Validation Error
if ((int)__response.StatusCode == 422)
{
string? __content_422 = null;
Expand Down Expand Up @@ -129,7 +130,17 @@ partial void ProcessGetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizat
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::System.InvalidOperationException(__content, __ex);
throw new global::G.ApiException(
message: __content ?? __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseBody = __content,
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

return
Expand All @@ -138,7 +149,24 @@ partial void ProcessGetDataSourceStatusStudioV1ConnectorsConnectedUsersOrganizat
}
else
{
__response.EnsureSuccessStatusCode();
try
{
__response.EnsureSuccessStatusCode();
}
catch (global::System.Net.Http.HttpRequestException __ex)
{
throw new global::G.ApiException(
message: __response.ReasonPhrase ?? string.Empty,
innerException: __ex,
statusCode: __response.StatusCode)
{
ResponseHeaders = global::System.Linq.Enumerable.ToDictionary(
__response.Headers,
h => h.Key,
h => h.Value),
};
}

using var __responseStream = await __response.Content.ReadAsStreamAsync().ConfigureAwait(false);

var __responseValue = await global::G.ConnectorsStatus.FromJsonStreamAsync(__responseStream, JsonSerializerOptions).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 9624480

Please sign in to comment.