How to call another function using Azure function

I wrote 3 functions as follows

  1. create users in the database
  2. get users from the database
  3. process users

In function [3], I will call function [2] to get users using the URL of the Azure function, as shown below: -

https://hdidownload.azurewebsites.net/api/getusers

Is there any other way to call an Azure function in another Azure function without a full path, as described above?

+18
source share
5 answers

In function applications, there is nothing built-in to call one HTTP function from other functions without actually making an HTTP call.

For simple use cases, I just stick with the full URL call.

Durable Functions, Chain Function.

+12

, , , (Q1/Q2 2018) Durable Functions. , :

... . , .

, . , A => B => C, .

Durable Functions Function. , , , # - ():

[FunctionName("ExpensiveDurableSequence")]
public static async Task<List<string>> Run(
    [OrchestrationTrigger] DurableOrchestrationTrigger context)
{
    var response = new List<Order>();

    // Call external function 1 
    var token = await context.CallActivityAsync<string>("GetDbToken", "i-am-root");

    // Call external function 2
    response.Add(await context.CallActivityAsync<IEnumerable<Order>>("GetOrdersFromDb", token));

    return response;
}

[FunctionName("GetDbToken")]
public static string GetDbToken([ActivityTrigger] string username)
{
    // do expensive remote api magic here
    return token;
}

[FunctionaName("GetOrdersFromDb")]
public static IEnumerable<Order> GetOrdersFromDb([ActivityTrigger] string apikey)
{
    // do expensive db magic here
    return orders;
}

:

  • ,
  • , ; , ,

(, ) (/).

:

+7

, Durable Functions, , , :

public static class Helloworld
{
    [FunctionName("Helloworld")]
    public static string Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
    {
        return "Hello World";
    }

}

public static class HelloWorldCall
{
    [FunctionName("HelloWorldCall")]
    public static string Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
    {
        var caller = Helloworld.Run(req, log);
        return caller;
    }

}
+2

, 2 :

  1. HTTP- URL .
  2. Azure Azure . (Microsoft )

, # :

static HttpClient client = new HttpClient();
[FunctionName("RequestImageProcessing")]
public static async Task RequestImageProcessing([HttpTrigger(WebHookType = "genericJson")]
    HttpRequestMessage req)
{

        string anotherFunctionSecret = ConfigurationManager.AppSettings
            ["AnotherFunction_secret"];
        // anotherFunctionUri is another Azure Function 
        // public URL, which should provide the secret code stored in app settings 
        // with key 'AnotherFunction_secret'
        Uri anotherFunctionUri = new Uri(req.RequestUri.AbsoluteUri.Replace(
            req.RequestUri.PathAndQuery, 
            $"/api/AnotherFunction?code={anotherFunctionSecret}"));

        var responseFromAnotherFunction = await client.GetAsync(anotherFunctionUri);
        // process the response

}

[FunctionName("AnotherFunction")]
public static async Task AnotherFunction([HttpTrigger(WebHookType = "genericJson")]
HttpRequestMessage req)
{
    await Worker.DoWorkAsync();
}
+1

, , .

URL- :

https://my-functn-app-1.azurewebsites.net/some-path-here1?code=123412somecodehereemiii888ii88k123m123l123k1l23k1l3==

Node.js, :

let request_options = {
        method: 'GET',
        host: 'my-functn-app-1.azurewebsites.net',
        path: '/path1/path2?&q1=v1&q2=v2&code=123412somecodehereemiii888ii88k123m123l123k1l23k1l3',
        headers: {
            'Content-Type': 'application/json'
        }
};
require('https')
    .request(
         request_options,
         function (res) {
               // do something here
         });

Worked without problems.
It should work similarly for other programming languages.
Hope this helps.

0
source

Source: https://habr.com/ru/post/1686015/


All Articles