Parse text in Azure Logic Apps

时光怂恿深爱的人放手 提交于 2019-12-10 14:25:52

问题


I want to create Azure Logic App which will constantly request a specific website on the Internet and parse received HTML.

I've created Logic App and set up interval and HTTP request action.

Which action should I choose as the next step for simple regex operation on HTML code?

What comes to my mind is creating Azure Function which will do the job, but I wonder if there is any other solution, more suitable for such task.

I want it the be simple as possible.


Edit:

Just found out some cool feature. Logic Apps contain some basic expressions for primitive types.

Unfortunetly it lacks of any regex or string.contains.

For now, I'll try with Azure Functions.


回答1:


I've managed to solve my problem with use of Workflow Definition Language and building blocks provided by Azure.

The Azure Function idea was not that bad and would fit perfectly for any more complex case, but as I mentioned, I wanted it as simple as possible, so here it is.

This is how my flow looks now.

For sake of completeness, here is the flow in JSON format

{
    "$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"
            }
        }
    }
}



回答2:


You can use inline code action in logic app to run javascript regex code (preview- May 2019) (Not supported on Flow).

Iniline Code

Logic App Inline Code Ref




回答3:


You're probably on the right track. An Azure Function would be the most appropriate way to implement this right now. An API App is an option but that's a heavier platform than you need.




回答4:


create an Azure Function along the lines of:

{

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 RegexReplace

{

public string Regex { get; set; }

public string Replace { get; set; }

}




回答5:


This is my function to use for replacing text in a string. this is reusable and the approach can be used for many similar type of aspects of working in 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));
}

I would then post an object like this from my logic app:

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

... to get the result: "P-37378633"



来源:https://stackoverflow.com/questions/45456381/parse-text-in-azure-logic-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!