从Azure函数调用Web API的最佳方法 [英] Best approach to call web api from azure function

查看:46
本文介绍了从Azure函数调用Web API的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际情况:

Actual situation:

我有2个功能完善的 blob触发天蓝色函数(一个是 v2 ,另一个是 v1 )另一方面,我在我的azure Devops中发布了一个REST WEB API应用程序(公开了加密和解密流的方法)(仍然没有部署在azure门户上,实际上,仅添加了代码)到天青的Devops回购)

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 :

通过azure函数中的http调用(调用加密或解密或其他方法)调用Web api应用程序以解密Blob内容.

call the web api application via http calls(call encrypt or decrypt or whatever) from my azure function to decrypt blob content.

无需身份验证.

按照最佳实践的顺序,从我的Web api制作API APP,还是将我的Web api项目作为Web App部署到Azure上是否更合适?以及为什么?

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 ?

换句话说,从我的azure函数调用api的最佳方法是什么?

In other terms, what is the best way to call an api from my azure function.?

有人可以给我一些代码示例吗?

Can anyone provide me some code samples?

推荐答案

似乎您想在 azure函数中调用 API ,这是供您理解的代码示例:

It seems you want to call API inside your azure function here is the code sample for your understanding:

在此函数中,我提供了一个MPN编号作为输入,该编号从第三方API 有效,并作为响应返回 true false

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

请求格式

{
    "MpnID": "123456789"
}

如果您还有任何疑问,请随时分享.谢谢,祝您编程愉快!

If you still have any query feel free to share. Thanks and happy coding!

这篇关于从Azure函数调用Web API的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆