From ebce592c201cd87c1301de054eac0b3592ddce80 Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Fri, 6 Oct 2023 22:34:18 +0100 Subject: [PATCH] closes#277 Fix: implemented EnsureSuccessStatusCode after before reading responses. --- OpenAI.SDK/Extensions/HttpclientExtensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/OpenAI.SDK/Extensions/HttpclientExtensions.cs b/OpenAI.SDK/Extensions/HttpclientExtensions.cs index ecaa5379..d1e7aa26 100644 --- a/OpenAI.SDK/Extensions/HttpclientExtensions.cs +++ b/OpenAI.SDK/Extensions/HttpclientExtensions.cs @@ -13,6 +13,7 @@ public static async Task PostAndReadAsAsync(this HttpClien { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } @@ -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; } @@ -62,18 +64,21 @@ private static HttpRequestMessage CreatePostEventStreamRequest(string uri, HttpC public static async Task PostFileAndReadAsAsync(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(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } public static async Task 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 DeleteAndReadAsAsync(this HttpClient client, string uri, CancellationToken cancellationToken = default) { var response = await client.DeleteAsync(uri, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } @@ -99,6 +104,7 @@ public static async Task ReadAsByteArrayAsync(this HttpContent content, public static async Task GetStreamAsync(this HttpClient client, string requestUri, CancellationToken cancellationToken) { var response = await client.GetAsync(requestUri, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStreamAsync(cancellationToken); }