Azure 函数中的 Azure DevOps REST API

Azure DevOps REST API in a Azure Function

我正在尝试为 Azure DevOps 团队和存储库创建 POST 请求,并希望通过 API 方法创建一个新团队和一个新存储库。我的团队创建得很好,但我不知道如何扩展代码以在同一个 HttpRequest 中创建存储库,也不知道如何让 'body' 包含团队名称和存储库名称因为它们都有相同的 'name' 参数。

我是 C# 和 Azure 函数的新手,不知道如何在我自己的项目中正确实现它。如果有人能指导我正确的方向,我将不胜感激。

我正在使用 Visual Studio 和 .NET Core 3.0。

目前的代码如下:

using System.IO;
using System;
using System.Net;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;


namespace TeamsAdd
{
    public static class TeamsAdd
    {
        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] 
             HttpRequestMessage req)
        {
            var personalaccesstoken = "";
            var body = new
            {
                name = "myteamname",
                
                project = new
                {
                    id = "xxxxxxx",
                    projectname = "myprojectname",
                }
        };

            using (HttpClient client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", personalaccesstoken))));

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = await client.SendAsync(requestMessage))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        response.EnsureSuccessStatusCode();
                    }

                    return req.CreateResponse(HttpStatusCode.Created, "Teams created successfully!");
                }
            }
        }
    }
}


用于创建团队和 repo 的 API 不同。详情请参考here and here。所以我们不能在一个请求中创建这两个资源。我们需要发送两个请求来创建两个不同的资源。

例如

public static class Function2
    {
        public static HttpClient Client = new HttpClient();
        public static string PAT = "";
        [FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
             
            Object teamBody = new
            {
                name = "mytest7878orf",

            };

           Object repoBody = new
            {
                name = "mytest85698",
                project= new {
                    id = "8d5fa9a0-1061-4891-977b-f91189a0dcbe",
                }
            };


           var teamRes=  sendRequest(HttpMethod.Post, "https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0", teamBody);
           var repoRes=  sendRequest(HttpMethod.Post, "https://dev.azure.com/{organization}/{projectId}/_apis/git/repositories?api-version=6.0", repoBody);

            var res = new
            {
                 teamRes = JsonConvert.DeserializeObject(teamRes),
                repoRes = JsonConvert.DeserializeObject(repoRes)

            };

            return req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(res), "application/json");
        }

        public static string sendRequest(HttpMethod method, string url, Object body = null) {
            Client.DefaultRequestHeaders.Accept.Add(
                 new MediaTypeWithQualityHeaderValue("application/json"));

            Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", PAT))));

            //Connecting to the DevOps REST API
            var requestMessage = new HttpRequestMessage(method, url);
            if (body != null) {
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
            }

            using (HttpResponseMessage response =  Client.SendAsync(requestMessage).Result)
            {
                if (!response.IsSuccessStatusCode)
                {
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException ex) {

                        return ex.Message;
                    }


                }
               var content =  response.Content.ReadAsStringAsync().Result;
              
                return content;
            }

        }
    }