Skip to content

Commit b28916f

Browse files
author
Ethan Celletti
authored
Making IRpcRequestHandler public (#115)
1 parent 3e324e3 commit b28916f

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/EdjCase.JsonRpc.Router/Abstractions/IRpcRequestHandler.cs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,65 @@
1-
using System;
1+
using System;
22
using System.Buffers;
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.Runtime.CompilerServices;
67
using System.Text;
78
using System.Threading.Tasks;
89
using EdjCase.JsonRpc.Router.Utilities;
910

1011
namespace EdjCase.JsonRpc.Router.Abstractions
1112
{
12-
internal interface IRpcRequestHandler
13+
public interface IRpcRequestHandler
1314
{
14-
Task<bool> HandleRequestAsync(Stream requestBody, Stream responseBody);
15+
/// <summary>
16+
/// Takes in the request bytes and context, invokes the rpc request and then
17+
/// sets the response bytes if there is a response
18+
/// </summary>
19+
/// <param name="context">Contextual information about the request being handled</param>
20+
/// <param name="requestBody">The request byte stream</param>
21+
/// <param name="responseBody">An writable stream to write the response to</param>
22+
/// <returns>True if there is a response. If false, no bytes will be written to the stream</returns>
23+
Task<bool> HandleRequestAsync(RpcContext context, Stream requestBody, Stream responseBody);
1524

16-
public virtual async Task<string?> HandleRequestAsync(string requestJson)
25+
/// <summary>
26+
/// Takes in the request bytes and context, invokes the rpc request and then returns
27+
/// the response bytes if there is a response
28+
/// </summary>
29+
/// <param name="context">Contextual information about the request being handled</param>
30+
/// <param name="requestBody">The request bytes</param>
31+
/// <returns>The response bytes or null (if there is no response)</returns>
32+
public virtual async Task<byte[]?> HandleRequestAsync(RpcContext context, byte[] requestBody)
33+
{
34+
using (var requestStream = new MemoryStream(requestBody))
35+
{
36+
using (var responseStream = new MemoryStream())
37+
{
38+
bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream);
39+
if (!hasResponse)
40+
{
41+
return null;
42+
}
43+
responseStream.Position = 0;
44+
return responseStream.ToArray();
45+
}
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Takes in the request json and context, invokes the rpc request and then returns
51+
/// the response json if there is a response
52+
/// </summary>
53+
/// <param name="context">Contextual information about the request being handled</param>
54+
/// <param name="requestJson">The request json</param>
55+
/// <returns>The response json or null (if there is no response)</returns>
56+
public virtual async Task<string?> HandleRequestAsync(RpcContext context, string requestJson)
1757
{
1858
using (var requestStream = StreamUtil.GetStreamFromUtf8String(requestJson))
1959
{
2060
using (var responseStream = new MemoryStream())
2161
{
22-
bool hasResponse = await this.HandleRequestAsync(requestStream, responseStream);
62+
bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream);
2363
if (!hasResponse)
2464
{
2565
return null;

src/EdjCase.JsonRpc.Router/RpcHttpRouter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ public async Task RouteAsync(RouteContext context)
6767

6868
IRpcRequestHandler requestHandler = context.HttpContext.RequestServices.GetRequiredService<IRpcRequestHandler>();
6969
var routeContext = new RpcContext(context.HttpContext.RequestServices, requestPath);
70-
context.HttpContext.RequestServices.GetRequiredService<IRpcContextAccessor>().Set(routeContext);
7170
Stream writableStream = this.BuildWritableResponseStream(context.HttpContext);
7271
using (var requestBody = new MemoryStream())
7372
{
7473
await context.HttpContext.Request.Body.CopyToAsync(requestBody);
7574
requestBody.Position = 0;
76-
bool hasResponse = await requestHandler.HandleRequestAsync(requestBody, writableStream);
75+
bool hasResponse = await requestHandler.HandleRequestAsync(routeContext, requestBody, writableStream);
7776
if (!hasResponse)
7877
{
7978
//No response required, but status code must be 204

src/EdjCase.JsonRpc.Router/RpcRequestHandler.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using EdjCase.JsonRpc.Router;
1+
using EdjCase.JsonRpc.Router;
22
using EdjCase.JsonRpc.Common;
33
using EdjCase.JsonRpc.Router.Abstractions;
44
using EdjCase.JsonRpc.Router.Defaults;
@@ -11,7 +11,8 @@
1111
using System.Linq;
1212
using System.Text;
1313
using System.Threading.Tasks;
14-
14+
using Microsoft.Extensions.DependencyInjection;
15+
1516
namespace EdjCase.JsonRpc.Router
1617
{
1718
internal class RpcRequestHandler : IRpcRequestHandler
@@ -55,8 +56,9 @@ public RpcRequestHandler(IOptions<RpcServerConfiguration> serverConfig,
5556
this.logger = logger;
5657
}
5758

58-
public async Task<bool> HandleRequestAsync(Stream requestBody, Stream responseBody)
59-
{
59+
public async Task<bool> HandleRequestAsync(RpcContext context, Stream requestBody, Stream responseBody)
60+
{
61+
context.RequestServices.GetRequiredService<IRpcContextAccessor>().Set(context);
6062
try
6163
{
6264
ParsingResult result = this.parser.ParseRequests(requestBody);

0 commit comments

Comments
 (0)