Skip to content

Commit

Permalink
improve formatting of templates
Browse files Browse the repository at this point in the history
TODO: generate tests for C# SDK
  • Loading branch information
dphuang2 committed Sep 29, 2023
1 parent ac0fa5b commit 399204c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,19 @@ services.AddHttpClient<YourApiClass>(httpClient =>
## Getting Started

```csharp
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{> api_doc_example}}{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
{{#apiInfo}}
{{#apis}}
{{#-first}}
{{#operations}}
{{#operation}}
{{#-first}}
{{> api_doc_example}}
{{/-first}}
{{/operation}}
{{/operations}}
{{/-first}}
{{/apis}}
{{/apiInfo}}
```

## Documentation for API Endpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace {{packageName}}.Test
[Fact]
public void ClientTest()
{
{{> api_doc_auth_partial}}
{{clientName}} client = new {{clientName}}();
{{> api_doc_auth_partial}}
Assert.NotNull(client);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ All URIs are relative to *{{{basePath}}}*

{{#operations}}
{{#operation}}
<a name="{{{operationIdLowerCase}}}"></a>

# **{{{operationId}}}**
> {{returnType}}{{^returnType}}void{{/returnType}} {{operationId}} ({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{#optionalMethodArgument}} = null{{/optionalMethodArgument}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})

{{{summary}}}{{#notes}}

Expand All @@ -35,7 +34,7 @@ try
{{#summary}}
// {{{.}}}
{{/summary}}
{{#returnType}}ApiResponse<{{{.}}}> response = {{/returnType}}apiInstance.{{{operationId}}}WithHttpInfo({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
{{#returnType}}ApiResponse<{{{.}}}> response = {{/returnType}}apiInstance.{{{operationId}}}WithHttpInfo{{> api_doc_example_execute_pass_params}};{{#returnType}}
Debug.Write("Status Code: " + response.StatusCode);
Debug.Write("Response Headers: " + response.Headers);
Debug.Write("Response Body: " + response.Data);{{/returnType}}
Expand All @@ -62,15 +61,6 @@ catch (ApiException e)

{{#returnType}}{{#returnTypeIsPrimitive}}**{{{returnType}}}**{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}[**{{returnType}}**]({{returnBaseType}}.md){{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}void (empty response body){{/returnType}}

### Authorization

{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{{name}}}](../README.md#{{{name}}}){{^-last}}, {{/-last}}{{/authMethods}}

### HTTP request headers

- **Content-Type**: {{#consumes}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/consumes}}{{^consumes}}Not defined{{/consumes}}
- **Accept**: {{#produces}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/produces}}{{^produces}}Not defined{{/produces}}

{{#responses.0}}

### HTTP response details
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@

{{#apiDocumentationAuthenticationPartial}}
{{{apiDocumentationAuthenticationPartial}}}

{{/apiDocumentationAuthenticationPartial}}
{{^apiDocumentationAuthenticationPartial}}
{{clientName}} client = new {{clientName}}();
// Configure custom BasePath if desired
client.SetBasePath("{{{basePath}}}");
{{#clientStateSetterGetterPascalCase}}
client.{{setter}}("{{upperSnakeCase}}");
{{/clientStateSetterGetterPascalCase}}
{{#clientStateWithExamplesSetterGetterPascalCase}}
client.{{setter}}("{{example}}");
{{/clientStateWithExamplesSetterGetterPascalCase}}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
// Configure HTTP basic authorization: {{{name}}}
client.SetUsername("YOUR_USERNAME");
client.SetPassword("YOUR_PASSWORD");
{{/isBasicBasic}}
{{#isBasicBearer}}
// Configure Bearer token for authorization: {{{name}}}
client.SetAccessToken("YOUR_BEARER_TOKEN");
{{/isBasicBearer}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
client.Set{{> api_key_name}}("YOUR_API_KEY");
{{/isApiKey}}
{{#isOAuth}}
{{#isApplication}}
// Configure OAuth2 credentials for "{{{flow}}}" OAuth flow
client.SetOAuthClientId(System.Environment.GetEnvironmentVariable("CLIENT_ID"));
client.SetOAuthClientSecret(System.Environment.GetEnvironmentVariable("CLIENT_SECRET"));
{{/isApplication}}
{{^isApplication}}
// Configure OAuth2 access token for authorization: {{{name}}}
client.SetAccessToken("YOUR_ACCESS_TOKEN");
{{/isApplication}}
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
{{#clientStateSetterGetterPascalCase}}
client.{{setter}}("{{upperSnakeCase}}");
{{/clientStateSetterGetterPascalCase}}
{{#clientStateWithExamplesSetterGetterPascalCase}}
client.{{setter}}("{{example}}");
{{/clientStateWithExamplesSetterGetterPascalCase}}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasicBasic}}
// Configure HTTP basic authorization
client.SetUsername("YOUR_USERNAME");
client.SetPassword("YOUR_PASSWORD");
{{/isBasicBasic}}
{{#isBasicBearer}}
// Configure token for authorization
client.SetAccessToken("YOUR_TOKEN");
{{/isBasicBearer}}
{{#isApiKey}}
// Configure API key authorization: {{{name}}}
client.Set{{> api_key_name}}("YOUR_API_KEY");
{{/isApiKey}}
{{#isOAuth}}
{{#isApplication}}
// Configure OAuth2 credentials for "{{{flow}}}" OAuth flow
client.SetOAuthClientId(System.Environment.GetEnvironmentVariable("CLIENT_ID"));
client.SetOAuthClientSecret(System.Environment.GetEnvironmentVariable("CLIENT_SECRET"));
{{/isApplication}}
{{^isApplication}}
// Configure OAuth2 access token for authorization: {{{name}}}
client.SetAccessToken("YOUR_ACCESS_TOKEN");
{{/isApplication}}
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
{{/apiDocumentationAuthenticationPartial}}
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,9 @@ namespace Example
{
public static void Main()
{
{{> api_doc_auth_partial}}
{{#useHttpClient}}
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
HttpClient httpClient = new HttpClient();
HttpClientHandler httpClientHandler = new HttpClientHandler();
var apiInstance = new {{classname}}(httpClient, config, httpClientHandler);
{{/useHttpClient}}
{{#allParams}}
{{#isPrimitiveType}}
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
var {{paramName}} = new {{{dataType}}}(); // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/isPrimitiveType}}
{{/allParams}}

try
{
{{#summary}}
// {{{.}}}
{{/summary}}
{{#returnType}}{{{.}}} result = {{/returnType}}client.{{classNamePascalCase}}.{{{operationId}}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});{{#returnType}}
Console.WriteLine(result);{{/returnType}}
}
catch (ApiException e)
{
Console.WriteLine("Exception when calling {{classname}}.{{operationId}}: " + e.Message);
Console.WriteLine("Status Code: "+ e.ErrorCode);
Console.WriteLine(e.StackTrace);
}
catch (ClientException e)
{
Console.WriteLine(e.Response.StatusCode);
Console.WriteLine(e.Response.RawContent);
Console.WriteLine(e.InnerException);
}
{{clientName}} client = new {{clientName}}();
{{> api_doc_auth_partial}}
{{> api_doc_example_execute}}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{#allParams}}
{{#isPrimitiveType}}
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
var {{paramName}} = new {{{dataType}}}(); // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
{{/isPrimitiveType}}
{{/allParams}}

try
{
{{#summary}}
// {{{.}}}
{{/summary}}
{{#returnType}}{{{.}}} result = {{/returnType}}client.{{classNamePascalCase}}.{{{operationId}}}{{> api_doc_example_execute_pass_params}};{{#returnType}}
Console.WriteLine(result);{{/returnType}}
}
catch (ApiException e)
{
Console.WriteLine("Exception when calling {{classname}}.{{operationId}}: " + e.Message);
Console.WriteLine("Status Code: "+ e.ErrorCode);
Console.WriteLine(e.StackTrace);
}
catch (ClientException e)
{
Console.WriteLine(e.Response.StatusCode);
Console.WriteLine(e.Response.RawContent);
Console.WriteLine(e.InnerException);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}})
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,28 @@ using Xunit;

using {{packageName}}.Client;
using {{packageName}}.{{apiPackage}};
{{#hasImport}}
// uncomment below to import models
//using {{packageName}}.{{modelPackage}};
{{/hasImport}}
using {{packageName}}.{{modelPackage}};

namespace {{packageName}}.Test.Api
{
/// <summary>
/// Class for testing {{classname}}
/// </summary>
/// <remarks>
/// This file is automatically generated by Konfig (https://konfigthis.com).
/// Please update the test case below to test the API endpoint.
/// </remarks>
public class {{classname}}Tests : IDisposable
{
{{^nonPublicApi}}
private {{classname}} instance;
private {{clientName}} client;

{{/nonPublicApi}}
public {{classname}}Tests()
{
{{^nonPublicApi}}
instance = new {{classname}}();
{{/nonPublicApi}}
client = new {{clientName}}();
client.SetBasePath("http://127.0.0.1:4010");
{{> api_doc_auth_partial}}
}

public void Dispose()
{
// Cleanup when everything is done.
}

/// <summary>
/// Test an instance of {{classname}}
/// </summary>
[Fact]
public void {{operationId}}InstanceTest()
{
// TODO uncomment below to test 'IsType' {{classname}}
//Assert.IsType<{{classname}}>(instance);
}
{{#operations}}
{{#operation}}

Expand All @@ -62,14 +43,7 @@ namespace {{packageName}}.Test.Api
[Fact]
public void {{operationId}}Test()
{
// TODO uncomment below to test the method and replace null with proper value
{{#allParams}}
//{{{dataType}}} {{paramName}} = null;
{{/allParams}}
//{{#returnType}}var response = {{/returnType}}instance.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{#returnType}}
//Assert.IsType<{{{.}}}>(response);
{{/returnType}}
{{> api_doc_example_execute}}
}
{{/operation}}
{{/operations}}
Expand Down

0 comments on commit 399204c

Please sign in to comment.