|
1 | 1 | using System;
|
2 | 2 | using System.Net;
|
| 3 | +using System.Net.Http; |
3 | 4 | using System.Net.Http.Headers;
|
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using WebApiClientCore.Exceptions;
|
@@ -83,73 +84,75 @@ public Task OnRequestAsync(ApiRequestContext context)
|
83 | 84 | /// <returns></returns>
|
84 | 85 | public async Task OnResponseAsync(ApiResponseContext context)
|
85 | 86 | {
|
86 |
| - this.UseSuccessStatusCode(context); |
87 |
| - if (this.UseMatchAcceptContentType(context) == true) |
| 87 | + if (this.EnsureMatchAcceptContentType == true) |
88 | 88 | {
|
89 |
| - await this.SetResultAsync(context).ConfigureAwait(false); |
| 89 | + var response = context.HttpContext.ResponseMessage; |
| 90 | + var responseContenType = response?.Content.Headers.ContentType; |
| 91 | + if (this.IsMatchAcceptContentType(responseContenType) == false) |
| 92 | + { |
| 93 | + return; |
| 94 | + } |
90 | 95 | }
|
| 96 | + |
| 97 | + await this.ValidateResponseAsync(context).ConfigureAwait(false); |
| 98 | + await this.SetResultAsync(context).ConfigureAwait(false); |
91 | 99 | }
|
92 | 100 |
|
93 | 101 | /// <summary>
|
94 |
| - /// 应用成功状态码 |
| 102 | + /// 指示响应的ContentType与Accept-ContentType是否匹配 |
| 103 | + /// 返回false则调用下一个ApiReturnAttribute来处理响应结果 |
95 | 104 | /// </summary>
|
96 |
| - /// <param name="context"></param> |
| 105 | + /// <param name="responseContentType">响应的ContentType</param> |
| 106 | + /// <returns></returns> |
| 107 | + protected virtual bool IsMatchAcceptContentType(MediaTypeHeaderValue? responseContentType) |
| 108 | + { |
| 109 | + var accept = this.AcceptContentType?.MediaType; |
| 110 | + var response = responseContentType?.MediaType; |
| 111 | + return string.Equals(accept, response, StringComparison.OrdinalIgnoreCase); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// 验证响应消息 |
| 116 | + /// 验证不通过则抛出指定的异常 |
| 117 | + /// </summary> |
| 118 | + /// <param name="context">上下文</param> |
97 | 119 | /// <exception cref="ApiResponseStatusException"></exception>
|
98 |
| - private void UseSuccessStatusCode(ApiResponseContext context) |
| 120 | + /// <returns></returns> |
| 121 | + protected virtual Task ValidateResponseAsync(ApiResponseContext context) |
99 | 122 | {
|
100 | 123 | var response = context.HttpContext.ResponseMessage;
|
101 |
| - if (response == null || this.EnsureSuccessStatusCode == false) |
| 124 | + if (response != null && this.EnsureSuccessStatusCode == true) |
102 | 125 | {
|
103 |
| - return; |
104 |
| - } |
105 |
| - |
106 |
| - if (this.IsSuccessStatusCode(response.StatusCode) == false) |
107 |
| - { |
108 |
| - throw new ApiResponseStatusException(response); |
| 126 | + this.ValidateResponseStatusCode(response); |
109 | 127 | }
|
| 128 | + return Task.CompletedTask; |
110 | 129 | }
|
111 | 130 |
|
112 | 131 | /// <summary>
|
113 |
| - /// 应用匹配ContentType |
| 132 | + /// 验证响应消息的http状态码 |
| 133 | + /// 验证不通过则抛出指定的异常 |
114 | 134 | /// </summary>
|
115 |
| - /// <param name="context"></param> |
116 |
| - /// <returns></returns> |
117 |
| - private bool UseMatchAcceptContentType(ApiResponseContext context) |
| 135 | + /// <param name="response">响应消息</param> |
| 136 | + /// <exception cref="ApiResponseStatusException"></exception> |
| 137 | + protected virtual void ValidateResponseStatusCode(HttpResponseMessage response) |
118 | 138 | {
|
119 |
| - if (this.EnsureMatchAcceptContentType == true && this.AcceptContentType != null) |
| 139 | + if (this.IsSuccessStatusCode(response.StatusCode) == false) |
120 | 140 | {
|
121 |
| - var contenType = context.HttpContext.ResponseMessage?.Content.Headers.ContentType; |
122 |
| - if (this.IsMatchAcceptContentType(contenType) == false) |
123 |
| - { |
124 |
| - return false; |
125 |
| - } |
| 141 | + throw new ApiResponseStatusException(response); |
126 | 142 | }
|
127 |
| - return true; |
128 | 143 | }
|
129 | 144 |
|
130 | 145 | /// <summary>
|
131 |
| - /// 指示状态码是否为成功的状态码 |
| 146 | + /// 指示http状态码是否为成功的状态码 |
132 | 147 | /// </summary>
|
133 |
| - /// <param name="statusCode">状态码</param> |
| 148 | + /// <param name="statusCode">http状态码</param> |
134 | 149 | /// <returns></returns>
|
135 | 150 | protected virtual bool IsSuccessStatusCode(HttpStatusCode statusCode)
|
136 | 151 | {
|
137 | 152 | var status = (int)statusCode;
|
138 | 153 | return status >= 200 && status <= 299;
|
139 | 154 | }
|
140 | 155 |
|
141 |
| - /// <summary> |
142 |
| - /// 验证响应的ContentType与Accept-ContentType是否匹配 |
143 |
| - /// </summary> |
144 |
| - /// <param name="responseContentType"></param> |
145 |
| - /// <returns></returns> |
146 |
| - protected virtual bool IsMatchAcceptContentType(MediaTypeHeaderValue? responseContentType) |
147 |
| - { |
148 |
| - var accept = this.AcceptContentType?.MediaType; |
149 |
| - var response = responseContentType?.MediaType; |
150 |
| - return string.Equals(accept, response, StringComparison.OrdinalIgnoreCase); |
151 |
| - } |
152 |
| - |
153 | 156 | /// <summary>
|
154 | 157 | /// 设置结果值
|
155 | 158 | /// </summary>
|
|
0 commit comments