Is there a way to generate sample code for how to call the API in REST way?
Description
I'm new to code-gen, and I'm trying to find a way to generate code samples for different languages, to give an example how to call the API in REST.
I have tried to generate the csharp client side examples as below:
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
namespace Example
{
public class DeletePetExample
{
public void main()
{
// Configure OAuth2 access token for authorization: petstore_auth
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
var apiInstance = new PetApi();
var petId = 789; // long? | Pet id to delete
var apiKey = apiKey_example; // string | (optional)
try
{
// Deletes a pet
apiInstance.DeletePet(petId, apiKey);
}
catch (Exception e)
{
Debug.Print("Exception when calling PetApi.DeletePet: " + e.Message );
}
}
}
}
But I'm finding how to call the REST API in different languages, refer to restdb code examples,
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
And Java
HttpResponse response = Unirest.get("https://inventory-fac4.restdb.io/appdeploy/motorbikes")
.header("x-apikey", "560bd47058e7ab1b2648f4e7")
.header("cache-control", "no-cache")
.asString();
Do you have any suggestions? Thanks!
Swagger-codegen version
2.2.3
Swagger declaration file content or url
http://petstore.swagger.io/v2/swagger.json
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l csharp -o client/petstore/csharp
Steps to reproduce
- Generate the samples for csharp.
- Find the samples for how to call API in REST way.
Related issues/PRs
Suggest a fix/enhancement
@hellosnow I think you're referring to the auto-generated documentation: https://github.com/swagger-api/swagger-codegen/issues/2359
For C#, the auto-generated README is a good starting point. Here is an example: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/csharp/SwaggerClient/docs/PetApi.md#updatepetwithform
Is that what you're looking for?
@wing328 I got the same example in documentation as your reference 😄 But what I'm looking for is the functionality to generated the stub code how to make the REST API call, as following. Any suggestion? Not sure whether it's the right place to ask. 😃
Refer to 1. https://restdb.io/docs/rest-api-code-examples
var client = new RestClient("https://inventory-fac4.restdb.io/rest/motorbikes");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-apikey", "560bd47058e7ab1b2648f4e7");
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);
- https://app.apiary.io/huangxueyuan/editor
//Common testing requirement. If you are consuming an API in a sandbox/test region, uncomment this line of code ONLY for non production uses.
//System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//Be sure to run "Install-Package Microsoft.Net.Http" from your nuget command line.
using System;
using System.Net.Http;
var baseAddress = new Uri("https://private-ad2e3-huangxueyuan.apiary-proxy.com/");
using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("key", "value");
using(var response = await httpClient.GetAsync("questions"))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
- https://dev.applicationinsights.io/quickstart
private const string URL = "https://api.applicationinsights.io/v1/apps/{0}/{1}/{2}?{3}";
public static string GetTelemetry(string appid, string apikey,
string queryType, string queryPath, string parameterString)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(URL, appid, queryType, queryPath, parameterString);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
return response.ReasonPhrase;
}
}
...
var json = GetTelemetry
("DEMO_APP", "DEMO_KEY",
"metrics", "requests/duration", "interval=PT1H");
@hellosnow so you're looking for ways to make raw HTTP calls to the REST API backend instead of using the C# SDK to make the call?
@wing328 yes it is. Look like it's out of the scope of code-gen, is it?
@hellosnow with the right templates, the codegen can generate this. Actually it does generate this kind of code but wraps it in API methods for convenience.
@cbornet do you have any guidance on this? 😄
@hellosnow you can use the -t CLI option to provide customized templates. The default template can be found in https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/csharp
To customize it, you can review the mustache template variables using -DdebugOperations: https://github.com/swagger-api/swagger-codegen/wiki/Mustache-Template-Variables
Would these help?