swagger-api / swagger-api/swagger-codegen
[Csharp/DotNet client] Feature request - option to use asynchronous callbacks
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Add an option which allows to generate asynchronous API calls instead of the current synchronous calls for the csharp/dotnet client. I haven't been able to find any options which enables this feature, if it exists somewhere please let me know 😄
Swagger declaration file content or url
Example declaration file used in suggestion to how to implement it.
swagger: '2.0'
tags:
- name: Server
description: Operations available to retrieve information about a server
paths:
/servers/{server_id}:
get:
tags:
- Server
summary: Get basic server information
operationId: getServer
description: |
This returns basic information about a specific server
parameters:
- name: server_id
in: path
description: The server id to retrieve the basic information about
type: integer
required: true
Related issues/PRs
- Maybe https://github.com/swagger-api/swagger-codegen/issues/9511
- It seems like #1889 does want to include asynchronous interface but is 2 years old.
Tried keywords such as .net, dotnet, csharp, callbacks, asynchronous, async
Suggest a fix/enhancement
This is what I currently change on the generated code and what could be generated alongside/in replacement.
Add a method called CallApiAsync to the generated ApiClient
public void CallApiAsync(String path, RestSharp.Method method, Dictionary<String, String> queryParams, String postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, String[] authSettings, Action<IRestResponse> callback)
{
var request = new RestRequest(path, method);
UpdateParamsForAuth(queryParams, headerParams, authSettings);
// add default header, if any
foreach (var defaultHeader in _defaultHeaderMap)
request.AddHeader(defaultHeader.Key, defaultHeader.Value);
// add header parameter, if any
foreach (var param in headerParams)
request.AddHeader(param.Key, param.Value);
// add query parameter, if any
foreach (var param in queryParams)
request.AddParameter(param.Key, param.Value, ParameterType.GetOrPost);
// add form parameter, if any
foreach (var param in formParams)
request.AddParameter(param.Key, param.Value, ParameterType.GetOrPost);
// add file parameter, if any
foreach (var param in fileParams)
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
if (postBody != null) // http body (model) parameter
request.AddParameter("application/json", postBody, ParameterType.RequestBody);
RestClient.ExecuteAsync(request, callback);
}
Change the generated client interface form from
Server GetServer (int? serverId);
to
void GetServer (int? serverId, Action<ApiException, Server> callback);
Change the generated client methods from:
public Server GetServer (int? serverId)
{
// verify the required parameter 'serverId' is set
if (serverId == null) throw new ApiException(400, "Missing required parameter 'serverId' when calling GetServer");
var path = "/servers/{server_id}";
path = path.Replace("{format}", "json");
path = path.Replace("{" + "server_id" + "}", ApiClient.ParameterToString(serverId));
var queryParams = new Dictionary<String, String>();
var headerParams = new Dictionary<String, String>();
var formParams = new Dictionary<String, String>();
var fileParams = new Dictionary<String, FileParameter>();
String postBody = null;
// make the HTTP request
IRestResponse response = (IRestResponse) ApiClient.CallApi(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams, authSettings);
if (((int)response.StatusCode) >= 400)
throw new ApiException ((int)response.StatusCode, "Error calling GetServer: " + response.Content, response.Content);
else if (((int)response.StatusCode) == 0)
throw new ApiException ((int)response.StatusCode, "Error calling GetServer: " + response.ErrorMessage, response.ErrorMessage);
return (Server) ApiClient.Deserialize(response.Content, typeof(Server), response.Headers);
}
to
public void GetServer (int? serverId, Action<ApiException, Server> callback)
{
// verify the required parameter 'serverId' is set
if (serverId == null) throw new ApiException(400, "Missing required parameter 'serverId' when calling GetServer");
var path = "/servers/{server_id}";
path = path.Replace("{format}", "json");
path = path.Replace("{" + "server_id" + "}", ApiClient.ParameterToString(serverId));
var queryParams = new Dictionary<String, String>();
var headerParams = new Dictionary<String, String>();
var formParams = new Dictionary<String, String>();
var fileParams = new Dictionary<String, FileParameter>();
String postBody = null;
// make the HTTP request
ApiClient.CallApiAsync(path, Method.GET, queryParams, postBody, headerParams, formParams, fileParams, authSettings, (response) =>
{
ApiException exception = null;
if (((int)response.StatusCode) >= 400)
exception = new ApiException((int)response.StatusCode, "Error calling GetServer: " + response.Content, response.Content);
else if (((int)response.StatusCode) == 0)
exception = new ApiException((int)response.StatusCode, "Error calling GetServer: " + response.ErrorMessage, response.ErrorMessage);
callback(exception, (Server)ApiClient.Deserialize(response.Content, typeof(Server), response.Headers));
});
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the Csharp/DotNet client generator's existing synchronous ApiClient and generated client interface and methods. Compare the proposed CallApiAsync and callback signatures with related issue #1889, then determine how an option should select asynchronous generation. Done should include a defined option and consistent asynchronous output for the ApiClient and generated client methods.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100