diff --git a/BasicNodes/ExtensionMethods.cs b/BasicNodes/ExtensionMethods.cs deleted file mode 100644 index 1ee4b6ab..00000000 --- a/BasicNodes/ExtensionMethods.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace FileFlows.BasicNodes; - -internal static class ExtensionMethods -{ - public static string? EmptyAsNull(this string str) - { - return str == string.Empty ? null : str; - } - public static void AddOrUpdate(this Dictionary dict, string key, object value) - { - if (dict.ContainsKey(key)) - dict[key] = value; - else - dict.Add(key, value); - } -} diff --git a/BasicNodes/FlowFailure.cs b/BasicNodes/FlowFailure.cs index a097c2b8..e10a36f3 100644 --- a/BasicNodes/FlowFailure.cs +++ b/BasicNodes/FlowFailure.cs @@ -44,12 +44,12 @@ Flow: {flowName} Node: {failedNode} {logNoDates}"; - Variables.AddOrUpdate("fail.Node", failedNode); - Variables.AddOrUpdate("fail.Flow", flowName); - Variables.AddOrUpdate("fail.Message", message); - Variables.AddOrUpdate("fail.MessageNoDates", messageNoDates); - Variables.AddOrUpdate("fail.Log", log); - Variables.AddOrUpdate("fail.LogNoDates", logNoDates); + Variables["fail.Node"] = failedNode; + Variables["fail.Flow"] = flowName; + Variables["fail.Message"] = message; + Variables["fail.MessageNoDates"] = messageNoDates; + Variables["fail.Log"] = log; + Variables["fail.LogNoDates"] = logNoDates; args.UpdateVariables(Variables); return 1; } diff --git a/BasicNodes/GlobalUsings.cs b/BasicNodes/GlobalUsings.cs index b34b58cc..d886856a 100644 --- a/BasicNodes/GlobalUsings.cs +++ b/BasicNodes/GlobalUsings.cs @@ -4,6 +4,7 @@ global using System.Linq; global using System.Net.Http; global using System.Threading; global using System.Threading.Tasks; +global using FileFlows.Common; #if(DEBUG) global using PluginTestLibrary; #endif \ No newline at end of file diff --git a/BasicNodes/Tools/WebRequest.cs b/BasicNodes/Tools/WebRequest.cs deleted file mode 100644 index 9b76813e..00000000 --- a/BasicNodes/Tools/WebRequest.cs +++ /dev/null @@ -1,191 +0,0 @@ -namespace FileFlows.BasicNodes.Tools; - -using FileFlows.Plugin; -using FileFlows.Plugin.Attributes; -using System; -using System.Text; - -/// -/// Flow element that makes a web request -/// -public class WebRequest : Node -{ - /// - public override int Inputs => 1; - /// - public override int Outputs => 2; - /// - public override FlowElementType Type => FlowElementType.Communication; - /// - public override bool FailureNode => true; - /// - public override string Icon => "fas fa-globe"; - /// - public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/web-request"; - - /// - public override bool Obsolete => true; - - /// - public override string ObsoleteMessage => - "This flow element has been replaced by the Web Request flow element in the Web plugin. This flow element will be removed in a future update."; - - /// - /// Gets or sets the URL - /// - [TextVariable(1)] - public string Url { get; set; } - - /// - /// Gets or sets the method - /// - [Select(nameof(MethodOptions), 2)] - public string Method { get; set; } - - private static List? _MethodOptions; - - /// - /// Gets the method options - /// - public static List MethodOptions - { - get - { - if (_MethodOptions == null) - { - _MethodOptions = new List - { - new () { Label = "GET", Value = "GET"}, - new () { Label = "POST", Value = "POST"}, - new () { Label = "PUT", Value = "PUT"}, - new () { Label = "DELETE", Value = "DELETE"}, - }; - } - return _MethodOptions; - } - } - - /// - /// Gets or sets the content type - /// - [Select(nameof(ContentTypeOptions), 3)] - public string ContentType { get; set; } - - private static List? _ContentTypeOptions; - /// - /// Gets the content type options - /// - public static List ContentTypeOptions - { - get - { - if (_ContentTypeOptions == null) - { - _ContentTypeOptions = new List - { - new () { Label = "None", Value = ""}, - new () { Label = "JSON", Value = "application/json"}, - new () { Label = "Form Data", Value = "application/x-www-form-urlencoded"}, - }; - } - return _ContentTypeOptions; - } - } - - - /// - /// Gest or sets any header - /// - [KeyValue(4, null)] - public List>? Headers { get; set; } - - /// - /// Gets or sets the body of the request - /// - [TextArea(5, variables: true)] - public string Body { get; set; } - - - private Dictionary? _Variables; - public override Dictionary Variables => _Variables; - - /// - /// Initialises a new instace of the web request - /// - public WebRequest() - { - _Variables = new Dictionary() - { - { "web.StatusCode", 200 }, - { "web.Body", "this is a sample body" } - }; - } - - /// - public override int Execute(NodeParameters args) - { - try - { - using var client = new HttpClient(); - - - string url = VariablesHelper.ReplaceVariables(this.Url, args.Variables, true, false, encoder: (string input) => - { - if (string.IsNullOrEmpty(input)) - return string.Empty; - return Uri.EscapeDataString(input); - }); - - HttpMethod method = this.Method switch - { - "POST" => HttpMethod.Post, - "PUT" => HttpMethod.Put, - "DELETE" => HttpMethod.Delete, - _ => HttpMethod.Get - }; - args.Logger.ILog("Requesting: [" + method + "] " + url); - HttpRequestMessage message = new HttpRequestMessage(method, url); - - if(this.Headers?.Any() == true) - { - foreach(var header in this.Headers) - { - if (string.IsNullOrEmpty(header.Key) || string.IsNullOrEmpty(header.Value)) - continue; - - message.Headers.Add(header.Key, header.Value); - } - } - - if (string.IsNullOrEmpty(this.ContentType) == false && method != HttpMethod.Get && string.IsNullOrWhiteSpace(this.Body) == false) - { - string body = args.ReplaceVariables(this.Body, stripMissing: false); - message.Content = new StringContent(body, Encoding.UTF8, this.ContentType?.EmptyAsNull() ?? "application/json"); - } - - var result = client.Send(message); - - string stringBody = result.Content.ReadAsStringAsync().Result ?? string.Empty; - - args.UpdateVariables(new Dictionary{ - { "web.StatusCode", (int)result.StatusCode }, - { "web.Body", stringBody } - }); - - if (result.IsSuccessStatusCode == false) - { - args.Logger.WLog("Non successfully status code returned: " + result.StatusCode); - return 2; - } - args.Logger?.ILog("Successful status code returned: " + result.StatusCode); - - - return 1; - } - catch (Exception ex) - { - args.Logger?.ELog("Failed sending web request: " + ex.Message + Environment.NewLine + ex.StackTrace); - return -1; - } - } -} diff --git a/Web/FlowElements/WebRequest.cs b/Web/FlowElements/WebRequest.cs index d1b8b002..53300591 100644 --- a/Web/FlowElements/WebRequest.cs +++ b/Web/FlowElements/WebRequest.cs @@ -1,5 +1,6 @@ using System.Text.RegularExpressions; using FileFlows.Web.Helpers; +using HttpMethod = System.Net.Http.HttpMethod; namespace FileFlows.Web.FlowElements;