-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathResponseCacheEntry.cs
108 lines (97 loc) · 3.66 KB
/
ResponseCacheEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebApiClientCore
{
/// <summary>
/// 表示要缓存的响应实体
/// </summary>
public class ResponseCacheEntry
{
/// <summary>
/// 获取或设置版本号
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// 获取或设置状态码
/// </summary>
public HttpStatusCode StatusCode { get; set; }
/// <summary>
/// 获取或设置状态说明
/// </summary>
public string? ReasonPhrase { get; set; }
/// <summary>
/// 获取或设置请求头
/// </summary>
public Dictionary<string, string[]>? Headers { get; set; }
/// <summary>
/// 获取或设置内容的请求头
/// </summary>
public Dictionary<string, string[]>? ContentHeaders { get; set; }
/// <summary>
/// 获取或设置响应内容
/// </summary>
public byte[] Content { get; set; } = Array.Empty<byte>();
/// <summary>
/// 转换为HttpResponseMessage
/// </summary>
/// <param name="requestMessage">请求信息</param>
/// <param name="cacheProviderName">缓存提供者名</param>
/// <returns></returns>
public HttpResponseMessage ToResponseMessage(HttpRequestMessage requestMessage, string cacheProviderName)
{
var response = new HttpResponseMessage
{
Content = new ByteArrayContent(this.Content),
ReasonPhrase = this.ReasonPhrase,
RequestMessage = requestMessage,
StatusCode = this.StatusCode,
Version = System.Version.Parse(this.Version)
};
if (this.Headers != null)
{
foreach (var item in this.Headers)
{
response.Headers.TryAddWithoutValidation(item.Key, item.Value);
}
}
if (this.ContentHeaders != null)
{
foreach (var item in this.ContentHeaders)
{
response.Content.Headers.TryAddWithoutValidation(item.Key, item.Value);
}
}
if (string.IsNullOrEmpty(cacheProviderName) == false)
{
response.Headers.TryAddWithoutValidation("Response-Cache-Provider", cacheProviderName);
}
return response;
}
/// <summary>
/// 从HttpResponseMessage转换得到
/// </summary>
/// <param name="responseMessage">响应消息</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static async Task<ResponseCacheEntry> FromResponseMessageAsync(HttpResponseMessage responseMessage)
{
if (responseMessage == null)
{
throw new ArgumentNullException(nameof(responseMessage));
}
return new ResponseCacheEntry
{
Content = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false),
ContentHeaders = responseMessage.Content.Headers.ToDictionary(kv => kv.Key, kv => kv.Value.ToArray()),
Headers = responseMessage.Headers.ToDictionary(kv => kv.Key, kv => kv.Value.ToArray()),
ReasonPhrase = responseMessage.ReasonPhrase,
StatusCode = responseMessage.StatusCode,
Version = responseMessage.Version.ToString()
};
}
}
}