Skip to content

Commit

Permalink
feat: Initial MultiPart form data support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 5, 2024
1 parent dc3592a commit 7dbccc1
Show file tree
Hide file tree
Showing 31 changed files with 15,730 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ public static bool IsDateTime(

return schema.Type == "string" && schema.Format == "date-time";
}

public static bool IsBinary(
this OpenApiSchema schema)
{
schema = schema ?? throw new ArgumentNullException(nameof(schema));

return schema.Type == "string" && schema.Format == "binary";
}

public static bool IsComponent(
this KeyValuePair<string, OpenApiSchema> schema)
Expand Down
25 changes: 25 additions & 0 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,31 @@ public static string GenerateRequestData(
}

var jsonSerializer = endPoint.Settings.JsonSerializerType.GetSerializer();
if (endPoint.IsMultipartFormData)
{
return $@"
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
{endPoint.Properties.Where(x => !x.IsMultiPartFormDataFilename).Select(x => x.Type.IsBinary ? @$"
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.{x.Name} ?? global::System.Array.Empty<byte>())
{{
Headers =
{{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse(""{endPoint.RequestMediaType}""),
}},
}},
name: ""{x.Id}"",
fileName: request.{x.Name + "name"} ?? string.Empty);
" : @$"
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($""{{request.{x.Name}}}""),
name: ""{x.Id}"");
").Inject()}
httpRequest.Content = __httpRequestContent;
".RemoveBlankLinesWhereOnlyWhitespaces();
}

var requestContent = endPoint.RequestType.IsBase64
? "global::System.Convert.ToBase64String(request)"
: jsonSerializer.GenerateSerializeCall(endPoint.RequestType, endPoint.Settings.JsonSerializerContext);
Expand Down
3 changes: 2 additions & 1 deletion src/libs/OpenApiGenerator.Core/Models/ModelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static ModelData FromSchema(
operationId: string.Empty,
settings: settings,
parents: innerParents);
if (x.Value.Type == "string" && x.Value.Format == "binary")
if (x.Value.IsBinary())
{
return new []
{
Expand All @@ -156,6 +156,7 @@ property with
{
Id = property.Id + "name",
Name = property.Name + "name",
IsMultiPartFormDataFilename = true,
Type = TypeData.Default with
{
CSharpType = property.IsRequired ? "string" : "string?",
Expand Down
3 changes: 3 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public readonly record struct PropertyData(
string Name,
TypeData Type,
bool IsRequired,
bool IsMultiPartFormDataFilename,
ParameterLocation? ParameterLocation,
ParameterStyle? ParameterStyle,
bool? ParameterExplode,
Expand All @@ -23,6 +24,7 @@ public readonly record struct PropertyData(
Name: string.Empty,
Type: TypeData.Default,
IsRequired: false,
IsMultiPartFormDataFilename: false,
ParameterLocation: null,
ParameterStyle: null,
ParameterExplode: null,
Expand Down Expand Up @@ -65,6 +67,7 @@ public static PropertyData FromSchema(
Name: name,
Type: type,
IsRequired: requiredProperties.Contains(schema.Key),
IsMultiPartFormDataFilename: false,
ParameterLocation: parameterLocation,
ParameterStyle: parameterStyle,
ParameterExplode: parameterExplode,
Expand Down
4 changes: 4 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/TypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public readonly record struct TypeData(
bool IsBase64,
bool IsDate,
bool IsDateTime,
bool IsBinary,
int AnyOfCount,
int OneOfCount,
int AllOfCount,
Expand All @@ -29,6 +30,7 @@ public readonly record struct TypeData(
IsBase64: false,
IsDate: false,
IsDateTime: false,
IsBinary: false,
AnyOfCount: 0,
OneOfCount: 0,
AllOfCount: 0,
Expand Down Expand Up @@ -95,6 +97,7 @@ public static TypeData FromSchema(
IsBase64: schema.Value.IsBase64(),
IsDate: schema.Value.IsDate(),
IsDateTime: schema.Value.IsDateTime(),
IsBinary: schema.Value.IsBinary(),
AnyOfCount: schema.Value.AnyOf?.Count ?? 0,
OneOfCount: schema.Value.OneOf?.Count ?? 0,
AllOfCount: schema.Value.AllOf?.Count ?? 0,
Expand Down Expand Up @@ -138,6 +141,7 @@ public static TypeData FromSchemaContext(SchemaContext context, Settings setting
IsBase64: context.Schema.IsBase64(),
IsDate: context.Schema.IsDate(),
IsDateTime: context.Schema.IsDateTime(),
IsBinary: context.Schema.IsBinary(),
AnyOfCount: context.Schema.AnyOf?.Count ?? 0,
OneOfCount: context.Schema.OneOf?.Count ?? 0,
AllOfCount: context.Schema.AllOf?.Count ?? 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,35 @@ partial void ProcessCreateTranscriptionResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/audio/transcriptions", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.File ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "file",
fileName: request.Filename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Model}"),
name: "model");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Language}"),
name: "language");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Prompt}"),
name: "prompt");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.ResponseFormat}"),
name: "response_format");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Temperature}"),
name: "temperature");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.TimestampGranularities}"),
name: "timestamp_granularities[]");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,29 @@ partial void ProcessCreateTranslationResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/audio/translations", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.File ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "file",
fileName: request.Filename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Model}"),
name: "model");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Prompt}"),
name: "prompt");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.ResponseFormat}"),
name: "response_format");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Temperature}"),
name: "temperature");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ partial void ProcessCreateFileResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/files", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.File ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "file",
fileName: request.Filename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Purpose}"),
name: "purpose");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,45 @@ partial void ProcessCreateImageEditResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/images/edits", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.Image ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "image",
fileName: request.Imagename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Prompt}"),
name: "prompt");
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.Mask ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "mask",
fileName: request.Maskname ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Model}"),
name: "model");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.N}"),
name: "n");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Size}"),
name: "size");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.ResponseFormat}"),
name: "response_format");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.User}"),
name: "user");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,32 @@ partial void ProcessCreateImageVariationResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/images/variations", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.Image ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "image",
fileName: request.Imagename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Model}"),
name: "model");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.N}"),
name: "n");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.ResponseFormat}"),
name: "response_format");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Size}"),
name: "size");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.User}"),
name: "user");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,35 @@ partial void ProcessCreateTranscriptionResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/audio/transcriptions", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: global::System.Text.Json.JsonSerializer.Serialize(request, _jsonSerializerOptions),
encoding: global::System.Text.Encoding.UTF8,
mediaType: "multipart/form-data");
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
__httpRequestContent.Add(
content: new global::System.Net.Http.ByteArrayContent(request.File ?? global::System.Array.Empty<byte>())
{
Headers =
{
ContentType = global::System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data"),
},
},
name: "file",
fileName: request.Filename ?? string.Empty);
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Model}"),
name: "model");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Language}"),
name: "language");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Prompt}"),
name: "prompt");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.ResponseFormat}"),
name: "response_format");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.Temperature}"),
name: "temperature");
__httpRequestContent.Add(
content: new global::System.Net.Http.StringContent($"{request.TimestampGranularities}"),
name: "timestamp_granularities[]");
httpRequest.Content = __httpRequestContent;

PrepareRequest(
Expand Down
Loading

0 comments on commit 7dbccc1

Please sign in to comment.