Implemented post put patch and delete request
All checks were successful
Build / build (push) Successful in 40s

This commit is contained in:
Ankitkumar Satapara
2026-04-29 14:29:21 +05:30
parent 7d0b7c3009
commit a9a3c385eb
2 changed files with 58 additions and 7 deletions

View File

@@ -683,7 +683,19 @@ namespace Nodify.Calculator.Execution.Handlers
? apiVm.OperationType?.ToLower() ?? "get" ? apiVm.OperationType?.ToLower() ?? "get"
: "get"; : "get";
var res = ctx.Executor.GetResponsePublic(url, httpMethod); // Read the request body if present (POST/PUT/PATCH have a Body input — Square/Grid)
string body = null;
var bodyInput = node.Input.FirstOrDefault(i =>
(i.Title ?? "").StartsWith("Body", StringComparison.OrdinalIgnoreCase)
&& i.Shape != ConnectorShape.Triangle);
if (bodyInput != null)
{
body = ctx.ReadInput(bodyInput);
if (!string.IsNullOrWhiteSpace(body))
ctx.Log($"[API] Request body: {(body.Length > 150 ? body.Substring(0, 150) + "..." : body)}");
}
var res = ctx.Executor.GetResponsePublic(url, httpMethod, body);
if (!string.IsNullOrEmpty(res)) if (!string.IsNullOrEmpty(res))
{ {
ctx.Outputs[node.NodeId] = res; ctx.Outputs[node.NodeId] = res;

View File

@@ -509,7 +509,8 @@ namespace Nodify.Calculator
internal void ExecuteFunctionPublic(FunctionOperationViewModel funcOp, ICollection<ConnectionViewModel> connections) internal void ExecuteFunctionPublic(FunctionOperationViewModel funcOp, ICollection<ConnectionViewModel> connections)
=> ExecuteFunction(funcOp, connections); => ExecuteFunction(funcOp, connections);
internal string GetResponsePublic(string url, string type) => GetResponse(url, type); internal string GetResponsePublic(string url, string type) => GetResponse(url, type, null);
internal string GetResponsePublic(string url, string type, string body) => GetResponse(url, type, body);
internal bool TraverseChainPublic(OperationViewModel node, string endNodeTitle, internal bool TraverseChainPublic(OperationViewModel node, string endNodeTitle,
ICollection<ConnectionViewModel> connections, ICollection<ConnectionViewModel> connections,
@@ -716,21 +717,59 @@ namespace Nodify.Calculator
OnLogMe?.Invoke($"Function '{funcOp.FunctionName}' execution completed."); OnLogMe?.Invoke($"Function '{funcOp.FunctionName}' execution completed.");
} }
private string GetResponse(string url, string type) private string GetResponse(string url, string type, string body)
{ {
string baseURL = !string.IsNullOrWhiteSpace(_authBaseUrl) ? _authBaseUrl : "https://localhost:7107"; string baseURL = !string.IsNullOrWhiteSpace(_authBaseUrl) ? _authBaseUrl : "https://localhost:7107";
string responseString = string.Empty; string responseString = string.Empty;
#if NET8_0_OR_GREATER #if NET8_0_OR_GREATER
if (string.Equals(type, "get", StringComparison.OrdinalIgnoreCase)) var fullUri = new Uri(new Uri(baseURL.EndsWith("/") ? baseURL : baseURL + "/"), url);
var methodLower = (type ?? "get").Trim().ToLowerInvariant();
HttpMethod httpMethod = methodLower switch
{ {
var fullUri = new Uri(new Uri(baseURL.EndsWith("/") ? baseURL : baseURL + "/"), url); "post" => HttpMethod.Post,
using var request = new HttpRequestMessage(HttpMethod.Get, fullUri); "put" => HttpMethod.Put,
"patch" => new HttpMethod("PATCH"),
"delete" => HttpMethod.Delete,
_ => HttpMethod.Get
};
try
{
using var request = new HttpRequestMessage(httpMethod, fullUri);
ApplyAuthHeaders(request); ApplyAuthHeaders(request);
// Attach body for write methods
if (methodLower != "get" && methodLower != "delete" && !string.IsNullOrWhiteSpace(body))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
else if (methodLower == "delete" && !string.IsNullOrWhiteSpace(body))
{
// DELETE may optionally carry a body
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
OnLogMe?.Invoke($"[HTTP] {httpMethod.Method} {fullUri}");
if (!string.IsNullOrWhiteSpace(body))
OnLogMe?.Invoke($"[HTTP] Body: {(body.Length > 200 ? body.Substring(0, 200) + "..." : body)}");
using var response = _httpClient.Send(request); using var response = _httpClient.Send(request);
response.EnsureSuccessStatusCode();
using var reader = new StreamReader(response.Content.ReadAsStream()); using var reader = new StreamReader(response.Content.ReadAsStream());
responseString = reader.ReadToEnd(); responseString = reader.ReadToEnd();
if (!response.IsSuccessStatusCode)
{
OnLogMe?.Invoke($"[HTTP] {(int)response.StatusCode} {response.StatusCode} — {responseString}", logType.Error);
}
else
{
OnLogMe?.Invoke($"[HTTP] {(int)response.StatusCode} {response.StatusCode}");
}
}
catch (Exception ex)
{
OnLogMe?.Invoke($"[HTTP] Request failed: {ex.Message}", logType.Error);
} }
#endif #endif