From d8b6763de46180c9b40dfc11a11437cb682874f7 Mon Sep 17 00:00:00 2001
From: Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
Date: Mon, 15 Aug 2022 17:46:05 +0200
Subject: [PATCH] Modules: Split http module and create http_multi module

---
 Module.cs   | 47 ++++++++++++++++++++++++++++++++++++++++++-----
 sample.conf | 26 ++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/Module.cs b/Module.cs
index 0957749..a4b2560 100644
--- a/Module.cs
+++ b/Module.cs
@@ -24,6 +24,7 @@ using System.Text.Json;
 using System.Text.Json.Nodes;
 using System.Linq;
 using System.Threading;
+using System.Threading.Tasks;
 
 using System.CommandLine;
 using System.Runtime.InteropServices;
@@ -336,17 +337,14 @@ class ModuleExec: ModuleAbstractPipe
 }
 
 
-[ModuleName("http")]
-class ModuleHttp: ModuleSourceThreadAndParser
+abstract class ModuleHttpAbstract: ModuleSourceThreadAndParser
 {
-	string url;
 	HttpRequestException error;
 	InnerStatusBar ifReadError;
 	int period_ms;
 	int timeout_ms;
 	public override void Init(ModuleParent _bar, ConfigSection section)
 	{
-		url = section.Mandatory("url").AsString();
 		ifReadError  = new InnerStatusBar(_bar, section.Optional("error_handler")?.AsConfig() ??
 				new ConfigParser(
 @"
@@ -359,6 +357,7 @@ text = ERR
 
 		base.Init(_bar, section);
 	}
+	abstract protected Task<string> inputThreadFunc_Get(HttpClient client);
 	async override protected void inputThreadFunc()
 	{
 		HttpClient client = null;
@@ -377,7 +376,7 @@ text = ERR
 			{
 				long t = Environment.TickCount64;
 
-				string _data = await client.GetStringAsync(url);
+				string _data = await inputThreadFunc_Get(client);
 				lock(this)
 				{
 					dataTick = t;
@@ -417,6 +416,44 @@ text = ERR
 	}
 }
 
+[ModuleName("http")]
+class ModuleHttp: ModuleHttpAbstract
+{
+	string url;
+	public override void Init(ModuleParent _bar, ConfigSection section)
+	{
+		url = section.Mandatory("url").AsString();
+		base.Init(_bar, section);
+	}
+	async override protected Task<string> inputThreadFunc_Get(HttpClient client)
+	{
+		return await client.GetStringAsync(url);
+	}
+}
+[ModuleName("http_multi")]
+class ModuleHttpMulti: ModuleHttpAbstract
+{
+	string[] urlList;
+	string separator;
+	public override void Init(ModuleParent _bar, ConfigSection section)
+	{
+		urlList = section.Mandatory("urls").Lines().Select(x => x.AsString()).ToArray();
+		separator = section.Optional("separator")?.AsString() ?? "\n";
+		base.Init(_bar, section);
+	}
+	async override protected Task<string> inputThreadFunc_Get(HttpClient client)
+	{
+		List<string> output = new();
+		foreach(string url in urlList)
+		{
+			string r = await client.GetStringAsync(url);
+			output.Add(r);
+		}
+		return string.Join(separator, output);
+	}
+}
+
+
 
 [ModuleName("i3status")]
 class ModuleI3Status: Module
diff --git a/sample.conf b/sample.conf
index b9edc21..e97ea91 100644
--- a/sample.conf
+++ b/sample.conf
@@ -291,6 +291,32 @@ timeout = 10
 # <time> default 10
 # Timeout of each HTTP request.
 
+[http_multi]
+# Fetch message from more than one http(s) pages
+
+error_handler = [constant]
+        _color = red
+        text = ERR
+delay = 0
+show_old = 10
+max_old = 30
+no_data_handler = [constant]
+        text = No data
+parser = text
+period = 20
+timeout = 10
+# See http module
+
+urls = http://kam.mff.cuni.cz/~jirikalvoda/y
+       http://kam.mff.cuni.cz/~jirikalvoda/z
+# mandatory <string> on each line
+
+separator << AMEN
+
+AMEN
+# <string> default see above (one new line)
+
+
 # Options for all modules
 # =======================
 
-- 
GitLab