Skip to content

Commit

Permalink
Added new request methods for passing JSON as string
Browse files Browse the repository at this point in the history
  • Loading branch information
bgmulinari committed Jul 22, 2021
1 parent 64413d6 commit e9ef327
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
38 changes: 38 additions & 0 deletions B1SLayer/B1SLayer.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions B1SLayer/SLRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ public async Task<T> PostAsync<T>(object data, bool unwrapCollection = true)
});
}

/// <summary>
/// Performs a POST request with the provided parameters and returns the result in the specified <see cref="Type"/>.
/// </summary>
/// <param name="data">
/// The JSON string to be sent as the request body.
/// </param>
/// <typeparam name="T">
/// The object type for the result to be deserialized into.
/// </typeparam>
/// <param name="unwrapCollection">
/// Whether the result should be unwrapped from the 'value' JSON array in case it is a collection.
/// </param>
public async Task<T> PostStringAsync<T>(string data, bool unwrapCollection = true)
{
return await _slConnection.ExecuteRequest(async () =>
{
string stringResult = await FlurlRequest.WithCookies(_slConnection.Cookies).PostStringAsync(data).ReceiveString();
var jObject = JObject.Parse(stringResult);

if (unwrapCollection)
{
// Checks if the result is a collection by selecting the "value" token
var valueCollection = jObject.SelectToken("value");
return valueCollection == null ? jObject.ToObject<T>() : valueCollection.ToObject<T>();
}
else
{
return jObject.ToObject<T>();
}
});
}

/// <summary>
/// Performs a POST request with the provided parameters and returns the result in the specified <see cref="Type"/>.
/// </summary>
Expand Down Expand Up @@ -208,6 +240,20 @@ await _slConnection.ExecuteRequest(async () =>
});
}

/// <summary>
/// Performs a POST request with the provided parameters.
/// </summary>
/// <param name="data">
/// The JSON string to be sent as the request body.
/// </param>
public async Task PostStringAsync(string data)
{
await _slConnection.ExecuteRequest(async () =>
{
return await FlurlRequest.WithCookies(_slConnection.Cookies).PostStringAsync(data);
});
}

/// <summary>
/// Performs a POST request with the provided parameters.
/// </summary>
Expand All @@ -233,6 +279,20 @@ await _slConnection.ExecuteRequest(async () =>
});
}

/// <summary>
/// Performs a PATCH request with the provided parameters.
/// </summary>
/// <param name="data">
/// The JSON string to be sent as the request body.
/// </param>
public async Task PatchStringAsync(string data)
{
await _slConnection.ExecuteRequest(async () =>
{
return await FlurlRequest.WithCookies(_slConnection.Cookies).PatchStringAsync(data);
});
}

/// <summary>
/// Performs a PATCH request with the provided file.
/// </summary>
Expand Down Expand Up @@ -295,6 +355,20 @@ await _slConnection.ExecuteRequest(async () =>
});
}

/// <summary>
/// Performs a PUT request with the provided parameters.
/// </summary>
/// <param name="data">
/// The JSON string to be sent as the request body.
/// </param>
public async Task PutStringAsync(string data)
{
await _slConnection.ExecuteRequest(async () =>
{
return await FlurlRequest.WithCookies(_slConnection.Cookies).PutStringAsync(data);
});
}

/// <summary>
/// Performs a DELETE request with the provided parameters.
/// </summary>
Expand Down

0 comments on commit e9ef327

Please sign in to comment.