美文网首页
一种类似Retrofit声明接口即可实现调用的WebApi客户端

一种类似Retrofit声明接口即可实现调用的WebApi客户端

作者: kingZXY2009 | 来源:发表于2017-04-05 23:33 被阅读110次

一种类似Retrofit声明接口即可实现调用的WebApi客户端框架

为.Net出力

java有okhttp,还在okhttp这上搞了一个retrofit,.net有HttpClient,但目前我没有发现有类似的retrofit框架。最近在搞mqtt的webApi封装,输出很多web api接口,给移动端也有给后台二次开发使用的,所以有了搞一个类似retrofit的.net实现,项目叫WebApiClient,托管在https://github.com/xljiulang/WebApiClient

WebApiClient使用

一个原则:声明它,然后调用它,不要实现它。

namespaceDemo

{

[JsonReturn]

[HttpHost("http://www.mywebapi.com")]publicinterfaceMyWebApi

{

[HttpGet("/webapi/{type}/about")]//GET webapi/typeValue/aboutTask> GetAboutAsync(stringtype);

[HttpGet("/webapi/user")]//GET webapi/user?userName=aa&nickName=bb&&BeginTime=cc&EndTime=ddTask> GetUserAsync(stringuserName,stringnickName, TimeFilter timeFilter);

[HttpPut("/webapi/user")]//PUT webapi/userTask>UpdateUserAsync([JsonContent] UserInfo loginInfo);

[HttpDelete("/webapi/user")]//DELETE  webapi/user?id=idValueTask> DeleteUserAsync(stringid);

[HttpDelete("/webapi/user/{id}")]//DELETE  webapi/user/idValueTask> DeleteUser2Async(stringid);

}

}

namespace Demo{    class Program    {        static async void Test()        {            var myWebApi = new WebApiClient.HttpApiClient().GetHttpApi();

await myWebApi.GetAboutAsync("typeValue");

await myWebApi.UpdateUserAsync(new UserInfo { UserName = "abc", Password = "123456" });

await myWebApi.DeleteUser2Async(id: "id001");

}

static void Main(string[] args)

{

Test();

Console.ReadLine();

}

}

}

欢迎加入技术QQ群:364595326

相关文章

网友评论

      本文标题:一种类似Retrofit声明接口即可实现调用的WebApi客户端

      本文链接:https://www.haomeiwen.com/subject/owhfattx.html