Docking API model creation

Request model creation

Extract public methods

Putting the URL in the model can facilitate later maintenance.

 [JsonObject(MemberSerialization.OptOut)]//JsonObject序列化时忽略某些列
 public abstract class BaseRequest
    {
    
    
     [JsonIgnore]//利用特性忽略序列化该字段
        public  string Host {
    
     get; set; }
        [JsonIgnore]
        public  string dev_key {
    
     get; set; }
        
      protected abstract string Url {
    
     get; }
       /// <summary>
        /// 获取Url
        /// </summary>
        /// <returns></returns>
        public  string Get_ApiUrl()
        {
    
    
            if(string.IsNullOrWhiteSpace(Host))
            {
    
    
                throw new ArgumentNullException(paramName: nameof(this.Host), message: "未给Host赋值");
            }
            if(string.IsNullOrWhiteSpace(dev_key))
            {
    
    
                throw new ArgumentNullException(paramName: nameof(this.dev_key), message: "未给dev_key赋值");
            }
            if(!string.IsNullOrWhiteSpace(Url))
            {
    
    
            	//这里是验签方法
                string datestr = JsonConvert.SerializeObject(this);
                string TobeEncrypted = datestr + "&" + this.dev_id + "&" + this.dev_key;
              string MD5str=  System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(TobeEncrypted, "MD5").ToLower();
                byte[] bytes = Encoding.UTF8.GetBytes(MD5str);
                string sign = Convert.ToBase64String(bytes);
                return Host + this.Url + "?sign=" + sign;
            }
            else
            {
    
    
                throw new  ArgumentNullException(paramName: nameof(this.Url), message: "模型类没有定义请求链接");
            }

        }
    }

Define the module request model

The parent class inherited here is the model from which we extracted the public attributes above. Here, the URL of the parent class is rewritten.
This method only needs to write a signature verification method in the parent class, which increases the code reuse rate and achieves high internal content. It is convenient for later maintenance.

 public  class AddOrderGratuityfeeReq : BaseRequest
 {
    
    
 //子类自己的属性
   public string order_id {
    
     get; set; }
 //重写父类URL属性
  protected override string Url {
    
     get {
    
     return "/open/api/external/addordergratuityfee"; } }
 }

Response Model Creation

Extract public methods

Here is also the extraction of public methods to reduce code redundancy

  /// <summary>
    /// 返回实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
   public class BaseResponse<T>
    {
    
    
        /// <summary>
        /// 错误代码
        /// </summary>
        public int error_code {
    
     get; set; }
        /// <summary>
        /// 错误描述
        /// </summary>
        public string error_msg {
    
     get; set; }
        /// <summary>
        /// 详细报错信息(报错的时候非空)
        /// </summary>
        public object error_data {
    
     get; set; }
        /// <summary>
        /// 返回数据
        /// </summary>
        public T result {
    
     get; set; }
    }

Define the response model

 public class CreateOrdeRes:BaseResponse<CreateOrdeRes>
    {
    
    
    //这里如果有其他单独属于这一个模块的属性可以写在这里
	}
	

call method

  CreateOrderReq ordeReq = new CreateOrderReq();//请求对象
string req = HttpHelper.Post(ordeReq.Get_ApiUrl(), JsonConvert.SerializeObject(ordeReq));
//将返回数据序列化为响应对象
 CreateOrdeRes ordeRes = JsonConvert.DeserializeObject<CreateOrdeRes>(req);

High-efficiency code lies in the summary of normal development. During the development process, it is necessary to think about how to facilitate later maintenance, and how to write to improve code efficiency and reuse rate to avoid code redundancy.

Guess you like

Origin blog.csdn.net/qq_42455262/article/details/128479039