Skip to content

Commit

Permalink
Merge pull request #114 from tochka-public/feature/CSHARP-77
Browse files Browse the repository at this point in the history
feat: can work with requests without params, JsonRpc.C…
  • Loading branch information
Rast1234 authored Sep 23, 2024
2 parents d8d0c5c + 2167a0d commit 0ff7734
Show file tree
Hide file tree
Showing 18 changed files with 886 additions and 140 deletions.
31 changes: 30 additions & 1 deletion docs/en/client/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Examples of basic JSON-RPC calls with default configuration
Client's method
</td>
<td>
Sent JSON-RPC call
Sent JSON-RPC call
</td>
</tr>

Expand Down Expand Up @@ -55,6 +55,35 @@ var response = await myClient.CreateUser("user_login", token);
<tr>
<td valign="top">

Request without params
```cs
public async Task<UserModel[]> GetUsers(CancellationToken token)
{
var response = await SendRequest("users.get", token);
return response.GetResponseOrThrow<UserModel[]>();
}

var response = await myClient.GetUsers(token);
```

</td>
<td>

```json
{
"id": "56249f26-9748-461c-aeaf-b74b6a244ac6",
"method": "users.get",
"params": null,
"jsonrpc": "2.0"
}
```

</td>
</tr>

<tr>
<td valign="top">

Notification
```cs
public async Task CreateUser(string login, CancellationToken token) =>
Expand Down
29 changes: 29 additions & 0 deletions docs/ru/client/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ var response = await myClient.CreateUser("user_login", token);
<tr>
<td valign="top">

Request without params
```cs
public async Task<UserModel[]> GetUsers(CancellationToken token)
{
var response = await SendRequest("users.get", token);
return response.GetResponseOrThrow<UserModel[]>();
}

var response = await myClient.GetUsers(token);
```

</td>
<td>

```json
{
"id": "56249f26-9748-461c-aeaf-b74b6a244ac6",
"method": "users.get",
"params": null,
"jsonrpc": "2.0"
}
```

</td>
</tr>

<tr>
<td valign="top">

Notification
```cs
public async Task CreateUser(string login, CancellationToken token) =>
Expand Down
8 changes: 8 additions & 0 deletions run_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set -e
cd "$(dirname "$0")"

rm -rf coverage
dotnet test -p:CollectCoverage=true -p:CoverletOutput="../../../coverage/" -p:MergeWith="../../../coverage/coverage.json" -p:CoverletOutputFormat=\"json,opencover\" -p:ExcludeByAttribute="GeneratedCode" -maxcpucount:1 src
reportgenerator -reports:coverage/coverage.opencover.xml -targetdir:coverage/report

# see result in coverage/report/index.html
150 changes: 150 additions & 0 deletions src/Tochka.JsonRpc.Client/IJsonRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,156 @@ Task<ISingleJsonRpcResult<TResponse>> SendRequest<TParams, TResponse>(IRpcId id,
where TParams : class
where TResponse : class;

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC Rpc response
/// </summary>
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="request">JSON-RPC request</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult> SendRequest(string requestUrl, Request request, CancellationToken cancellationToken);

/// <summary>
/// Send request to BaseUrl. Expects HTTP 200 with JSON-RPC response
/// </summary>
/// <param name="request">JSON-RPC request</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Task<ISingleJsonRpcResult> SendRequest(Request request, CancellationToken cancellationToken);

/// <summary>
/// Send request to given url. Id is generated with IJsonRpcIdGenerator. Expects HTTP 200 with JSON-RPC response
/// </summary>
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="method">JSON-RPC method</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult> SendRequest(string requestUrl, string method, CancellationToken cancellationToken);

/// <summary>
/// Send request to BaseUrl. Id is generated with IJsonRpcIdGenerator. Expects HTTP 200 with JSON-RPC response
/// </summary>
/// <param name="method">JSON-RPC method</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Task<ISingleJsonRpcResult> SendRequest(string method, CancellationToken cancellationToken);

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC response
/// </summary>
/// <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>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult> SendRequest(string requestUrl, IRpcId id, string method, CancellationToken cancellationToken);

/// <summary>
/// Send request to BaseUrl. Expects HTTP 200 with JSON-RPC response
/// </summary>
/// <param name="id">JSON-RPC request id. Can be null</param>
/// <param name="method">JSON-RPC method</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult> SendRequest(IRpcId id, string method, CancellationToken cancellationToken);

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC Rpc typed response
/// </summary>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="request">JSON-RPC request</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(string requestUrl, Request request, CancellationToken cancellationToken)
where TResponse : class;

/// <summary>
/// Send request to BaseUrl. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="request">JSON-RPC request</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(Request request, CancellationToken cancellationToken)
where TResponse : class;

/// <summary>
/// Send request to given url. Id is generated with IJsonRpcIdGenerator. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="requestUrl">Relative path, appended to BaseAddress. Must not start with '/'</param>
/// <param name="method">JSON-RPC method</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(string requestUrl, string method, CancellationToken cancellationToken)
where TResponse : class;

/// <summary>
/// Send request to BaseUrl. Id is generated with IJsonRpcIdGenerator. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <typeparam name="TResponse">Type of response</typeparam>
/// <param name="method">JSON-RPC method</param>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(string method, CancellationToken cancellationToken)
where TResponse : class;

/// <summary>
/// Send request to given url. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <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>
/// <param name="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(string requestUrl, IRpcId id, string method, CancellationToken cancellationToken)
where TResponse : class;

/// <summary>
/// Send request to BaseUrl. Expects HTTP 200 with JSON-RPC typed response
/// </summary>
/// <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="cancellationToken"></param>
/// <returns>Result to be inspected for response data or errors</returns>
/// <exception cref="JsonRpcException">When HTTP status code is not 200, body is empty or deserialized as batch response</exception>
/// <exception cref="JsonException">When reading or deserializing JSON from body failed</exception>
/// <exception cref="System.ArgumentException">When requestUrl starts with '/'</exception>
Task<ISingleJsonRpcResult<TResponse>> SendRequest<TResponse>(IRpcId id, string method, CancellationToken cancellationToken)
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
Loading

0 comments on commit 0ff7734

Please sign in to comment.