Skip to content

Commit

Permalink
refactor: used code style
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznecovIT committed Jun 17, 2024
1 parent 61e34f0 commit 6ee03c7
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 183 deletions.
18 changes: 9 additions & 9 deletions src/Tochka.JsonRpc.Client/IJsonRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Text.Json;
using JetBrains.Annotations;
using Tochka.JsonRpc.Client.Models;
using Tochka.JsonRpc.Client.Models.Batch;
using Tochka.JsonRpc.Client.Models.Single;
using Tochka.JsonRpc.Client.Models.BatchResult;
using Tochka.JsonRpc.Client.Models.SingleResult;
using Tochka.JsonRpc.Common.Models.Id;
using Tochka.JsonRpc.Common.Models.Request;

Expand Down Expand Up @@ -155,7 +155,7 @@ Task<ISingleJsonRpcResult> SendRequest<TParams>(string requestUrl, IRpcId id, st
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult> SendRequest<TParams>(IRpcId id, string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class;

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC Rpc typed response
/// </summary>
Expand Down Expand Up @@ -217,12 +217,12 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string req
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class;

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <typeparam name="TParams">Type of params</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="id">JSON-RPC request id. Can be null</param>
/// <param name="method">JSON-RPC method</param>
Expand All @@ -240,7 +240,7 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string req
/// Send request to BaseUrl. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <typeparam name="TParams">Type of params</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="id">JSON-RPC request id. Can be null</param>
/// <param name="method">JSON-RPC method</param>
/// <param name="parameters">JSON-RPC params - This member MAY be omitted</param>
Expand All @@ -252,7 +252,7 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string req
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(IRpcId id, string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class;

/// <summary>
/// Send batch of requests or notifications to given url. Expects HTTP 200 with batch JSON-RPC response if batch contains at least one request
/// </summary>
Expand Down Expand Up @@ -282,7 +282,7 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(IRpcId id,
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="calls">JSON-RPC requests or notifications</param>
/// <param name="cancellationToken"></param>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body deserialized as single response, response count does not match requests in batch</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Expand All @@ -294,7 +294,7 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(IRpcId id,
/// </summary>
/// <param name="calls">JSON-RPC requests or notifications</param>
/// <param name="cancellationToken"></param>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body deserialized as single response, response count does not match requests in batch</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Expand Down
27 changes: 13 additions & 14 deletions src/Tochka.JsonRpc.Client/JsonRpcClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Tochka.JsonRpc.Client.Models;
using Tochka.JsonRpc.Client.Models.Batch;
using Tochka.JsonRpc.Client.Models.Single;
using Tochka.JsonRpc.Client.Models.BatchResult;
using Tochka.JsonRpc.Client.Models.SingleResult;
using Tochka.JsonRpc.Client.Services;
using Tochka.JsonRpc.Common;
using Tochka.JsonRpc.Common.Models.Id;
Expand Down Expand Up @@ -148,7 +148,7 @@ public async Task<ISingleJsonRpcResult> SendRequest<TParams>(IRpcId id, string m
var request = new Request<TParams>(id, method, parameters);
return await SendRequestInternal(null, request, cancellationToken);
}

/// <inheritdoc />
public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string requestUrl, Request<TParams> request, CancellationToken cancellationToken)
where TParams : class
Expand All @@ -164,7 +164,7 @@ public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TRespons
/// <inheritdoc />
public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string requestUrl, string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class
where TResponse : class
{
var id = RpcIdGenerator.GenerateId();
Log.LogTrace("Generated request id [{requestId}]", id);
Expand All @@ -175,7 +175,7 @@ public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TRespons
/// <inheritdoc />
public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class
where TResponse : class
{
var id = RpcIdGenerator.GenerateId();
Log.LogTrace("Generated request id [{requestId}]", id);
Expand All @@ -186,7 +186,7 @@ public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TRespons
/// <inheritdoc />
public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(string requestUrl, IRpcId id, string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class
where TResponse : class
{
var request = new Request<TParams>(id, method, parameters);
return await SendRequestInternal<TParams, TResponse>(requestUrl, request, cancellationToken);
Expand All @@ -195,20 +195,20 @@ public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TRespons
/// <inheritdoc />
public async Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(IRpcId id, string method, TParams? parameters, CancellationToken cancellationToken)
where TParams : class
where TResponse : class
where TResponse : class
{
var request = new Request<TParams>(id, method, parameters);
return await SendRequestInternal<TParams, TResponse>(null, request, cancellationToken);
}

/// <inheritdoc />
public async Task<IBatchJsonRpcResult?> SendBatch(string requestUrl, IEnumerable<ICall> calls, CancellationToken cancellationToken) =>
await SendBatchInternal(requestUrl, calls, cancellationToken);

/// <inheritdoc />
public async Task<IBatchJsonRpcResult?> SendBatch(IEnumerable<ICall> calls, CancellationToken cancellationToken) =>
await SendBatchInternal(null, calls, cancellationToken);

/// <inheritdoc />
public async Task<IBatchJsonRpcResult<TResponse>?> SendBatch<TResponse>(string requestUrl, IEnumerable<ICall> calls, CancellationToken cancellationToken) =>
await SendBatchInternal<TResponse>(requestUrl, calls, cancellationToken);
Expand Down Expand Up @@ -265,11 +265,11 @@ internal virtual async Task<ISingleJsonRpcResult> SendRequestInternal<TParams>(s
throw new JsonRpcException(message, context);
}
}

// internal virtual for mocking in tests
internal virtual async Task<ISingleJsonRpcResult<TResponse>> SendRequestInternal<TParams, TResponse>(string? requestUrl, Request<TParams> request, CancellationToken cancellationToken)
where TParams : class
where TResponse : class
where TResponse : class
{
var (context, contentString) = await PrepareInternalRequestContext(requestUrl, request, cancellationToken);
var responseWrapper = ParseBody(contentString);
Expand All @@ -285,7 +285,7 @@ internal virtual async Task<ISingleJsonRpcResult<TResponse>> SendRequestInternal
throw new JsonRpcException(message, context);
}
}

private async Task<(IJsonRpcCallContext, string)> PrepareInternalRequestContext<TParams>(string? requestUrl, Request<TParams> request,
CancellationToken cancellationToken) where TParams : class
{
Expand All @@ -301,7 +301,7 @@ internal virtual async Task<ISingleJsonRpcResult<TResponse>> SendRequestInternal
context.WithHttpContent(httpResponseMessage.Content, contentString);
return (context, contentString);
}

// internal virtual for mocking in tests
internal virtual async Task<IBatchJsonRpcResult?> SendBatchInternal(string? requestUrl, IEnumerable<ICall> calls, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -389,7 +389,6 @@ internal virtual async Task<ISingleJsonRpcResult<TResponse>> SendRequestInternal
throw new JsonRpcException(message2, context);
}
}


// internal virtual for mocking in tests
internal virtual async Task<HttpResponseMessage> SendInternal(string? requestUrl, ICall call, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Tochka.JsonRpc.Common.Models.Id;
using Tochka.JsonRpc.Common.Models.Response;

namespace Tochka.JsonRpc.Client.Models.Batch;
namespace Tochka.JsonRpc.Client.Models.BatchResult;

/// <summary>
/// Extensions for batch JSON-RPC responses
Expand All @@ -22,4 +22,4 @@ public static bool TryGetResponse(this IReadOnlyDictionary<IRpcId, IResponse> re
IRpcId id,
[NotNullWhen(true)] out IResponse? response) =>
responses.TryGetValue(id, out response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
using Tochka.JsonRpc.Common.Models.Response;
using Tochka.JsonRpc.Common.Models.Response.Untyped;

namespace Tochka.JsonRpc.Client.Models.Batch;
namespace Tochka.JsonRpc.Client.Models.BatchResult;

/// <inheritdoc cref="Tochka.JsonRpc.Client.Models.Batch.IBatchJsonRpcResult" />
/// <inheritdoc cref="IBatchJsonRpcResult" />
[PublicAPI]
public sealed class BatchJsonRpcResult : BatchJsonRpcResult<object>, IBatchJsonRpcResult
{
/// <summary></summary>
public BatchJsonRpcResult(IJsonRpcCallContext context, JsonSerializerOptions headersJsonSerializerOptions,
JsonSerializerOptions dataJsonSerializerOptions) : base(context, headersJsonSerializerOptions, dataJsonSerializerOptions)
{ }
{
}

/// <inheritdoc />
public TResponse? GetResponseOrThrow<TResponse>(IRpcId id) => Advanced.GetResponseOrThrow<TResponse>(id);
Expand All @@ -31,22 +32,22 @@ public class BatchJsonRpcResult<TResponse> : IBatchJsonRpcResult<TResponse>
/// Context with all information about request and response
/// </summary>
protected readonly IJsonRpcCallContext Context;

/// <summary>
/// JsonSerializerOptions used to process JSON-RPC root object. Does not affect JSON-RPC params/result/error.data
/// </summary>
protected readonly JsonSerializerOptions HeadersJsonSerializerOptions;

/// <summary>
/// JsonSerializerOptions used to process JSON-RPC params/result/error.data. Does not affect JSON-RPC root object
/// </summary>
protected readonly JsonSerializerOptions DataJsonSerializerOptions;

/// <summary>
/// Collection of responses if call was batch
/// </summary>
protected readonly Dictionary<IRpcId, IResponse> Responses;

/// <inheritdoc />
public IBatchJsonRpcResultAdvanced Advanced { get; init; }

Expand All @@ -70,7 +71,7 @@ public BatchJsonRpcResult(IJsonRpcCallContext context, JsonSerializerOptions hea

/// <inheritdoc />
public TResponse? AsResponse(IRpcId id) => Advanced.AsResponse<TResponse>(id);

/// <inheritdoc />
public bool HasError(IRpcId id)
{
Expand All @@ -81,7 +82,7 @@ public bool HasError(IRpcId id)

return response is UntypedErrorResponse;
}

[ExcludeFromCodeCoverage]
private static Dictionary<IRpcId, IResponse> CreateDictionary(IEnumerable<IResponse>? items) =>
items?.ToDictionary(static x => x.Id, static x => x) ?? new Dictionary<IRpcId, IResponse>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Tochka.JsonRpc.Common.Models.Response.Errors;
using Tochka.JsonRpc.Common.Models.Response.Untyped;

namespace Tochka.JsonRpc.Client.Models.Batch;
namespace Tochka.JsonRpc.Client.Models.BatchResult;

/// <inheritdoc />
public class BatchJsonRpcResultAdvanced : IBatchJsonRpcResultAdvanced
Expand Down Expand Up @@ -53,7 +53,7 @@ public BatchJsonRpcResultAdvanced(IJsonRpcCallContext context, Dictionary<IRpcId
_ => default
};
}

/// <inheritdoc />
public Error<JsonDocument>? AsAnyError(IRpcId id)
{
Expand Down Expand Up @@ -81,4 +81,4 @@ public BatchJsonRpcResultAdvanced(IJsonRpcCallContext context, Dictionary<IRpcId
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public Error<ExceptionInfo>? AsErrorWithExceptionInfo(IRpcId id) => AsTypedError<ExceptionInfo>(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using Tochka.JsonRpc.Common.Models.Id;

namespace Tochka.JsonRpc.Client.Models.Batch;
namespace Tochka.JsonRpc.Client.Models.BatchResult;

/// <summary>
/// Result of batch JSON-RPC request
Expand Down Expand Up @@ -31,7 +31,7 @@ public interface IBatchJsonRpcResult
/// <param name="id">Response id</param>
/// <exception cref="JsonRpcException">if no response with given id or response has error</exception>
bool HasError(IRpcId id);

/// <summary>
/// Advanced data for complex work with batch Result of JSON-RPC request
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Tochka.JsonRpc.Common.Models.Id;
using Tochka.JsonRpc.Common.Models.Response.Errors;

namespace Tochka.JsonRpc.Client.Models.Batch;
namespace Tochka.JsonRpc.Client.Models.BatchResult;

/// <summary>
/// Advanced data for complex work with batch Result of JSON-RPC request
Expand All @@ -26,7 +26,7 @@ public interface IBatchJsonRpcResultAdvanced
/// <typeparam name="TResponse">Type to deserialize result to</typeparam>
/// <returns>Result or null if no response with given id or response has error</returns>
TResponse? AsResponse<TResponse>(IRpcId id);

/// <summary>
/// Get error without deserializing data
/// </summary>
Expand All @@ -48,4 +48,4 @@ public interface IBatchJsonRpcResultAdvanced
/// <param name="id">Response id</param>
/// <returns>Error or null if no response with given id or response has no error</returns>
Error<ExceptionInfo>? AsErrorWithExceptionInfo(IRpcId id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using JetBrains.Annotations;

namespace Tochka.JsonRpc.Client.Models.Single;
namespace Tochka.JsonRpc.Client.Models.SingleResult;

/// <summary>
/// Result of single JSON-RPC request
Expand Down Expand Up @@ -47,13 +47,13 @@ public interface ISingleJsonRpcResult<out TResponse>
/// </summary>
/// <exception cref="JsonRpcException">if response has error</exception>
TResponse? GetResponseOrThrow();

/// <summary>
/// Get deserialized to typed response result
/// </summary>
/// <returns>Result or null if response has error</returns>
TResponse? AsResponse();

/// <summary>
/// Check if response has error
/// </summary>
Expand All @@ -64,4 +64,4 @@ public interface ISingleJsonRpcResult<out TResponse>
/// </summary>
/// <returns></returns>
ISingleJsonRpcResultAdvanced Advanced { get; init; }
}
}
Loading

0 comments on commit 6ee03c7

Please sign in to comment.