Skip to content

Commit

Permalink
Add nullable annotations #11
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Aug 3, 2023
1 parent 500c567 commit 4a47b88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
16 changes: 4 additions & 12 deletions src/DallENet/DallEClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using DallENet.Exceptions;
using DallENet.Models;

Expand Down Expand Up @@ -56,27 +55,22 @@ public async Task<DallEImageGenerationResponse> GenerateImagesAsync(string promp
var operationLocation = httpResponse.Headers.GetValues("Operation-Location").FirstOrDefault();
var retryAfter = httpResponse.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(10);

using var responseStream = await httpResponse.Content.ReadAsStreamAsync(cancellationToken);
using var document = await JsonDocument.ParseAsync(responseStream, cancellationToken: cancellationToken);

var operationId = document.RootElement.GetProperty("id").GetString();

// Waits until the actual response (with images URL) is available.
var isRunning = true;
while (isRunning)
{
await Task.Delay(retryAfter, cancellationToken);

response = await httpClient.GetFromJsonAsync<DallEImageGenerationResponse>(operationLocation, cancellationToken);
NormalizeRenspose(response!, httpResponse, operationId);
NormalizeRenspose(response!, httpResponse);

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

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

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

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

if (!httpResponse.IsSuccessStatusCode && response.Error is null)
{
response.Error = new DallEError
Expand Down
9 changes: 3 additions & 6 deletions src/DallENet/Models/DallEImageGenerationResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using DallENet.Models.Converters;

namespace DallENet.Models;
Expand Down Expand Up @@ -30,11 +31,6 @@ public class DallEImageGenerationResponse
/// </summary>
public string Id { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the Operation Id that identifies the original image generation request.
/// </summary>
public string? OperationId { get; set; }

/// <summary>
/// Gets or sets the status of the response.
/// </summary>
Expand All @@ -51,6 +47,7 @@ public class DallEImageGenerationResponse
/// <summary>
/// Gets a value that determines if the response was successful.
/// </summary>
[MemberNotNullWhen(false, nameof(Error))]
public bool IsSuccessful => Error is null;

/// <summary>
Expand Down

0 comments on commit 4a47b88

Please sign in to comment.