Skip to content

Commit

Permalink
Merge pull request #389 from betalgo/277-invalid-exception-is-thrown-…
Browse files Browse the repository at this point in the history
…when-server-is-unavailable

closes#277 Fix: implemented EnsureSuccessStatusCode after before read…
  • Loading branch information
kayhantolga authored Oct 17, 2023
2 parents c9e40ba + ebce592 commit aa257e6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions OpenAI.SDK/Extensions/HttpclientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static async Task<TResponse> PostAndReadAsAsync<TResponse>(this HttpClien
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
}, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken) ?? throw new InvalidOperationException();
}

Expand Down Expand Up @@ -47,6 +48,7 @@ private static HttpResponseMessage SendRequestPreNet6(HttpClient client, HttpReq
{
var responseTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
var response = responseTask.GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
return response;
}

Expand All @@ -62,18 +64,21 @@ private static HttpRequestMessage CreatePostEventStreamRequest(string uri, HttpC
public static async Task<TResponse> PostFileAndReadAsAsync<TResponse>(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default)
{
var response = await client.PostAsync(uri, content, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken) ?? throw new InvalidOperationException();
}

public static async Task<string> PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default)
{
var response = await client.PostAsync(uri, content, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync(cancellationToken) ?? throw new InvalidOperationException();
}

public static async Task<TResponse> DeleteAndReadAsAsync<TResponse>(this HttpClient client, string uri, CancellationToken cancellationToken = default)
{
var response = await client.DeleteAsync(uri, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken) ?? throw new InvalidOperationException();
}

Expand All @@ -99,6 +104,7 @@ public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent content,
public static async Task<Stream> GetStreamAsync(this HttpClient client, string requestUri, CancellationToken cancellationToken)
{
var response = await client.GetAsync(requestUri, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStreamAsync(cancellationToken);
}

Expand Down

0 comments on commit aa257e6

Please sign in to comment.