From cf9c4273e53352ac965d1ca23758dc7cd87e8bf5 Mon Sep 17 00:00:00 2001 From: st0le Date: Fri, 29 Jul 2022 10:32:27 -0700 Subject: [PATCH] Initial --- Flow.Launcher.Core/Plugin/HttpPlugin.cs | 37 ++++++++++++++++++++++ Flow.Launcher.Core/Plugin/PluginConfig.cs | 21 +++++++++--- Flow.Launcher.Core/Plugin/PluginsLoader.cs | 30 ++++++++++++------ Flow.Launcher.Plugin/AllowedLanguage.cs | 8 ++++- Flow.Launcher.Plugin/PluginMetadata.cs | 9 ++++-- 5 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 Flow.Launcher.Core/Plugin/HttpPlugin.cs diff --git a/Flow.Launcher.Core/Plugin/HttpPlugin.cs b/Flow.Launcher.Core/Plugin/HttpPlugin.cs new file mode 100644 index 00000000000..71c01a552cb --- /dev/null +++ b/Flow.Launcher.Core/Plugin/HttpPlugin.cs @@ -0,0 +1,37 @@ +using System.IO; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.Core.Plugin +{ + internal class HttpPlugin : JsonRPCPlugin + { + private readonly HttpClient client; + private readonly string url; + + public override string SupportedLanguage { get; set; } = AllowedLanguage.Http; + + public HttpPlugin(string url) + { + this.client = new HttpClient(); + this.url = url; + } + + protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default) + { + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url); + requestMessage.Content = new StringContent(rpcRequest.ToString(), Encoding.UTF8); + var response = this.client.Send(requestMessage, token); + return response.Content.ReadAsStringAsync().Result; + } + + protected override async Task RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default) + { + var response = await this.client.PostAsync(url, new StringContent(rpcRequest.ToString(), Encoding.UTF8), token).ConfigureAwait(false); + return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + } + } +} diff --git a/Flow.Launcher.Core/Plugin/PluginConfig.cs b/Flow.Launcher.Core/Plugin/PluginConfig.cs index dd6517a7fd1..bb344a01e67 100644 --- a/Flow.Launcher.Core/Plugin/PluginConfig.cs +++ b/Flow.Launcher.Core/Plugin/PluginConfig.cs @@ -77,7 +77,7 @@ internal static (List, List) GetUniqueLatestPlug // If metadata's version greater than each duplicate's version, CompareTo > 0 var count = group.Where(x => metadata.Version.CompareTo(x.Version) > 0).Count(); - + // Only add if the meatadata's version is the highest of all duplicates in the group if (count == group.Count() - 1) { @@ -89,7 +89,7 @@ internal static (List, List) GetUniqueLatestPlug } } } - + if (!duplicatesExist) unique_list.Add(metadata); } @@ -128,10 +128,21 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory) return null; } - if (!File.Exists(metadata.ExecuteFilePath)) + if (metadata.Language.Equals(AllowedLanguage.Http, StringComparison.OrdinalIgnoreCase)) { - Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}"); - return null; + if (!string.IsNullOrEmpty(metadata.ApiEndpoint)) + { + Log.Error($"|PluginConfig.GetPluginMetadata|api endpoint didn't exist <{metadata.ApiEndpoint}> for config <{configPath}"); + return null; + } + } + else + { + if (!File.Exists(metadata.ExecuteFilePath)) + { + Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for config <{configPath}"); + return null; + } } return metadata; diff --git a/Flow.Launcher.Core/Plugin/PluginsLoader.cs b/Flow.Launcher.Core/Plugin/PluginsLoader.cs index b3d56221a71..6efa639a45d 100644 --- a/Flow.Launcher.Core/Plugin/PluginsLoader.cs +++ b/Flow.Launcher.Core/Plugin/PluginsLoader.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; using Droplex; @@ -11,7 +10,6 @@ using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; -using System.Diagnostics; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; namespace Flow.Launcher.Core.Plugin @@ -25,7 +23,8 @@ public static List Plugins(List metadatas, PluginsSe var dotnetPlugins = DotNetPlugins(metadatas); var pythonPlugins = PythonPlugins(metadatas, settings); var executablePlugins = ExecutablePlugins(metadatas); - var plugins = dotnetPlugins.Concat(pythonPlugins).Concat(executablePlugins).ToList(); + var httpPlugins = HttpPlugins(metadatas); + var plugins = dotnetPlugins.Concat(pythonPlugins).Concat(executablePlugins).Concat(httpPlugins).ToList(); return plugins; } @@ -85,7 +84,7 @@ public static IEnumerable DotNetPlugins(List source) return; } - plugins.Add(new PluginPair {Plugin = plugin, Metadata = metadata}); + plugins.Add(new PluginPair { Plugin = plugin, Metadata = metadata }); }); metadata.InitTime += milliseconds; } @@ -119,7 +118,7 @@ public static IEnumerable PythonPlugins(List source, return SetPythonPathForPluginPairs(source, Path.Combine(settings.PythonDirectory, PythonExecutable)); var pythonPath = string.Empty; - + if (MessageBox.Show("Flow detected you have installed Python plugins, which " + "will need Python to run. Would you like to download Python? " + Environment.NewLine + Environment.NewLine + @@ -188,22 +187,35 @@ public static IEnumerable PythonPlugins(List source, } private static IEnumerable SetPythonPathForPluginPairs(List source, string pythonPath) - => source + => source .Where(o => o.Language.ToUpper() == AllowedLanguage.Python) .Select(metadata => new PluginPair { - Plugin = new PythonPlugin(pythonPath), + Plugin = new PythonPlugin(pythonPath), Metadata = metadata }) .ToList(); - public static IEnumerable ExecutablePlugins(IEnumerable source) + public static IEnumerable ExecutablePlugins(IEnumerable source) { return source .Where(o => o.Language.ToUpper() == AllowedLanguage.Executable) .Select(metadata => new PluginPair { - Plugin = new ExecutablePlugin(metadata.ExecuteFilePath), Metadata = metadata + Plugin = new ExecutablePlugin(metadata.ExecuteFilePath), + Metadata = metadata + }); + } + + + public static IEnumerable HttpPlugins(List source) + { + return source + .Where(o => o.Language.ToUpper() == AllowedLanguage.Http) + .Select(metadata => new PluginPair + { + Plugin = new HttpPlugin(metadata.ApiEndpoint), + Metadata = metadata }); } } diff --git a/Flow.Launcher.Plugin/AllowedLanguage.cs b/Flow.Launcher.Plugin/AllowedLanguage.cs index 827958a7b38..466199457b9 100644 --- a/Flow.Launcher.Plugin/AllowedLanguage.cs +++ b/Flow.Launcher.Plugin/AllowedLanguage.cs @@ -17,6 +17,11 @@ public static string FSharp get { return "FSHARP"; } } + public static string Http + { + get { return "HTTP"; } + } + public static string Executable { get { return "EXECUTABLE"; } @@ -32,7 +37,8 @@ public static bool IsAllowed(string language) { return IsDotNet(language) || language.ToUpper() == Python.ToUpper() - || language.ToUpper() == Executable.ToUpper(); + || language.ToUpper() == Executable.ToUpper() + || language.ToUpper() == Http.ToUpper(); } } } \ No newline at end of file diff --git a/Flow.Launcher.Plugin/PluginMetadata.cs b/Flow.Launcher.Plugin/PluginMetadata.cs index e8f5cf74432..d66a3477950 100644 --- a/Flow.Launcher.Plugin/PluginMetadata.cs +++ b/Flow.Launcher.Plugin/PluginMetadata.cs @@ -16,7 +16,10 @@ public class PluginMetadata : BaseModel public string Description { get; set; } public string Website { get; set; } public bool Disabled { get; set; } - public string ExecuteFilePath { get; private set;} + + public string ApiEndpoint { get; set; } + + public string ExecuteFilePath { get; private set; } public string ExecuteFileName { get; set; } @@ -35,8 +38,8 @@ internal set public List ActionKeywords { get; set; } - public string IcoPath { get; set;} - + public string IcoPath { get; set; } + public override string ToString() { return Name;