|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using WebApiClient.Contexts; |
| 4 | + |
| 5 | +namespace WebApiClient.Attributes |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// 表示请求参数特性 |
| 9 | + /// </summary> |
| 10 | + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] |
| 11 | + public sealed class ParameterAttribute : Attribute, IApiParameterAttribute |
| 12 | + { |
| 13 | + private static readonly IApiParameterAttribute pathQuery = new PathQueryAttribute(); |
| 14 | + |
| 15 | + private static readonly IApiParameterAttribute headers = new HeadersAttribute(); |
| 16 | + |
| 17 | + private static readonly IApiParameterAttribute formContent = new FormContentAttribute(); |
| 18 | + |
| 19 | + private static readonly IApiParameterAttribute formdataContent = new MulitpartContentAttribute(); |
| 20 | + |
| 21 | + private static readonly IApiParameterAttribute jsonContent = new JsonContentAttribute(); |
| 22 | + |
| 23 | + private static readonly IApiParameterAttribute xmlContent = new XmlContentAttribute(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// 获取参数类型 |
| 27 | + /// </summary> |
| 28 | + public Kind Kind { get; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// 请求参数特性 |
| 32 | + /// </summary> |
| 33 | + /// <param name="kind">参数类型</param> |
| 34 | + /// <exception cref="ArgumentOutOfRangeException"></exception> |
| 35 | + public ParameterAttribute(Kind kind) |
| 36 | + { |
| 37 | + if (Enum.IsDefined(typeof(Kind), kind) == false) |
| 38 | + { |
| 39 | + throw new ArgumentOutOfRangeException(nameof(kind)); |
| 40 | + } |
| 41 | + this.Kind = kind; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// 执行前 |
| 46 | + /// </summary> |
| 47 | + /// <param name="context">上下文</param> |
| 48 | + /// <param name="parameter">参数信息</param> |
| 49 | + /// <returns></returns> |
| 50 | + public async Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) |
| 51 | + { |
| 52 | + switch (this.Kind) |
| 53 | + { |
| 54 | + case Kind.Path: |
| 55 | + await pathQuery.BeforeRequestAsync(context, parameter); |
| 56 | + break; |
| 57 | + |
| 58 | + case Kind.Header: |
| 59 | + await headers.BeforeRequestAsync(context, parameter); |
| 60 | + break; |
| 61 | + |
| 62 | + case Kind.Form: |
| 63 | + await formContent.BeforeRequestAsync(context, parameter); |
| 64 | + break; |
| 65 | + |
| 66 | + case Kind.FormData: |
| 67 | + await formdataContent.BeforeRequestAsync(context, parameter); |
| 68 | + break; |
| 69 | + |
| 70 | + case Kind.JsonBody: |
| 71 | + await jsonContent.BeforeRequestAsync(context, parameter); |
| 72 | + break; |
| 73 | + |
| 74 | + case Kind.XmlBody: |
| 75 | + await xmlContent.BeforeRequestAsync(context, parameter); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// 表示参数类型 |
| 83 | + /// </summary> |
| 84 | + public enum Kind |
| 85 | + { |
| 86 | + /// <summary> |
| 87 | + /// Uri路径参数 |
| 88 | + /// 等效PathQueryAttribute |
| 89 | + /// </summary> |
| 90 | + Path = 0, |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Uri Query |
| 94 | + /// 等效PathQueryAttribute |
| 95 | + /// </summary> |
| 96 | + Query = 0, |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Header |
| 100 | + /// 等效HeaderAttribute |
| 101 | + /// </summary> |
| 102 | + Header = 1, |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// x-www-form-urlencoded |
| 106 | + /// 等效FormContentAttribute |
| 107 | + /// </summary> |
| 108 | + Form = 2, |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// multipart/form-data |
| 112 | + /// 等效MulitpartContentAttribute |
| 113 | + /// </summary> |
| 114 | + FormData = 3, |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// application/json |
| 118 | + /// 等效JsonContentAttribute |
| 119 | + /// </summary> |
| 120 | + JsonBody = 4, |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// application/xml |
| 124 | + /// 等效XmlContentAttribute |
| 125 | + /// </summary> |
| 126 | + XmlBody = 5 |
| 127 | + } |
| 128 | +} |
0 commit comments