Skip to content

Commit

Permalink
Merge 40dd9f7 into 3cd7df0
Browse files Browse the repository at this point in the history
  • Loading branch information
StokedPrune committed Apr 29, 2020
2 parents 3cd7df0 + 40dd9f7 commit b24265a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
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}]";
}
}
}

0 comments on commit b24265a

Please sign in to comment.