From 55a5ec6524971a5c906edccad9621fd0b48be85b Mon Sep 17 00:00:00 2001 From: brunomartinspro Date: Wed, 4 Mar 2020 15:50:28 +0000 Subject: [PATCH 1/2] Added Csharp template --- package.json | 2 +- templates/openapi3/code_csharp.dot | 133 +++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 templates/openapi3/code_csharp.dot diff --git a/package.json b/package.json index a9059e1a..259c5c9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "widdershins", - "version": "3.6.6", + "version": "3.6.7", "description": "OpenAPI/ Swagger / AsyncAPI / Semoasa definition to Slate / Shins compatible markdown", "main": "lib/index.js", "bin": { diff --git a/templates/openapi3/code_csharp.dot b/templates/openapi3/code_csharp.dot new file mode 100644 index 00000000..39597ad5 --- /dev/null +++ b/templates/openapi3/code_csharp.dot @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +/// <> +/// Example of Http Client +/// <> +public class HttpExample +{ + private HttpClient Client { get; set; } + + /// <> + /// Setup http client + /// <> + public HttpExample() + { + Client = new HttpClient(); + } + {{? data.methodUpper == "GET"}} + /// Make a dummy request + public async Task MakeGetRequest() + { + string url = "{{=data.url}}"; + var result = await GetAsync(url); + } + + /// Performs a GET Request + public async Task GetAsync(string url) + { + //Start the request + HttpResponseMessage response = await Client.GetAsync(url); + + //Validate result + response.EnsureSuccessStatusCode(); + + }{{? }} + {{? data.methodUpper == "POST"}} + /// Make a dummy request + public async Task MakePostRequest() + { + string url = "{{=data.url}}"; + {{? data.bodyParameter.refName !== undefined }} + string json = @"{{=data.bodyParameter.exampleValues.json.replace(new RegExp('"', "g"), '""')}}"; + {{=data.bodyParameter.refName}} content = JsonConvert.DeserializeObject(json); + await PostAsync(content, url); + {{? }} + {{? data.bodyParameter.refName === undefined }} + await PostAsync(null, url); + {{? }} + } + + /// Performs a POST Request + public async Task PostAsync({{=data.bodyParameter.refName}} content, string url) + { + //Serialize Object + StringContent jsonContent = SerializeObject(content); + + //Execute POST request + HttpResponseMessage response = await Client.PostAsync(url, jsonContent); + }{{? }} + {{? data.methodUpper == "PUT"}} + /// Make a dummy request + public async Task MakePutRequest() + { + int id = 1; + string url = "{{=data.url}}"; + + {{? data.bodyParameter.refName !== undefined }} + string json = @"{{=data.bodyParameter.exampleValues.json.replace(new RegExp('"', "g"), '""')}}"; + {{=data.bodyParameter.refName}} content = JsonConvert.DeserializeObject(json); + var result = await PutAsync(id, content, url); + {{? }} + {{? data.bodyParameter.refName === undefined }} + var result = await PutAsync(id, null, url); + {{? }} + } + + /// Performs a PUT Request + public async Task PutAsync(int id, {{=data.bodyParameter.refName}} content, string url) + { + //Serialize Object + StringContent jsonContent = SerializeObject(content); + + //Execute PUT request + HttpResponseMessage response = await Client.PutAsync(url + $"/{id}", jsonContent); + + //Return response + return await DeserializeObject(response); + }{{? }} + {{? data.methodUpper == "DELETE"}} + /// Make a dummy request + public async Task MakeDeleteRequest() + { + int id = 1; + string url = "{{=data.url}}"; + + await DeleteAsync(id, url); + } + + /// Performs a DELETE Request + public async Task DeleteAsync(int id, string url) + { + //Execute DELETE request + HttpResponseMessage response = await Client.DeleteAsync(url + $"/{id}"); + + //Return response + await DeserializeObject(response); + }{{? }} + {{? data.methodUpper == "POST" || data.methodUpper == "PUT"}} + /// Serialize an object to Json + private StringContent SerializeObject({{=data.bodyParameter.refName}} content) + { + //Serialize Object + string jsonObject = JsonConvert.SerializeObject(content); + + //Create Json UTF8 String Content + return new StringContent(jsonObject, Encoding.UTF8, "application/json"); + } + {{? }} + /// Deserialize object from request response + private async Task DeserializeObject(HttpResponseMessage response) + { + //Read body + string responseBody = await response.Content.ReadAsStringAsync(); + + //Deserialize Body to object + var result = JsonConvert.DeserializeObject(responseBody); + } +} From 416b5c641a9401c2b5e7b3a435329324e11513ba Mon Sep 17 00:00:00 2001 From: Mike Ralphson Date: Sat, 28 Mar 2020 19:30:38 +0000 Subject: [PATCH 2/2] PRs should not update version in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 259c5c9c..a9059e1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "widdershins", - "version": "3.6.7", + "version": "3.6.6", "description": "OpenAPI/ Swagger / AsyncAPI / Semoasa definition to Slate / Shins compatible markdown", "main": "lib/index.js", "bin": {