Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed deleteRow method to use Upsert and added a helper class (Payl… #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions SODA/SodaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,52 @@ public SodaResult DeleteRow(string rowId, string resourceId)
if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(password))
throw new InvalidOperationException("Write operations require an authenticated client.");

var uri = SodaUri.ForResourceAPI(Host, resourceId, rowId);
//Get the unique identifier field
var metaData = GetMetadata(resourceId);
var idFieldName = metaData.RowIdentifierField;

var request = new SodaRequest(uri, "DELETE", AppToken, Username, password);
//construct the json payload
string jsonPayload = PayloadBuilder.GetDeletePayload(idFieldName, rowId);

return request.ParseResponse<SodaResult>();
return Upsert(jsonPayload, SodaDataFormat.JSON, resourceId);
}


/// <summary>
/// Delete multiple rows using the provided list of id's and resource identifier.
/// </summary>
/// <param name="rowIds">A List of identifiers of the rows to be deleted.</param>
/// <param name="resourceId">The identifier (4x4) for a resource on the Socrata host to target.</param>
/// <returns>A <see cref="SodaResult"/> indicating success or failure.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if the specified <paramref name="resourceId"/> does not match the Socrata 4x4 pattern.</exception>
/// <exception cref="System.InvalidOperationException">Thrown if this SodaClient was initialized without authentication credentials.</exception>
public SodaResult DeleteRow(List<string> rowIds, string resourceId)
{

if (rowIds.Count == 0)
return new SodaResult()
{
Message = "No row identifiers provided to delete"
};

if (FourByFour.IsNotValid(resourceId))
throw new ArgumentOutOfRangeException("resourceId", "The provided resourceId is not a valid Socrata (4x4) resource identifier.");

if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(password))
throw new InvalidOperationException("Write operations require an authenticated client.");

//Get the unique identifier field
var metaData = GetMetadata(resourceId);
var idFieldName = metaData.RowIdentifierField;

//construct the json payload
string jsonPayload = PayloadBuilder.GetDeletePayload(idFieldName, rowIds);

return Upsert(jsonPayload, SodaDataFormat.JSON, resourceId);

}


/// <summary>
/// Send an HTTP GET request to the specified URI and intepret the result as TResult.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions SODA/Utilities/PayloadBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SODA.Utilities
{
/// <summary>
/// Helper class for constructing payload for post requests
/// </summary>
class PayloadBuilder
{
/// <summary>
/// Construct JSON payload to delete a single row using Upsert
/// </summary>
/// <param name="idFieldName">The name of the unique id field for the resource.</param>
/// <param name="rowId">The identifier of the row to be deleted.</param>
/// <returns>A json array string for submitting to the Upsert method</returns>
public static string GetDeletePayload(string IdFieldName,string rowId)
{
return $"[{{\"{IdFieldName}\": \"{rowId}\",\":deleted\": true }}]";
}

/// <summary>
/// Construct JSON payload to delete multiple rows using Upsert
/// </summary>
/// <param name="idFieldName">The name of the unique id field for the resource.</param>
/// <param name="rowIds">List of row identifiers to be deleted.</param>
/// <returns>A json array string for submitting to the Upsert method</returns>
public static string GetDeletePayload(string idFieldName, List<string> rowIds)
{
string jsonPayload = "[";
foreach (var rowId in rowIds)
{
jsonPayload = $"{jsonPayload}{{\"{idFieldName}\": \"{rowId}\",\":deleted\": true }},";
}

jsonPayload = jsonPayload.TrimEnd(',');
return $"{jsonPayload}]";
}
}
}