1.前言
前一段时间,DaoCloud公布了DaoCloud API的使用方法。官方API文档非常详细,大家可以进去看看.大概的样子如下:
2.姿势
正确使用DaoCloud的姿势主要分三步:
1)获取API Token
在官网的"个人账户信息"中可以查看到API Token.
2) 创建RESTClient
由于DaoCloud是基于Rest风格的,想要使用得更加顺手,一个RestClinet是必须的,建议使用RestSharp.通过NuGet可以直接获取.
关于RestSharp如何使用,优势及源码,Restsharp官网是最好的去处.
3) Code
我这里为了演示,只实现了两个方法,可以一窥Restsharp的使用方法.
using DaoCloudModel; using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DaoCloudApi { public class DaoCloudProvider { static RestClient client = new RestClient("https://openapi.daocloud.io/v1"); static string daoToken = "[Your token]"; /// <summary> /// Get all project /// </summary> /// <returns></returns> public static string GetAllProject() { var request = new RestRequest("build-flows", Method.GET); request.AddHeader("Authorization", "token " + daoToken); IRestResponse response = client.Execute(request); var content = response.Content; return content; } /// <summary> /// Get project by project id /// </summary> /// <param name="appId"></param> /// <returns></returns> public static DaoProject GetProject(string appId) { var request = new RestRequest("build-flows/{appId}", Method.GET); request.AddHeader("Authorization", "token " + daoToken); request.AddUrlSegment("appId", appId); IRestResponse<DaoProject> response = client.Execute<DaoProject>(request); var content = response.Data; return content; } } }调用一下试试.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DaoCloudClient { class Program { static void Main(string[] args) { string projectId = "f970d97b-b625-4fd0-a0e0-bd831eff2419"; string result1 = DaoCloudApi.DaoCloudProvider.GetAllProject(); DaoCloudModel.DaoProject result2 = DaoCloudApi.DaoCloudProvider.GetProject(projectId); Console.WriteLine(result1); Console.WriteLine("--------------------------"); Console.WriteLine(result2.Name); Console.ReadKey(); } } }结果:
Docker 实现了DevOps的自动化,Docker本身的API调用起来比较复杂,DaoCloud为我们屏蔽了这种难度,我们可以基于DaoCloud的平台开发一些自动化方面的应用,目前来看效率还是很高的.