Skip to content

Commit

Permalink
Little optmization
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Jul 21, 2023
1 parent 1a36267 commit 500c567
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/DallENet/DallEClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ public async Task<DallEImageGenerationResponse> GenerateImagesAsync(string promp
await Task.Delay(retryAfter, cancellationToken);

response = await httpClient.GetFromJsonAsync<DallEImageGenerationResponse>(operationLocation, cancellationToken);
EnsureErrorIsSet(response!, httpResponse);
response!.OperationId = operationId ?? string.Empty;
NormalizeRenspose(response!, httpResponse, operationId);

isRunning = response.Status is "notRunning" or "running";
isRunning = response!.Status is "notRunning" or "running";
}
}
else
{
response = await httpResponse.Content.ReadFromJsonAsync<DallEImageGenerationResponse>(cancellationToken: cancellationToken);
EnsureErrorIsSet(response!, httpResponse);
NormalizeRenspose(response!, httpResponse, null);
}

if (!response!.IsSuccessful && options.ThrowExceptionOnError)
Expand All @@ -96,7 +95,7 @@ public async Task DeleteImagesAsync(string operationId, CancellationToken cancel
if (!httpResponse.IsSuccessStatusCode)
{
var response = await httpResponse.Content.ReadFromJsonAsync<DallEImageGenerationResponse>(cancellationToken: cancellationToken);
EnsureErrorIsSet(response!, httpResponse);
NormalizeRenspose(response!, httpResponse, operationId);

throw new DallEException(response!.Error, httpResponse.StatusCode);
}
Expand All @@ -110,8 +109,10 @@ private DallEImageGenerationRequest CreateRequest(string prompt, int? imageCount
ImageCount = imageCount ?? options.DefaultImageCount
};

private static void EnsureErrorIsSet(DallEImageGenerationResponse response, HttpResponseMessage httpResponse)
private static void NormalizeRenspose(DallEImageGenerationResponse response, HttpResponseMessage httpResponse, string? operationId)
{
response.OperationId = operationId;

if (!httpResponse.IsSuccessStatusCode && response.Error is null)
{
response.Error = new DallEError
Expand Down
11 changes: 1 addition & 10 deletions src/DallENet/Models/Converters/UnixToDateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed)
{
var date = UnixTimeStampToDateTime(number);
var date = DateTimeOffset.FromUnixTimeSeconds(number).UtcDateTime;
return date;
}
}
Expand All @@ -36,15 +36,6 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
}

return DateTime.MinValue;

static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimeStamp).ToUniversalTime();

return dateTime;
}
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
Expand Down
2 changes: 1 addition & 1 deletion src/DallENet/Models/DallEImageGenerationResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DallEImageGenerationResponse
/// <summary>
/// Gets or sets the Operation Id that identifies the original image generation request.
/// </summary>
public string OperationId { get; set; } = string.Empty;
public string? OperationId { get; set; }

/// <summary>
/// Gets or sets the status of the response.
Expand Down

0 comments on commit 500c567

Please sign in to comment.