Skip to content

Commit 8b69340

Browse files
authored
Merge pull request #1 from dotnetcore/master
合并
2 parents da6f3af + ac4981e commit 8b69340

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1566
-479
lines changed

App/App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<ProjectReference Include="..\WebApiClientCore.Extensions.NewtonsoftJson\WebApiClientCore.Extensions.NewtonsoftJson.csproj" />
910
<ProjectReference Include="..\WebApiClientCore.Extensions.OAuths\WebApiClientCore.Extensions.OAuths.csproj" />
1011
<ProjectReference Include="..\WebApiClientCore\WebApiClientCore.csproj" />
1112
</ItemGroup>

README.md

Lines changed: 405 additions & 257 deletions
Large diffs are not rendered by default.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Threading.Tasks;
2+
using WebApiClientCore.Extensions.JsonRpc;
3+
4+
namespace WebApiClientCore.Attributes
5+
{
6+
/// <summary>
7+
/// 表示Json-Rpc请求方法
8+
/// </summary>
9+
public class JsonRpcMethodAttribute : HttpPostAttribute, IApiFilterAttribute
10+
{
11+
/// <summary>
12+
/// JsonRpc的方法名称
13+
/// </summary>
14+
private readonly string? method;
15+
16+
/// <summary>
17+
/// 获取或设置提交的Content-Type
18+
/// 默认为application/json-rpc
19+
/// </summary>
20+
public string ContentType { get; set; } = JsonRpcContent.MediaType;
21+
22+
/// <summary>
23+
/// 获取或设置JsonRpc的参数风格
24+
/// 默认为JsonRpcParamsStyle.Array
25+
/// </summary>
26+
public JsonRpcParamsStyle ParamsStyle { get; set; } = JsonRpcParamsStyle.Array;
27+
28+
/// <summary>
29+
/// 获取过滤器是否启用
30+
/// </summary>
31+
bool IAttributeEnable.Enable => true;
32+
33+
/// <summary>
34+
/// Json-Rpc请求方法
35+
/// </summary>
36+
public JsonRpcMethodAttribute()
37+
: this(method: null, path: null)
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Json-Rpc请求方法
43+
/// </summary>
44+
/// <param name="method">JsonRpc的方法名称</param>
45+
public JsonRpcMethodAttribute(string? method)
46+
: this(method, path: null)
47+
{
48+
}
49+
50+
/// <summary>
51+
/// Json-Rpc请求方法
52+
/// </summary>
53+
/// <param name="method">JsonRpc的方法名称</param>
54+
/// <param name="path">JsonRpc路径</param>
55+
public JsonRpcMethodAttribute(string? method, string? path)
56+
: base(path)
57+
{
58+
this.method = method;
59+
}
60+
61+
/// <summary>
62+
/// Action请求前
63+
/// </summary>
64+
/// <param name="context"></param>
65+
/// <returns></returns>
66+
public override Task OnRequestAsync(ApiRequestContext context)
67+
{
68+
var parametrs = new JsonRpcParameters();
69+
context.Properties.Set(typeof(JsonRpcParameters), parametrs);
70+
71+
return base.OnRequestAsync(context);
72+
}
73+
74+
/// <summary>
75+
/// Filter请求前
76+
/// </summary>
77+
/// <param name="context">上下文</param>
78+
/// <returns></returns>
79+
Task IApiFilterAttribute.OnRequestAsync(ApiRequestContext context)
80+
{
81+
var jsonRpcContent = new JsonRpcContent(this.ContentType);
82+
context.HttpContext.RequestMessage.Content = jsonRpcContent;
83+
84+
var parameters = context.Properties.Get<JsonRpcParameters>(typeof(JsonRpcParameters));
85+
var jsonRpcRequest = new JsonRpcRequest
86+
{
87+
Method = this.method ?? context.ApiAction.Name,
88+
Params = parameters.ToJsonRpcParams(this.ParamsStyle),
89+
};
90+
91+
var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;
92+
var jsonSerializer = context.HttpContext.ServiceProvider.GetJsonSerializer();
93+
jsonSerializer.Serialize(jsonRpcContent, jsonRpcRequest, options);
94+
95+
return Task.CompletedTask;
96+
}
97+
98+
/// <summary>
99+
/// Filter响应后
100+
/// </summary>
101+
/// <param name="context">上下文</param>
102+
/// <returns></returns>
103+
Task IApiFilterAttribute.OnResponseAsync(ApiResponseContext context)
104+
{
105+
return Task.CompletedTask;
106+
}
107+
}
108+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading.Tasks;
2+
using WebApiClientCore.Exceptions;
3+
using WebApiClientCore.Extensions.JsonRpc;
4+
5+
namespace WebApiClientCore.Attributes
6+
{
7+
/// <summary>
8+
/// 表示Rpc请求的一个参数
9+
/// </summary>
10+
public class JsonRpcParamAttribute : ApiParameterAttribute
11+
{
12+
/// <summary>
13+
/// 请求前
14+
/// </summary>
15+
/// <param name="context"></param>
16+
/// <returns></returns>
17+
public override Task OnRequestAsync(ApiParameterContext context)
18+
{
19+
var parameters = context.Properties.Get<JsonRpcParameters>(typeof(JsonRpcParameters));
20+
if (parameters == null)
21+
{
22+
throw new ApiInvalidConfigException($"请为接口方法{context.ApiAction.Name}修饰{nameof(JsonRpcMethodAttribute)}");
23+
}
24+
25+
parameters.Add(context);
26+
return Task.CompletedTask;
27+
}
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace WebApiClientCore.Attributes
2+
{
3+
/// <summary>
4+
/// 表示JsonRpc的参数风格
5+
/// </summary>
6+
public enum JsonRpcParamsStyle
7+
{
8+
/// <summary>
9+
/// 数组形式
10+
/// </summary>
11+
Array,
12+
13+
/// <summary>
14+
/// 对象形式
15+
/// </summary>
16+
Object
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using WebApiClientCore.HttpContents;
2+
3+
namespace WebApiClientCore.Extensions.JsonRpc
4+
{
5+
/// <summary>
6+
/// 表示JsonRpc请求内容
7+
/// </summary>
8+
class JsonRpcContent : BufferContent
9+
{
10+
/// <summary>
11+
/// 获取对应的ContentType
12+
/// </summary>
13+
public static string MediaType => "application/json-rpc";
14+
15+
/// <summary>
16+
/// JsonRpc请求内容
17+
/// </summary>
18+
/// <param name="mediaType">媒体类型</param>
19+
public JsonRpcContent( string? mediaType)
20+
: base(mediaType ?? MediaType)
21+
{
22+
}
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using WebApiClientCore.Attributes;
4+
5+
namespace WebApiClientCore.Extensions.JsonRpc
6+
{
7+
/// <summary>
8+
/// 表示JsonRpc参数
9+
/// </summary>
10+
class JsonRpcParameters : List<ApiParameterContext>
11+
{
12+
/// <summary>
13+
/// 转换为jsonRpc请求参数
14+
/// </summary>
15+
/// <param name="paramsStyle"></param>
16+
/// <returns></returns>
17+
public object ToJsonRpcParams(JsonRpcParamsStyle paramsStyle)
18+
{
19+
if (paramsStyle == JsonRpcParamsStyle.Array)
20+
{
21+
return this.Select(item => item.ParameterValue).ToArray();
22+
}
23+
else
24+
{
25+
return this.ToDictionary(item => item.Parameter.Name, item => item.ParameterValue);
26+
}
27+
}
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
using System.Threading;
4+
5+
namespace WebApiClientCore.Extensions.JsonRpc
6+
{
7+
/// <summary>
8+
/// 表示JsonRpc的请求实体
9+
/// </summary>
10+
class JsonRpcRequest
11+
{
12+
/// <summary>
13+
/// id值
14+
/// </summary>
15+
private static int @id = 0;
16+
17+
/// <summary>
18+
/// jsonrpc
19+
/// 2.0
20+
/// </summary>
21+
[JsonPropertyName("jsonrpc")]
22+
public string JsonRpc { get; set; } = "2.0";
23+
24+
/// <summary>
25+
/// 请求的方法名
26+
/// </summary>
27+
[JsonPropertyName("method")]
28+
public string Method { get; set; } = string.Empty;
29+
30+
/// <summary>
31+
/// 请求的参数数组
32+
/// </summary>
33+
[JsonPropertyName("params")]
34+
public object Params { get; set; } = Array.Empty<object>();
35+
36+
/// <summary>
37+
/// 请求的id
38+
/// </summary>
39+
[JsonPropertyName("id")]
40+
public int Id { get; set; } = Interlocked.Increment(ref id);
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace WebApiClientCore.Extensions.JsonRpc
2+
{
3+
/// <summary>
4+
/// 表示JsonRpc的错误内容
5+
/// </summary>
6+
public class JsonRpcError
7+
{
8+
/// <summary>
9+
/// 错误码
10+
/// </summary>
11+
public int Code { get; set; }
12+
13+
/// <summary>
14+
/// 提示消息
15+
/// </summary>
16+
public string? Message { get; set; }
17+
18+
/// <summary>
19+
/// 错误内容
20+
/// </summary>
21+
public object? Data { get; set; }
22+
}
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace WebApiClientCore.Extensions.JsonRpc
4+
{
5+
/// <summary>
6+
/// 表示JsonRpc的请求返回结果
7+
/// </summary>
8+
/// <typeparam name="TResult"></typeparam>
9+
public class JsonRpcResult<TResult>
10+
{
11+
/// <summary>
12+
/// 请求id
13+
/// </summary>
14+
[JsonPropertyName("id")]
15+
public int? Id { get; set; }
16+
17+
/// <summary>
18+
/// jsonrpc版本号
19+
/// </summary>
20+
[JsonPropertyName("jsonrpc")]
21+
public string JsonRpc { get; set; } = string.Empty;
22+
23+
/// <summary>
24+
/// 结果值
25+
/// </summary>
26+
#nullable disable
27+
[JsonPropertyName("result")]
28+
public TResult Result { get; set; }
29+
#nullable enable
30+
31+
/// <summary>
32+
/// 错误内容
33+
/// </summary>
34+
[JsonPropertyName("error")]
35+
public JsonRpcError? Error { get; set; }
36+
}
37+
}
38+

0 commit comments

Comments
 (0)