解析 Azure 逻辑应用中的文本
Parse text in Azure Logic Apps
我想创建 Azure Logic App,它将不断请求 Internet 上的特定网站并解析接收到的 HTML。
我已经创建了逻辑应用并设置了时间间隔和 HTTP 请求操作。
我应该选择哪个操作作为对 HTML 代码进行简单正则表达式操作的下一步?
我想到的是创建 Azure Function 来完成这项工作,但我想知道是否有任何其他解决方案,更适合这样的任务。
我希望它尽可能简单。
编辑:
刚刚发现了一些很棒的功能。逻辑应用程序包含一些基本类型的基本表达式。
不幸的是,它缺少任何 regex
或 string.contains
。
现在,我将尝试使用 Azure Functions。
您可能走对了路。 Azure Functions 是目前最合适的实现方式。 API 应用程序是一种选择,但这是一个比您需要的平台更重的平台。
按照以下行创建 Azure 函数:
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
string input = data?.input.ToString();
var regexJson = data?.regexList;
var regexes = regexJson.ToObject<List<RegexReplace>>();
foreach (var regex in regexes)
{
var re = Regex.Replace(regex.Regex, "\\","\");
var replace = Regex.Replace(regex.Replace, "\\","\");
input = Regex.Replace(input, "\\"","\"");
input = Regex.Replace(input, re, replace);
}
input = Regex.Replace(input, "[\r\n]", "");
return data.regexList == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, input, "application/json");
}
public class 正则表达式替换
{
public string Regex { get; set; }
public string Replace { get; set; }
}
我已经通过使用 Workflow Definition Language 和 Azure 提供的构建块解决了我的问题。
Azure Function 的想法还不错,可以完美地适用于任何更复杂的情况,但正如我提到的,我希望它尽可能简单,所以就在这里。
这就是我的流程现在的样子。
为了完整起见,这里是 JSON 格式的流程
{
"$connections": {
"value": {
"wunderlist": {
"connectionId": "/subscriptions/.../providers/Microsoft.Web/connections/wunderlist",
"connectionName": "wunderlist",
"id": "/subscriptions/.../providers/Microsoft.Web/locations/northeurope/managedApis/wunderlist"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Condition": {
"actions": {
"Create_a_task": {
"inputs": {
"body": {
"completed": false,
"list_id": 000000000,
"starred": true,
"title": "@{variables('today date')}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/tasks",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {},
"type": "ApiConnection"
},
"Set_a_reminder": {
"inputs": {
"body": {
"date": "@{addHours(utcNow(), 3)}",
"list_id": 000000,
"task_id": "@body('Create_a_task')?.id"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/reminders",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {
"Create_a_task": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"expression": "@contains(body('HTTP'), variables('today date'))",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "If"
},
"HTTP": {
"inputs": {
"method": "GET",
"uri": "..."
},
"runAfter": {},
"type": "Http"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "today date",
"type": "String",
"value": "@{utcNow('yyyy/MM/dd')}"
}
]
},
"runAfter": {
"HTTP": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2017-08-01T23:55:00Z",
"timeZone": "UTC"
},
"type": "Recurrence"
}
}
}
}
这是我用来替换字符串中的文本的函数。这是可重用的,并且该方法可用于在逻辑应用程序中工作的许多类似类型的方面:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string removeme = data?.removeme;
string replacewith = data?.replacewith;
string value = data?.value;
return req.CreateResponse(HttpStatusCode.OK, value.Replace(removeme, replacewith));
}
然后我会 post 我的逻辑应用程序中的这样一个对象:
{
"removeme": "SKU-",
"replacewith": "P-",
"value": "SKU-37378633"
}
...得到结果:"P-37378633"
您可以在逻辑应用中使用内联代码操作 运行 javascript 正则表达式代码(预览版 - 2019 年 5 月)(Flow 不支持)。
Iniline Code
我想创建 Azure Logic App,它将不断请求 Internet 上的特定网站并解析接收到的 HTML。
我已经创建了逻辑应用并设置了时间间隔和 HTTP 请求操作。
我应该选择哪个操作作为对 HTML 代码进行简单正则表达式操作的下一步?
我想到的是创建 Azure Function 来完成这项工作,但我想知道是否有任何其他解决方案,更适合这样的任务。
我希望它尽可能简单。
编辑:
刚刚发现了一些很棒的功能。逻辑应用程序包含一些基本类型的基本表达式。
不幸的是,它缺少任何 regex
或 string.contains
。
现在,我将尝试使用 Azure Functions。
您可能走对了路。 Azure Functions 是目前最合适的实现方式。 API 应用程序是一种选择,但这是一个比您需要的平台更重的平台。
按照以下行创建 Azure 函数:
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
string input = data?.input.ToString();
var regexJson = data?.regexList;
var regexes = regexJson.ToObject<List<RegexReplace>>();
foreach (var regex in regexes)
{
var re = Regex.Replace(regex.Regex, "\\","\");
var replace = Regex.Replace(regex.Replace, "\\","\");
input = Regex.Replace(input, "\\"","\"");
input = Regex.Replace(input, re, replace);
}
input = Regex.Replace(input, "[\r\n]", "");
return data.regexList == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, input, "application/json");
}
public class 正则表达式替换
{
public string Regex { get; set; }
public string Replace { get; set; }
}
我已经通过使用 Workflow Definition Language 和 Azure 提供的构建块解决了我的问题。
Azure Function 的想法还不错,可以完美地适用于任何更复杂的情况,但正如我提到的,我希望它尽可能简单,所以就在这里。
这就是我的流程现在的样子。
为了完整起见,这里是 JSON 格式的流程
{
"$connections": {
"value": {
"wunderlist": {
"connectionId": "/subscriptions/.../providers/Microsoft.Web/connections/wunderlist",
"connectionName": "wunderlist",
"id": "/subscriptions/.../providers/Microsoft.Web/locations/northeurope/managedApis/wunderlist"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Condition": {
"actions": {
"Create_a_task": {
"inputs": {
"body": {
"completed": false,
"list_id": 000000000,
"starred": true,
"title": "@{variables('today date')}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/tasks",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {},
"type": "ApiConnection"
},
"Set_a_reminder": {
"inputs": {
"body": {
"date": "@{addHours(utcNow(), 3)}",
"list_id": 000000,
"task_id": "@body('Create_a_task')?.id"
},
"host": {
"connection": {
"name": "@parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/reminders",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {
"Create_a_task": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"expression": "@contains(body('HTTP'), variables('today date'))",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "If"
},
"HTTP": {
"inputs": {
"method": "GET",
"uri": "..."
},
"runAfter": {},
"type": "Http"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "today date",
"type": "String",
"value": "@{utcNow('yyyy/MM/dd')}"
}
]
},
"runAfter": {
"HTTP": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2017-08-01T23:55:00Z",
"timeZone": "UTC"
},
"type": "Recurrence"
}
}
}
}
这是我用来替换字符串中的文本的函数。这是可重用的,并且该方法可用于在逻辑应用程序中工作的许多类似类型的方面:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string removeme = data?.removeme;
string replacewith = data?.replacewith;
string value = data?.value;
return req.CreateResponse(HttpStatusCode.OK, value.Replace(removeme, replacewith));
}
然后我会 post 我的逻辑应用程序中的这样一个对象:
{
"removeme": "SKU-",
"replacewith": "P-",
"value": "SKU-37378633"
}
...得到结果:"P-37378633"
您可以在逻辑应用中使用内联代码操作 运行 javascript 正则表达式代码(预览版 - 2019 年 5 月)(Flow 不支持)。
Iniline Code