Subscribe via RSS
16Jan/260

Getting A Locally Debugged Logic App’s Callback URL

For those of you who want to locally automate testing of Logic Apps running in visual studio, this post is for you! Across the web, there are people asking for a dynamic way to get the Callback URLs for locally debugged logic apps. They run via func.exe and it has been previously thought that the only place to get the URL with the required 'auth' signature was the overview window of the workflow:

Fear not! I've used the development tools to rip apart VSCode and have worked out that our trusty local func.exe exposes all the webservices that Azure does! The full list of services available is over here. Specifically endpoints to list workflows, workflow triggers and then trigger Callback URLs with full signatures!

internal class LogicAppDetector
{
    private string base_url;
    public LogicAppDetector(string baseUrl = "http://localhost:7071/") { 
        this.base_url = baseUrl;
    }

    public List<LogicAppWorkflow> GetListOfWorkflows()
    {
        var workflowListJson = WebClient.DownloadAsString(base_url + 
            "runtime/webhooks/workflow/api/management/workflows");
        return JsonSerializer.Deserialize<List<LogicAppWorkflow>>(workflowListJson); 
    }

    public List<LogicAppWorkflowTrigger> GetWorkflowTriggers(string workflowName) {
        var triggerListJson = WebClient.DownloadAsString(
            base_url + "runtime/webhooks/workflow/api/management/" + 
            "workflows/" + workflowName + "/triggers");
        return JsonSerializer.Deserialize<LogicAppWorkflowTriggerContainer>(triggerListJson).value;
    }

    public string GetWorkflowCallbackUrl(string logicAppWorkflowName)
    {
        var wf = GetListOfWorkflows().FirstOrDefault(x => x.name == logicAppWorkflowName);
        if (wf == null) {
            throw new Exception(
                "Couldn't find workflow [" + logicAppWorkflowName + "] in the list?");
        }
        var trigs = GetWorkflowTriggers(logicAppWorkflowName);
        if (trigs == null) {
            throw new Exception(
                "Couldn't find triggers for workflow [" + logicAppWorkflowName + "]?");
        }
        if (trigs.Count() != 1) {
            throw new Exception(
                "Incorrect trigger count for workflow [" + logicAppWorkflowName + "]" + 
                " ... count was " + trigs.Count() + "?");
        }
        var callbackUrlJson = WebClient.PostJsonData(
            base_url + "runtime/webhooks/workflow/api/management/workflows/" + 
            logicAppWorkflowName + "/triggers/" + trigs[0].name + 
            "/listCallbackUrl", null); //payload is actually NULL.
        var callbackData = 
            JsonSerializer.Deserialize<LogicAppWorkflowTriggerCallbackUrl>(callbackUrlJson);
        return callbackData.value;
    }
}

internal class LogicAppWorkflow
{
    public string name { get; set; }
    public string definition_href { get; set; }
    public string href { get; set; }
    public string kind { get; set; }
    public object triggers { get; set; }
    public bool isDisabled { get; set; }
}

internal class LogicAppWorkflowTriggerContainer
{
    public List<LogicAppWorkflowTrigger> value { get; set; }
}

internal class LogicAppWorkflowTrigger
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}

internal class LogicAppWorkflowTriggerQueries
{
    [JsonProperty("api-version")]
    public string apiversion { get; set; }
    public string sp { get; set; }
    public string sv { get; set; }
    public string sig { get; set; }
}

internal class LogicAppWorkflowTriggerCallbackUrl
{
    public string value { get; set; }
    public string method { get; set; }
    public string basePath { get; set; }
    public LogicAppWorkflowTriggerQueries queries { get; set; }
}

You'll just need to plug in whatever you're using for Web Requests to GET and POST. Also make sure that the actual Logic App is running/debugging in VSCode!

Leave a comment if you have any issues.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.