From a2bdc71b9d828c90d75d63e88ffce7eb3fde8eb4 Mon Sep 17 00:00:00 2001 From: ducksoop Date: Tue, 13 Feb 2024 10:22:16 -0600 Subject: [PATCH] feat: add the delete command to interact with delete API from qb Add the fluent delete command to delete records via Quickbase API fix #10 --- QuickbaseNet/Services/QuickbaseClient.cs | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/QuickbaseNet/Services/QuickbaseClient.cs b/QuickbaseNet/Services/QuickbaseClient.cs index 036c12b..ad6efa7 100644 --- a/QuickbaseNet/Services/QuickbaseClient.cs +++ b/QuickbaseNet/Services/QuickbaseClient.cs @@ -129,5 +129,46 @@ public async Task> UpdateRecords( // Its a 5xx error return QuickbaseResult.Failure(QuickbaseError.ServerError("QuickbaseNet.ServerError", errorResponse, "Server error")); } + + public async Task> DeleteRecords( + DeleteRecordRequest quickBaseRequest) + { + // Serialize your request object into a JSON string + var requestJson = JsonConvert.SerializeObject(quickBaseRequest); + HttpContent content = new StringContent(requestJson, Encoding.UTF8, "application/json"); + + // Create an HttpRequestMessage for DELETE + var request = new HttpRequestMessage(HttpMethod.Delete, "/v1/records") + { + Content = content + }; + + // Send the request + var response = await Client.SendAsync(request); + + // The rest of your method remains the same + if (response.IsSuccessStatusCode) + { + var jsonResponse = await response.Content.ReadAsStringAsync(); + + if (string.IsNullOrEmpty(jsonResponse)) + { + return QuickbaseResult.Failure(QuickbaseError.NotFound("QuickbaseNet.Failure", + "No records found", $"The query did not find any records matching that criteria")); + } + + return QuickbaseResult.Success(JsonConvert.DeserializeObject(jsonResponse)); + } + + var errorResponse = await response.Content.ReadAsStringAsync(); + + if (response.StatusCode >= System.Net.HttpStatusCode.BadRequest && + response.StatusCode < System.Net.HttpStatusCode.InternalServerError) + { + return QuickbaseResult.Failure(QuickbaseError.ClientError("QuickbaseNet.ClientError", errorResponse, "Client error")); + } + + return QuickbaseResult.Failure(QuickbaseError.ServerError("QuickbaseNet.ServerError", errorResponse, "Server error")); + } } }