-
Notifications
You must be signed in to change notification settings - Fork 447
WebApiClient基础
public interface MyWebApi : IDisposable
{
// GET http://www.mywebapi.com/webapi/user?account=laojiu
[HttpGet("http://www.mywebapi.com/webapi/user")]
ITask<string> GetUserByAccountAsync(string account);
}
var myWebApi = HttpApiClient.CreateMyWebApi>();
var userStr = await myWebApi.GetUserByAccountAsync("laojiu");
myWebApi.Dispose();
如果你有多个接口,而且都指向对一服务器,可以将请求的域名抽出来放到HttpHost特性,接口的代码如下:
[HttpHost("http://www.mywebapi.com")]
public interface MyWebApi : IDisposable
{
// GET /webapi/user?account=laojiu
[HttpGet("/webapi/user")]
ITask<string> GetUserByAccountAsync(string account);
}
如果接口的返回内容是xml或json,你希望使用ITask这样的强类型TResult模型接收返回值,你需要给接口打上对应的[XmlReturn]或[JsonResult],实际上还有一个[AutoReturn],它会根据回复头标识,自动选择不同的转换器转换为TResult类型的结果,默认的,每个接口都使用了[AutoReturn],除非给接口显示使用了[XmlReturn]或[JsonResult]。如果以上返回的userStr是UserInfo类型的xml或json文本,那么强类型的代码声明如下:
[HttpHost("http://www.mywebapi.com")]
public interface MyWebApi : IDisposable
{
// GET /webapi/user?account=laojiu
[HttpGet("/webapi/user")]
[AutoReturn] // 这是可选的
ITask<UserInfo> GetUserByAccountAsync(string account);
}
你可以将多个参数一一设计为接口的参数,类似于:
// GET /webapi/user?account=laojiu&password=123456
[HttpGet("/webapi/user")]
ITask<UserInfo> GetUserAsync(string account, string password);
可也可以将所有参数合到一个简单的多属性模型对象:
public class MyParameters
{
public string Account { get; set; }
public string Password { get; set; }
}
// GET /webapi/user?account=laojiu&password=123456
[HttpGet("/webapi/user")]
ITask<UserInfo> GetUserAsync(MyParameters parameters);
在一些场景中,除了提交多属性模型对象之外,可能还需要一个简单类型的额外参数,你可以如下编写接口:
// GET /webapi/user?account=laojiu&password=123456&birthDay=2010-01-01 01:01:01
[HttpGet("/webapi/user")]
ITask<UserInfo> GetUserAsync(MyParameters parameters,DateTime birthDay);
上面这里,你可能会遇到一个问题,birthDay会简单的ToString()值做为参数值,如果你希望只需要日期而不包含时间,你可以给birthDay指定格式:
// GET /webapi/user?account=laojiu&password=123456&birthDay=2010-01-01
[HttpGet("/webapi/user")]
ITask<UserInfo> GetUserAsync(MyParameters parameters,[PathQuery("yyyy-MM-dd")] DateTime birthDay);
实际上,对于没有任何特性修饰的每个参数,都默认被[PathQuery]修饰,表示做为请求路径或请求参数处理,[PathQuery]的重载构造器可以指定日期时间格式。
// POST webapi/user
// Body Account=laojiu&Password=123456
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([FormContent] UserInfo user);
设计风格和Get请求是差不多的,你应该发现,接口参数被[FormContent]修饰了,[FormContent]的作用是将模型参数user写入到请求的body中。如果你还需要提供一个额外的简单类型参数,请使用[FormField]修饰这个参数,可以这样设计接口:
// POST webapi/user
// Body Account=laojiu&Password=123456&fieldX=xxx
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([FormContent] UserInfo user, [FormField] string fieldX);
// POST webapi/user
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([MulitpartContent] UserInfo user);
// POST webapi/user
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([MulitpartContent] UserInfo user, [MulitpartText] string nickName, MulitpartFile file);
需要了解的是,[MulitpartText]表示是一个文本项,而MulitpartFile表示一个文件项,MulitpartFile实现了IApiParameterable接口,有关IApiParameterable的说明先跳过。
对于json和xml,只能一次性提交一个参数,不支持额外参数之说
// POST webapi/user
// Body user的json文本
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([JsonContent] UserInfo user);
// POST webapi/user
// Body user的xml文本
[HttpPost("/webapi/user")]
ITask<UserInfo> UpdateUserWithFormAsync([XmlContent] UserInfo user);
如果你的UserInfo有DateTime类型的属性,你可以使用[JsonContent("时间格式")]来修饰接口参数。
IApiParameterable是一个接口,任何实现了此接口的类型,都可以直接做一个参数,在请求之前,参数实例就被执行Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter);
目前有以下类型可以直接做为参数:
- BasicAuth
- FormField
- MulitpartFile
- MulitpartText
- Timeout