Skip to content

Commit e1c962e

Browse files
committed
增加CollectionExtensions类型
1 parent 24cf3b2 commit e1c962e

File tree

3 files changed

+91
-66
lines changed

3 files changed

+91
-66
lines changed

WebApiClient/Attributes/ParameterAttributes/PathQueryAttribute.cs

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,66 +88,31 @@ public async Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescr
8888
return;
8989
}
9090

91-
var options = context.HttpApiConfig.FormatOptions.CloneChange(this.DateTimeFormat);
92-
var keyValues = context.HttpApiConfig.KeyValueFormatter.Serialize(parameter, options);
93-
var query = this.FormateCollection(keyValues);
94-
95-
context.RequestMessage.RequestUri = this.UsePathQuery(uri, query);
91+
var options = context
92+
.HttpApiConfig
93+
.FormatOptions
94+
.CloneChange(this.DateTimeFormat);
95+
96+
var keyValues = context
97+
.HttpApiConfig
98+
.KeyValueFormatter
99+
.Serialize(parameter, options)
100+
.FormateAs(this.CollectionFormat);
101+
102+
context.RequestMessage.RequestUri = this.UsePathQuery(uri, keyValues);
96103
await ApiTask.CompletedTask;
97104
}
98105

99-
/// <summary>
100-
/// 格式化集合
101-
/// </summary>
102-
/// <param name="keyValues">键值对</param>
103-
/// <returns></returns>
104-
private IEnumerable<KeyValuePair<string, string>> FormateCollection(IEnumerable<KeyValuePair<string, string>> keyValues)
105-
{
106-
IEnumerable<KeyValuePair<string, string>> JoinValue(IEnumerable<IGrouping<string, KeyValuePair<string, string>>> grouping, string separator)
107-
{
108-
return grouping.Select(item =>
109-
{
110-
var value = string.Join(separator, item.Select(i => i.Value));
111-
return new KeyValuePair<string, string>(item.Key, value);
112-
});
113-
}
114-
115-
if (this.CollectionFormat == CollectionFormat.Multi)
116-
{
117-
return keyValues;
118-
}
119-
120-
var groups = keyValues.GroupBy(item => item.Key);
121-
switch (this.CollectionFormat)
122-
{
123-
case CollectionFormat.Csv:
124-
return JoinValue(groups, @",");
125-
126-
case CollectionFormat.Ssv:
127-
return JoinValue(groups, @" ");
128-
129-
case CollectionFormat.Tsv:
130-
return JoinValue(groups, @"\");
131-
132-
case CollectionFormat.Pipes:
133-
return JoinValue(groups, @"|");
134-
135-
default:
136-
throw new NotImplementedException();
137-
}
138-
}
139-
140-
141106
/// <summary>
142107
/// url添加query或替换segment
143108
/// </summary>
144109
/// <param name="uri">url</param>
145-
/// <param name="query">键值对</param>
110+
/// <param name="keyValues">键值对</param>
146111
/// <returns></returns>
147-
protected Uri UsePathQuery(Uri uri, IEnumerable<KeyValuePair<string, string>> query)
112+
protected Uri UsePathQuery(Uri uri, IEnumerable<KeyValuePair<string, string>> keyValues)
148113
{
149114
var editor = new UriEditor(uri, this.encoding);
150-
foreach (var keyValue in query)
115+
foreach (var keyValue in keyValues)
151116
{
152117
if (editor.Replace(keyValue.Key, keyValue.Value) == false)
153118
{

WebApiClient/Extensions.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@ namespace WebApiClient
1010
/// </summary>
1111
public static class Extensions
1212
{
13-
/// <summary>
14-
/// 转换为只读列表
15-
/// </summary>
16-
/// <typeparam name="T"></typeparam>
17-
/// <param name="source"></param>
18-
/// <exception cref="ArgumentNullException"></exception>
19-
/// <returns></returns>
20-
public static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> source)
21-
{
22-
if (source == null)
23-
{
24-
throw new ArgumentNullException(nameof(source));
25-
}
26-
return source.ToList().AsReadOnly();
27-
}
28-
2913
/// <summary>
3014
/// 返回提供请求重试的请求任务对象
3115
/// </summary>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace WebApiClient
6+
{
7+
/// <summary>
8+
/// 提供集合扩展
9+
/// </summary>
10+
static class CollectionExtensions
11+
{
12+
/// <summary>
13+
/// 格式化集合
14+
/// </summary>
15+
/// <param name="collection">集合</param>
16+
/// <param name="format">格式</param>
17+
/// <returns></returns>
18+
public static IEnumerable<KeyValuePair<string, string>> FormateAs(this IEnumerable<KeyValuePair<string, string>> collection, CollectionFormat format)
19+
{
20+
if (format == CollectionFormat.Multi)
21+
{
22+
return collection;
23+
}
24+
25+
switch (format)
26+
{
27+
case CollectionFormat.Csv:
28+
return collection.FormateAs(@",");
29+
30+
case CollectionFormat.Ssv:
31+
return collection.FormateAs(@" ");
32+
33+
case CollectionFormat.Tsv:
34+
return collection.FormateAs(@"\");
35+
36+
case CollectionFormat.Pipes:
37+
return collection.FormateAs(@"|");
38+
39+
default:
40+
throw new NotImplementedException();
41+
}
42+
}
43+
44+
/// <summary>
45+
/// 格式化集合
46+
/// </summary>
47+
/// <param name="collection">集合</param>
48+
/// <param name="separator">分隔符</param>
49+
/// <returns></returns>
50+
private static IEnumerable<KeyValuePair<string, string>> FormateAs(this IEnumerable<KeyValuePair<string, string>> collection, string separator)
51+
{
52+
return collection.GroupBy(item => item.Key).Select(item =>
53+
{
54+
var value = string.Join(separator, item.Select(i => i.Value));
55+
return new KeyValuePair<string, string>(item.Key, value);
56+
});
57+
}
58+
59+
60+
/// <summary>
61+
/// 转换为只读列表
62+
/// </summary>
63+
/// <typeparam name="T"></typeparam>
64+
/// <param name="source"></param>
65+
/// <exception cref="ArgumentNullException"></exception>
66+
/// <returns></returns>
67+
public static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> source)
68+
{
69+
if (source == null)
70+
{
71+
throw new ArgumentNullException(nameof(source));
72+
}
73+
return source.ToList().AsReadOnly();
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)