问题
Actual situation:
I have a 2 blob triggered azure functions
that are working perfectly (one is v2
and the other v1
)
I have, in the other hand, a REST WEB API application
(that exposes methods to encrypt and decrypt a stream) published in my azure Devops (still not deployed on azure portal, actually, only code is added to an azure Devops repo)
-> What I want to do is :
call the web api application via http calls(call encrypt or decrypt or whatever) from my azure function to decrypt blob content.
No authentication needed.
In order of best practices, is it more suitable to make an API APP from my web api, or just deploy my web api project as web app to azure? and why ?
In other terms, what is the best way to call an api from my azure function.?
Can anyone provide me some code samples?
回答1:
It seems you want to call API
inside your azure function
here is the code sample for your understanding:
In this Function I supplied a MPN number as Input which valid from a 3rd party API
and return true
and false
in response
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.Text;
namespace HaithemKAROUIApiCase.Functions
{
public static class HaithemKAROUIApiCaseClass
{
[FunctionName("HaithemKAROUIApiCaseFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
// Convert all request param into Json object
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
dynamic requestPram = JsonConvert.DeserializeObject<PartnerMpnModel>(jsonContent);
// Extract each param
//string mpnId = requestPram.mpnId;
if (string.IsNullOrEmpty(requestPram.MpnID))
{
return req.CreateResponse(HttpStatusCode.OK, "Please enter the valid partner Mpn Id!");
}
// Call Your API
HttpClient newClient = new HttpClient();
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, string.Format("YourAPIURL?mpnId={0}", requestPram.MpnID));
//Read Server Response
HttpResponseMessage response = await newClient.SendAsync(newRequest);
bool isValidMpn = await response.Content.ReadAsAsync<bool>();
//Return Mpn status
return req.CreateResponse(HttpStatusCode.OK, new PartnerMpnResponseModel { isValidMpn = isValidMpn });
}
catch (Exception ex)
{
return req.CreateResponse(HttpStatusCode.OK, "Invaild MPN Number! Reason: {0}", string.Format(ex.Message));
}
}
}
public class PartnerMpnModel
{
public string MpnID { get; set; }
}
public class PartnerMpnResponseModel
{
public bool isValidMpn { get; set; }
}
}
Request Format
{
"MpnID": "123456789"
}
If you still have any query feel free to share. Thanks and happy coding!
来源:https://stackoverflow.com/questions/56288606/best-approach-to-call-web-api-from-azure-function