Skip to content

Commit

Permalink
Refactoring to use the EnsureSuccess extension method to ensure that …
Browse files Browse the repository at this point in the history
…HTTP requests were successful and throw HttpOperationException otherwise.
  • Loading branch information
SergeyMenshykh committed Jul 26, 2023
1 parent 3727341 commit 1ab2add
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ private async Task<IReadOnlyList<ITextStreamingResult>> ExecuteGetCompletionsAsy

var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
response.EnsureSuccess(responseContent);

List<TextCompletionResponse>? completionResponse = JsonSerializer.Deserialize<List<TextCompletionResponse>>(responseContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,7 @@ public sealed class OobaboogaTextCompletion : ITextCompletion

var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
response.EnsureSuccess(responseContent, this._logger);

TextCompletionResponse? completionResponse = JsonSerializer.Deserialize<TextCompletionResponse>(responseContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,11 @@ private protected async Task<HttpResponseMessage> ExecuteRequestAsync(string url

this._log.LogDebug("HTTP response: {0} {1}", (int)response.StatusCode, response.StatusCode.ToString("G"));

if (response.IsSuccessStatusCode)
{
return response;
}

string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

try
{
response.EnsureSuccessStatusCode();
return response;
}
catch (HttpRequestException e)
{
this._log.LogError(e, "HTTP request failed: {0} {1}", response.StatusCode, e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
response.EnsureSuccess(responseContent);

return response;
}

#endregion
Expand Down
10 changes: 1 addition & 9 deletions dotnet/src/Connectors/Connectors.Memory.Chroma/ChromaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,7 @@ public async Task<ChromaQueryResultModel> QueryEmbeddingsAsync(string collection

string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
this._logger.LogError(e, "{0} {1} operation failed: {2}, {3}", request.Method.Method, operationName, e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
response.EnsureSuccess(responseContent, this._logger);

return (response, responseContent);
}
Expand Down
92 changes: 41 additions & 51 deletions dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public PineconeClient(string pineconeEnvironment, string apiKey, ILogger? logger

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError("Error occurred on Get Vectors request: {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

FetchResponse? data = JsonSerializer.Deserialize<FetchResponse>(responseContent, this._jsonSerializerOptions);
Expand Down Expand Up @@ -115,12 +115,12 @@ public PineconeClient(string pineconeEnvironment, string apiKey, ILogger? logger

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError("Error occurred on Query Vectors request: {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

QueryResponse? queryResponse = JsonSerializer.Deserialize<QueryResponse>(responseContent, this._jsonSerializerOptions);
Expand Down Expand Up @@ -222,12 +222,12 @@ await foreach (UpsertRequest? batch in PineconeUtils.GetUpsertBatchesAsync(valid

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError("Failed to upsert vectors {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

UpsertResponse? data = JsonSerializer.Deserialize<UpsertResponse>(responseContent, this._jsonSerializerOptions);
Expand Down Expand Up @@ -282,12 +282,12 @@ await foreach (UpsertRequest? batch in PineconeUtils.GetUpsertBatchesAsync(valid

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError("Delete operation failed: {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
}

Expand All @@ -307,12 +307,12 @@ public async Task UpdateAsync(string indexName, PineconeDocument document, strin

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Vector update for Document {0} failed. Message: {1}", document.Id, e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
}

Expand All @@ -334,12 +334,12 @@ public async Task UpdateAsync(string indexName, PineconeDocument document, strin

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Index not found {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

IndexStats? result = JsonSerializer.Deserialize<IndexStats>(responseContent, this._jsonSerializerOptions);
Expand All @@ -365,12 +365,12 @@ public async Task UpdateAsync(string indexName, PineconeDocument document, strin

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Listing index names failed. Message: {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

string[]? indices = JsonSerializer.Deserialize<string[]?>(responseContent, this._jsonSerializerOptions);
Expand Down Expand Up @@ -399,22 +399,17 @@ public async Task CreateIndexAsync(IndexDefinition indexDefinition, Cancellation

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.BadRequest)
{
this._logger.LogError(e, "Bad Request: {0}, {1}", response.StatusCode, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.Conflict)
catch (HttpOperationException e) when (response.StatusCode == HttpStatusCode.Conflict)
{
this._logger.LogError(e, "Index of given name already exists: {0}, {1}", response.StatusCode, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Creating index failed: {0}, {1}", e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
}

Expand All @@ -429,17 +424,17 @@ public async Task DeleteIndexAsync(string indexName, CancellationToken cancellat

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.NotFound)
catch (HttpOperationException e) when (response.StatusCode == HttpStatusCode.NotFound)
{
this._logger.LogError(e, "Index Not Found: {0}, {1}", response.StatusCode, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Deleting index failed: {0}, {1}", e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

this._logger.LogDebug("Index: {0} has been successfully deleted.", indexName);
Expand Down Expand Up @@ -473,17 +468,12 @@ public async Task<bool> DoesIndexExistAsync(string indexName, CancellationToken

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.BadRequest)
{
this._logger.LogError(e, "Bad Request: {0}, {1}", response.StatusCode, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Describe index failed: {0}, {1}", e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

PineconeIndex? indexDescription = JsonSerializer.Deserialize<PineconeIndex>(responseContent, this._jsonSerializerOptions);
Expand Down Expand Up @@ -511,22 +501,22 @@ public async Task ConfigureIndexAsync(string indexName, int replicas = 1, PodTyp

try
{
response.EnsureSuccessStatusCode();
response.EnsureSuccess(responseContent, this._logger);
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.BadRequest)
catch (HttpOperationException e) when (response.StatusCode == HttpStatusCode.BadRequest)
{
this._logger.LogError(e, "Request exceeds quota or collection name is invalid. {0}", indexName);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
catch (HttpRequestException e) when (response.StatusCode == HttpStatusCode.NotFound)
catch (HttpOperationException e) when (response.StatusCode == HttpStatusCode.NotFound)
{
this._logger.LogError(e, "Index not found. {0}", indexName);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}
catch (HttpRequestException e)
catch (HttpOperationException e)
{
this._logger.LogError(e, "Index configuration failed: {0}, {1}", e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
throw;
}

this._logger.LogDebug("Collection created. {0}", indexName);
Expand Down
Loading

0 comments on commit 1ab2add

Please sign in to comment.