Skip to content

Commit

Permalink
Making IRpcRequestHandler public (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Celletti authored Mar 23, 2023
1 parent 3e324e3 commit b28916f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
50 changes: 45 additions & 5 deletions src/EdjCase.JsonRpc.Router/Abstractions/IRpcRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
using System;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using EdjCase.JsonRpc.Router.Utilities;

namespace EdjCase.JsonRpc.Router.Abstractions
{
internal interface IRpcRequestHandler
public interface IRpcRequestHandler
{
Task<bool> HandleRequestAsync(Stream requestBody, Stream responseBody);
/// <summary>
/// Takes in the request bytes and context, invokes the rpc request and then
/// sets the response bytes if there is a response
/// </summary>
/// <param name="context">Contextual information about the request being handled</param>
/// <param name="requestBody">The request byte stream</param>
/// <param name="responseBody">An writable stream to write the response to</param>
/// <returns>True if there is a response. If false, no bytes will be written to the stream</returns>
Task<bool> HandleRequestAsync(RpcContext context, Stream requestBody, Stream responseBody);

public virtual async Task<string?> HandleRequestAsync(string requestJson)
/// <summary>
/// Takes in the request bytes and context, invokes the rpc request and then returns
/// the response bytes if there is a response
/// </summary>
/// <param name="context">Contextual information about the request being handled</param>
/// <param name="requestBody">The request bytes</param>
/// <returns>The response bytes or null (if there is no response)</returns>
public virtual async Task<byte[]?> HandleRequestAsync(RpcContext context, byte[] requestBody)
{
using (var requestStream = new MemoryStream(requestBody))
{
using (var responseStream = new MemoryStream())
{
bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream);
if (!hasResponse)
{
return null;
}
responseStream.Position = 0;
return responseStream.ToArray();
}
}
}

/// <summary>
/// Takes in the request json and context, invokes the rpc request and then returns
/// the response json if there is a response
/// </summary>
/// <param name="context">Contextual information about the request being handled</param>
/// <param name="requestJson">The request json</param>
/// <returns>The response json or null (if there is no response)</returns>
public virtual async Task<string?> HandleRequestAsync(RpcContext context, string requestJson)
{
using (var requestStream = StreamUtil.GetStreamFromUtf8String(requestJson))
{
using (var responseStream = new MemoryStream())
{
bool hasResponse = await this.HandleRequestAsync(requestStream, responseStream);
bool hasResponse = await this.HandleRequestAsync(context, requestStream, responseStream);
if (!hasResponse)
{
return null;
Expand Down
3 changes: 1 addition & 2 deletions src/EdjCase.JsonRpc.Router/RpcHttpRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ public async Task RouteAsync(RouteContext context)

IRpcRequestHandler requestHandler = context.HttpContext.RequestServices.GetRequiredService<IRpcRequestHandler>();
var routeContext = new RpcContext(context.HttpContext.RequestServices, requestPath);
context.HttpContext.RequestServices.GetRequiredService<IRpcContextAccessor>().Set(routeContext);
Stream writableStream = this.BuildWritableResponseStream(context.HttpContext);
using (var requestBody = new MemoryStream())
{
await context.HttpContext.Request.Body.CopyToAsync(requestBody);
requestBody.Position = 0;
bool hasResponse = await requestHandler.HandleRequestAsync(requestBody, writableStream);
bool hasResponse = await requestHandler.HandleRequestAsync(routeContext, requestBody, writableStream);
if (!hasResponse)
{
//No response required, but status code must be 204
Expand Down
10 changes: 6 additions & 4 deletions src/EdjCase.JsonRpc.Router/RpcRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EdjCase.JsonRpc.Router;
using EdjCase.JsonRpc.Router;
using EdjCase.JsonRpc.Common;
using EdjCase.JsonRpc.Router.Abstractions;
using EdjCase.JsonRpc.Router.Defaults;
Expand All @@ -11,7 +11,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyInjection;

namespace EdjCase.JsonRpc.Router
{
internal class RpcRequestHandler : IRpcRequestHandler
Expand Down Expand Up @@ -55,8 +56,9 @@ public RpcRequestHandler(IOptions<RpcServerConfiguration> serverConfig,
this.logger = logger;
}

public async Task<bool> HandleRequestAsync(Stream requestBody, Stream responseBody)
{
public async Task<bool> HandleRequestAsync(RpcContext context, Stream requestBody, Stream responseBody)
{
context.RequestServices.GetRequiredService<IRpcContextAccessor>().Set(context);
try
{
ParsingResult result = this.parser.ParseRequests(requestBody);
Expand Down

0 comments on commit b28916f

Please sign in to comment.