GZAPI框架初识
新建一个MVC项目(GZAPIFramework.Demo):
mvc:用于API接口文档查看,Log日志查看
webapi:api调用
新建一个Biz类库并添加nuget引用:
搜索GZAPI.Core并安装,
也可以用指令安装
Install-Package GZAPI.Core
初始化配置:
实现接口【配置接口列表】:GZAPI.Core.Interface.IInfaceCodeList,
/// <summary> /// 接口列表 /// </summary> public class InterfaceCodeList : GZAPI.Core.Interface.IInfaceCodeList { public Dictionary<int, Type> InitInterface() { Dictionary<int, Type> dic = new Dictionary<int, Type>(); return dic; } }
实现接口【请求统一处理】:GZAPI.Core.Interface.IValidateRequset
/// <summary> /// 获取请求成功后处理 /// </summary> public class ValidateRequest : GZAPI.Core.Interface.IValidateRequset { public bool DoValidateRequset(IRequestModel request, IResponseModel response, bool ValidaToken) { return true; } }
实现接口【处理异常捕获】:GZAPI.Core.Interface.ICatchAPIException
/// <summary> /// 处理异常捕获 /// </summary> public class CatchAPIException : GZAPI.Core.Interface.ICatchAPIException { public void CatchException(IResponseModel data, Exception e) { } }
新建GZAPIFramework.Model并新增两个模型
请求参数模型
/// <summary> /// 请求参数 /// </summary> public class RequestModel : GZAPI.Core.Models.Interface.IRequestModel { /// <summary> /// 接口请求参数 /// </summary> public object Data { get; set; } /// <summary> /// 接口号 /// </summary> public int InterfaceCode { get; set; } /// <summary> /// Token令牌 /// </summary> public string Token { get; set; } /// <summary> /// 接口参数转换为对象集合 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public List<T> DataToList<T>() where T : class { List<T> lst = new List<T>(); if (Data is Newtonsoft.Json.Linq.JArray) { foreach (Newtonsoft.Json.Linq.JObject o in Data as Newtonsoft.Json.Linq.JArray) { var v = ToObject<T>(o); if (v != null) { lst.Add(v); } } } return lst; } /// <summary> /// 接口参数转换为对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T DataToObject<T>() where T : class { if (Data is Newtonsoft.Json.Linq.JObject) return ToObject<T>(Data as Newtonsoft.Json.Linq.JObject); else return null; } /// <summary> /// JObjerct转换为对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="o"></param> /// <returns></returns> private T ToObject<T>(Newtonsoft.Json.Linq.JObject o) where T : class { if (o == null) return null; return o.ToObject<T>(); } }
响应参数模型
/// <summary> /// 响应数据 /// </summary> public class ResponseModel : GZAPI.Core.Models.Interface.IResponseModel { /// <summary> /// 错误代码 /// </summary> public int ErrCode { get; private set; } /// <summary> /// 错误消息 /// </summary> public string ErrMsg { get; private set; } /// <summary> /// 返回数据 /// </summary> public object Data { get; set; } /// <summary> /// 设置错误 /// </summary> /// <param name="errcode"></param> /// <param name="errorMsg"></param> public void setError(int errcode, string errorMsg) { ErrCode = errcode; ErrMsg = errorMsg; } }
初始化框架:
public class GZAPIConfig { /// <summary> /// 初始化API框架 /// </summary> public static void InitConfig() { GZAPI.Core.BicycleAPIuration.Configure(Config => { Config.SetInfaceCodeList(new InterfaceCodeList()); Config.SetValidateToken(new ValidateRequest()); Config.SetCathException(new CatchAPIException()); }); } }
目录结构:
在GZAPIFramework.Demo.Global.asax.cs中调用配置
项目图
/// <summary> /// 空参数空返回 /// </summary> public abstract class RequestWithVoidGetVoid : GZAPI.Core.Biz.Base.RequestWithVoidGetVoidBase<Model.RequestModel, Model.ResponseModel> { public RequestWithVoidGetVoid(Model.RequestModel data) : base(data) { } }
/// <summary> /// 空参数 Object类型返回 /// </summary> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithVoidGetObject<TResponseBody> : GZAPI.Core.Biz.Base.RequestWithVoidGetObjectBase<Model.RequestModel, Model.ResponseModel, TResponseBody> where TResponseBody : class { public RequestWithVoidGetObject(Model.RequestModel data) : base(data) { } }
/// <summary> /// 空参数 Arry类型返回 /// </summary> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithVoidGetArray<TResponseBody> : GZAPI.Core.Biz.Base.RequestWithVoidGetArrayBase<Model.RequestModel, Model.ResponseModel, TResponseBody> where TResponseBody : class { public RequestWithVoidGetArray(Model.RequestModel data) : base(data) { } }
/// <summary> /// Object参数 空返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> public abstract class RequestWithObjectGetVoid<TRequestBody> : GZAPI.Core.Biz.Base.RequestWithObjectGetVoidBase<Model.RequestModel, Model.ResponseModel, TRequestBody> where TRequestBody : class { public RequestWithObjectGetVoid(Model.RequestModel data) : base(data) { } }
/// <summary> /// Object参数 Object类型返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithObjectGetObject<TRequestBody, TResponseBody> : GZAPI.Core.Biz.Base.RequestWithObjectGetObjectBase<Model.RequestModel, Model.ResponseModel, TRequestBody, TResponseBody> where TRequestBody : class where TResponseBody : class { public RequestWithObjectGetObject(Model.RequestModel data) : base(data) { } }
/// <summary> /// Object参数 Array类型返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithObjectGetArray<TRequestBody, TResponseBody> : GZAPI.Core.Biz.Base.RequestWithObjectGetArrayBase<Model.RequestModel, Model.ResponseModel, TRequestBody, TResponseBody> where TRequestBody : class where TResponseBody : class { public RequestWithObjectGetArray(Model.RequestModel data) : base(data) { } }
/// <summary> /// Array参数 空返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> public abstract class RequestWithArrayGetVoid<TRequestBody> : GZAPI.Core.Biz.Base.RequestWithArrayGetVoidBase<Model.RequestModel, Model.ResponseModel, TRequestBody> where TRequestBody : class { public RequestWithArrayGetVoid(Model.RequestModel data) : base(data) { } }
/// <summary> /// Array参数 Object类型返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithArrayGetObject<TRequestBody, TResponseBody> : GZAPI.Core.Biz.Base.RequestWithArrayGetObjectBase<Model.RequestModel, Model.ResponseModel, TRequestBody, TResponseBody> where TRequestBody : class where TResponseBody : class { public RequestWithArrayGetObject(Model.RequestModel data) : base(data) { } }
/// <summary> /// Array参数 Array类型返回 /// </summary> /// <typeparam name="TRequestBody"></typeparam> /// <typeparam name="TResponseBody"></typeparam> public abstract class RequestWithArrayGetArray<TRequestBody, TResponseBody> : GZAPI.Core.Biz.Base.RequestWithArrayGetArrayBase<Model.RequestModel, Model.ResponseModel, TRequestBody, TResponseBody> where TRequestBody : class where TResponseBody : class { public RequestWithArrayGetArray(Model.RequestModel data) : base(data) { } }
调用
GZAPIFramework.Biz项目添加一个APIHelp类
public class APIHelp { /// <summary> /// 处理请求 /// </summary> /// <param name="data"></param> /// <returns></returns> public static object RunInterface(Model.RequestModel data) { return GZAPI.Core.BicycleAPIuration.Configuration.DoRun(data); } /// <summary> /// 获得所有接口列表 /// </summary> /// <returns></returns> public static object getAllInterface() { return GZAPI.Core.BicycleAPIuration.Configuration.getAllInterface(); } }
项目结构:
GZAPIFramework.Demo项目添加一个API控制器
项目结构
添加Post方法:
public class GZDataController : ApiController { [HttpGet] public object get() { return "API is Success!!"; } [HttpPost] public object Post(Model.RequestModel request) { return Biz.APIHelp.RunInterface(request); } }
实现自动API文档:
添加一个测试APi接口,这里添加一个有参数有返回值的API接口
[Description("测试接口实例1")] public class RequestSample : RequestBase.RequestWithObjectGetObject<SmpleRequestBody, SampleResponseBody> { public RequestSample(RequestModel data) : base(data) { } protected override bool CustomerValidateBodyData(SmpleRequestBody BodyData) { if (BodyData.Account != "garsonzhang") { base.setError(-1, "用户名必须是:garsonzhang"); return false; } return base.CustomerValidateBodyData(BodyData); } protected override SampleResponseBody ProcessingRequest() { SampleResponseBody data = new SampleResponseBody(); data.Account = RequestBodyData.Account; data.UserName = "GZFramework"; data.Status = 1; return data; } } public class SmpleRequestBody { [GZAPI.Core.DataValidate.StringNotNull(ErrorMsg = "Account不能为空")] [DefaultValue("garsonzhang")] [Description("用户账号")] public string Account { get; set; } [GZAPI.Core.DataValidate.StringNotNull(ErrorMsg = "Pwd不能为空")] [DefaultValue("123456")] [Description("登录密码")] public string Pwd { get; set; } } public class SampleResponseBody { [Description("账号")] [DefaultValue("garsonzhang")] public string Account { get; set; } [Description("用户名")] [DefaultValue("GZ")] public string UserName { get; set; } [Description("用户状态 1 正常 2 被锁定")] [DefaultValue(1)] public int Status { get; set; } }
同理,添加第二个接口
特性介绍,主要用于生成接口文档:
DescriptionAttribute:接口/属性 描述
DefaultValue:属性值示例
GZAPI.Core.DataValidate.*:数据验证,验证也可以放在CustomerValidateBodyData方法中进行
项目结构:
配置接口:GZAPIFramework.Biz.Config.InterfaceCodeList
接口我已经查看页面和日志页面主要用到angularjs和signalR,代码就不贴了,有兴趣下载源码查看!
运行项目,查看接口
****/APIHelp/Index
请求例子:post提交请求:
日志监控!
账号为*标识全部监听,空即表示账号为空时也监听跟踪
本节项目分支:https://github.com/GarsonZhang/GZAPIFramework.Demo/tree/step01
源代码:https://github.com/GarsonZhang/GZAPIFramework.Demo
结束!
接下来有时间,补充上Token验证机制