一種類似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