首页 文章

解析Azure Logic Apps中的文本

提问于
浏览
5

我想创建Azure Logic App,它将不断请求Internet上的特定网站并解析收到的HTML .

我创建了Logic App并设置了间隔和HTTP请求操作 .

我应该选择哪种操作作为HTML代码上简单正则表达式操作的下一步?

我想到的是创建Azure Function这将完成工作,但我想知道是否还有其他解决方案,更适合这样的任务 .

我希望它尽可能简单 .


Edit:

刚发现一些很酷的功能 . 逻辑应用包含原始类型的一些基本表达式 .

不幸的是,它没有任何 regexstring.contains .

现在,我将尝试使用Azure Functions .

4 回答

  • 1

    这是我用来替换字符串中的文本的函数 . 这是可重用的,并且该方法可用于在Logic Apps中工作的许多类似方面:

    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));
    }
    

    然后我会从我的逻辑应用程序发布这样的对象:

    {
        "removeme": "SKU-",
        "replacewith": "P-",
        "value": "SKU-37378633"
    }
    

    ...得到结果:“P-37378633”

  • 0

    你可能走在正确的轨道上 . Azure函数现在是实现此功能的最合适方式 . API应用程序是一个选项,但这是一个比您需要的更重的平台 .

  • 0

    我已经设法使用Workflow Definition Language和Azure提供的构建块解决了我的问题 .

    Azure功能的想法并不是那么糟糕,适合任何更复杂的情况,但正如我所提到的,我希望它尽可能简单,所以在这里 .

    这就是我的流程现在的样子 .

    为了完整起见,这里是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"
                }
            }
        }
    }
    
  • 0

    按以下方式创建Azure功能:

    {

    log.Info(“C#HTTP触发函数处理了一个请求 . ”);

    // 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");
    

    }

    公共类RegexReplace

    {

    public string Regex { get; set; }
    
    public string Replace { get; set; }
    

    }

相关问题