diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppTinyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppTinyClientCodegen.java index ef5329ad8ea..49d70eda08b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppTinyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppTinyClientCodegen.java @@ -152,17 +152,16 @@ public class CppTinyClientCodegen extends AbstractCppCodegen implements CodegenC // SERVICES: Helpers supportingFiles.add(new SupportingFile("service/Response.h.mustache", serviceFolder, "Response.h")); - supportingFiles.add(new SupportingFile("service/AbstractService.h.mustache", serviceFolder, "AbstractService.h")); - supportingFiles.add(new SupportingFile("service/AbstractService.cpp.mustache", serviceFolder, "AbstractService.cpp")); + supportingFiles.add(new SupportingFile("service/Service.h.mustache", serviceFolder, "Service.h")); + supportingFiles.add(new SupportingFile("service/Service.cpp.mustache", serviceFolder, "Service.cpp")); // Main - supportingFiles.add(new SupportingFile("main.mustache", CppTinyClientCodegen.sourceFolder, "main.cpp")); + supportingFiles.add(new SupportingFile("main.mustache", CppTinyClientCodegen.sourceFolder, "main.cpp")); // TODO no overwrite // Config files supportingFiles.add(new SupportingFile("README.mustache", rootFolder, "README.md")); - supportingFiles.add(new SupportingFile("platformio.ini.mustache", rootFolder, "platformio.ini")); - supportingFiles.add(new SupportingFile("root.cert.mustache", rootFolder, "root.cert")); - supportingFiles.add(new SupportingFile("README.mustache", rootFolder, "README.md")); + supportingFiles.add(new SupportingFile("platformio.ini.mustache", rootFolder, "platformio.ini")); // TODO no overwrite + supportingFiles.add(new SupportingFile("root.cert.mustache", rootFolder, "root.cert")); // TODO no overwrite supportingFiles.add(new SupportingFile("pre_compiling_bourne.py.mustache", rootFolder, "pre_compiling_bourne.py")); defaultIncludes = new HashSet( @@ -232,7 +231,6 @@ public class CppTinyClientCodegen extends AbstractCppCodegen implements CodegenC @Override public void processOpts() { super.processOpts(); - // Throw exception if http and esp8266 // -- --additional-properties=controller= if (additionalProperties.containsKey(MICROCONTROLLER)) { diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/platformio.ini.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/platformio.ini.mustache index 1e364ee0ab2..9fb13516eff 100644 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/platformio.ini.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/platformio.ini.mustache @@ -3,7 +3,7 @@ platform = espressif32 board = nodemcu-32s framework = arduino -lib_deps = https://github.com/steinwurf/bourne.git +lib_deps = https://github.com/steinwurf/bourne.git#9.0.0 extra_scripts = pre_compiling_bourne.py {{/isESP32}} @@ -12,7 +12,7 @@ extra_scripts = pre_compiling_bourne.py platform = espressif8266 board = d1_mini framework = arduino -lib_deps = https://github.com/steinwurf/bourne.git +lib_deps = https://github.com/steinwurf/bourne.git#9.0.0 build_flags = -fexceptions build_unflags = -fno-exceptions extra_scripts = pre_compiling_bourne.py diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/root.cert.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/root.cert.mustache index 59d760f1e16..bb78f18edc6 100644 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/root.cert.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/root.cert.mustache @@ -2,7 +2,7 @@ // Below is two examples of root certificates. // Let's encrypt root certificate -/** + "-----BEGIN CERTIFICATE-----\n" \ "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ @@ -25,10 +25,10 @@ "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ "-----END CERTIFICATE-----\n" -*/ + // Amazon_Root_CA_1.pem -/** + "-----BEGIN CERTIFICATE-----\n" \ "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" \ "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \ @@ -49,4 +49,5 @@ "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" \ "rqXRfboQnoZsG4q5WTP468SQvvG5\n" \ "-----END CERTIFICATE-----\n" \ -*/ + + diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.cpp.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.cpp.mustache deleted file mode 100644 index 0c056c85ae3..00000000000 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.cpp.mustache +++ /dev/null @@ -1,15 +0,0 @@ -#include "AbstractService.h" -#include "Arduino.h" - -{{#isESP8266}} -void Tiny::AbstractService::begin(std::string url){ - http.begin(client, String(url.c_str())); -} -{{/isESP8266}} - - -{{#isESP32}} -void Tiny::AbstractService::begin(std::string url){ - http.begin(String(url.c_str()), test_root_ca); //HTTPS connection -} -{{/isESP32}} diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.h.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.h.mustache deleted file mode 100644 index 7dc46b5b20f..00000000000 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/service/AbstractService.h.mustache +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ -#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ -{{#isESP8266}} -#include -#include -{{/isESP8266}} - -{{#isESP32}} -#include "HTTPClient.h" -{{/isESP32}} -#include "Response.h" -namespace Tiny { - -/** -* Class -* Generated with openapi::tiny-cpp-client -*/ -class AbstractService { -public: -HTTPClient http; -{{#isESP8266}} -WiFiClient client; -{{/isESP8266}} -std::string basepath = "https://petstore3.swagger.io/api/v3"; // TODO: change to your url - -void begin(std::string url); - -{{#isESP32}} -// Go and comment out a certificate in root.cert, if you get an error here -// Certificate from file -const char* test_root_ca = -#include "../../root.cert" -; -{{/isESP32}} - -}; // end class -}// namespace Tinyclient - -#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */ diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.cpp.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.cpp.mustache new file mode 100644 index 00000000000..d164c311e70 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.cpp.mustache @@ -0,0 +1,75 @@ +#include "Service.h" +#include "Arduino.h" + +{{#isESP8266}} +void Tiny::Service::begin(std::string url){ + http.begin(client, String(url.c_str())); + + // reset params + queryParams.begin(); + formParams.begin(); +} +{{/isESP8266}} +{{#isESP32}} +void Tiny::Service::begin(std::string url){ + http.begin(String(url.c_str()), test_root_ca); //HTTPS connection + + + // reset params + queryParams.begin(); + formParams.begin(); +} +{{/isESP32}} + +void Tiny::Service::addHeader(std::string key, std::string value){ + http.addHeader(String(key.c_str()), String(value.c_str())); +} + +void Tiny::Service::addQueryParam(std::string key, std::string value){ + formParams.push_back(std::make_tuple(key, value)); +} + +void Tiny::Service::addFormParam(std::string key, std::string value){ + formParams.push_back(std::make_tuple(key, value)); +} + +int Tiny::Service::sendRequest(std::string url, const char * type, uint8_t * payload, size_t size){ + this->url = url; + prepareRequest(); + + begin(this->url); + + int httpCode = http.sendRequest(type, payload, size); + return httpCode; +} + +void Tiny::Service::prepareRequest(){ + if (!queryParams.empty()){ + this->url += "?" + encodeKeyValueTuple(this->queryParams); + } + + if (!formParams.empty()){ + this->payload = encodeKeyValueTuple(this->formParams); + } +} + +std::string Tiny::Service::encodeKeyValueTuple(std::list> params){ + std::string encoded = ""; + for (auto const& tuple : params) { + encoded += std::get<0>(tuple) + "=" + std::get<1>(tuple) + "&"; + } + + // Remove last '&' char from url + if (! encoded.empty()) + encoded.pop_back(); + + return encoded; +} + + +String Tiny::Service::getResponseBody(){ + String respBody = http.getString(); + http.end(); + return respBody; +} + diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.h.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.h.mustache new file mode 100644 index 00000000000..a2f70c1a108 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/service/Service.h.mustache @@ -0,0 +1,54 @@ +#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ +#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ +{{#isESP8266}} +#include +#include +{{/isESP8266}} + +{{#isESP32}} +#include "HTTPClient.h" +{{/isESP32}} +#include "Response.h" +#include +namespace Tiny { + +/** +* Class +* Generated with openapi::tiny-cpp-client +*/ +class Service { + public: + HTTPClient http; + {{#isESP8266}} + WiFiClient client; + {{/isESP8266}} + std::string basepath = "https://petstore3.swagger.io/api/v3"; // TODO: change to your url + std::string url = ""; + std::string payload = ""; + std::list> queryParams; + std::list> formParams; + + int sendRequest(std::string url, const char * type, uint8_t * payload, size_t size); + String getResponseBody(); + + void addQueryParam(std::string key, std::string value); + void addFormParam(std::string key, std::string value); + void addHeader(std::string key, std::string value); + + {{#isESP32}} + // Go and comment out a certificate in root.cert, if you get an error here + // Certificate from file + const char* test_root_ca = + #include "../../root.cert" + ; + {{/isESP32}} + + private: + void begin(std::string url); + void prepareRequest(); + std::string encodeKeyValueTuple(std::list> params); + +}; // end class +}// namespace Tinyclient + +#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */ diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-body.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-body.mustache index c151d92c4d8..78e5d0bc8cf 100644 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-body.mustache @@ -40,10 +40,44 @@ using namespace Tiny; {{! Method start}} { std::string url = basepath + "{{{path}}}"; //{{#pathParams}}{{{paramName}}} {{/pathParams}} + + + // Headers | {{#headerParams}} {{paramName}} {{/headerParams}} + {{#headerParams}} + addHeader("{{baseName}}",{{paramName}}); + {{/headerParams}} + // Query | {{#queryParams}}{{paramName}} {{/queryParams}} - // Headers | {{#headerParams}}{{paramName}} {{/headerParams}} + {{#queryParams}} + {{#isArray}} + for (auto &x : {{paramName}}){ + addQueryParam("{{baseName}}", std::string(x)); + } + {{/isArray}} + {{^isArray}} + addQueryParam("{{baseName}}",{{paramName}}); + {{/isArray}} + {{/queryParams}} + // Form | {{#formParams}}{{paramName}} {{/formParams}} - // Body | {{#bodyParam}}{{paramName}}{{/bodyParam}} + {{#hasFormParams}} + addHeader("Content-Type", "application/x-www-form-urlencoded"); + {{/hasFormParams}} + {{#hasBodyParam}} + addHeader("Content-Type", "application/json"); + {{/hasBodyParam}} + + {{#formParams}} + {{#isArray}} + for (auto &x : {{paramName}}){ + addFormParam("{{baseName}}", std::string(x)); + } + {{/isArray}} + {{^isArray}} + addFormParam("{{baseName}}",{{paramName}}); + {{/isArray}} + {{/formParams}} + {{#pathParams}} std::string s_{{paramName}}("{"); @@ -56,13 +90,12 @@ using namespace Tiny; url.insert(pos, stringify({{paramName}})); {{/pathParams}} - begin(url); std::string payload = ""; // Send Request // METHOD | {{httpMethod}} + // Body | {{#bodyParam}}{{paramName}}{{/bodyParam}} {{#bodyParam}} - http.addHeader("Content-Type", "application/json"); {{#isContainer}} {{#isArray}} @@ -101,14 +134,12 @@ using namespace Tiny; {{/isContainer}} {{/bodyParam}} - int httpCode = http.sendRequest("{{httpMethod}}", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "{{httpMethod}}", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - {{#returnType}} {{#returnContainer}} diff --git a/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-header.mustache index 416369dcf31..35e3bc4b8f1 100644 --- a/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-tiny/service/api-header.mustache @@ -4,7 +4,7 @@ {{{defaultInclude}}} #include "Response.h" #include "Arduino.h" -#include "AbstractService.h" +#include "Service.h" #include "Helpers.h" #include @@ -20,7 +20,7 @@ namespace Tiny { * Generated with openapi::tiny-cpp-client */ -class {{classname}} : public AbstractService { +class {{classname}} : public Service { public: {{! Constructor }} {{classname}}() = default; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyBaseTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyBaseTest.java new file mode 100644 index 00000000000..7728f8f7259 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyBaseTest.java @@ -0,0 +1,56 @@ +package org.openapitools.codegen.cpptiny; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.ObjectSchema; +import io.swagger.v3.oas.models.media.Schema; +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.TestUtils; +import org.openapitools.codegen.languages.CppTinyClientCodegen; +import org.testng.Assert; +import org.testng.annotations.Test; + +@SuppressWarnings("rawtypes") +public class CppTinyBaseTest { + protected CppTinyClientCodegen codegen = new CppTinyClientCodegen(); + + protected CodegenModel makeCodeGenFrom(Schema model) { + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); + codegen.setOpenAPI(openAPI); + return codegen.fromModel("sample", model); + } + + protected Schema testSchema() { + return new ObjectSchema() + .description("a sample model"); + } + + + @Test(description = "An example of testing the code model before it is rendered") + public void exampleOfATest() { + + // Make a schema object. We pretend this model comes from an OAS (openapi specification) + // https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaObject + // Then add the properties to your model. + + // Arrange + Schema schemaModel = testSchema(); + schemaModel.addProperties("id", new IntegerSchema().format("int64")); + + + // Then we generated a codemodel, with the cpp tiny code generator. + // Act + CodegenModel model_to_be_generated = makeCodeGenFrom(schemaModel); + + // We can then test, and see if the codegen model have the right data. + // Assert + CodegenProperty id_property = model_to_be_generated.vars.get(0); + Assert.assertEquals(id_property.dataType, "long"); + } + + // Make example test of assert that some codegen is generated correctly + // Assert.assertEquals(generated_code, "long id = 0;" + + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyClientModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyClientModelTest.java new file mode 100644 index 00000000000..da97ae45645 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyClientModelTest.java @@ -0,0 +1,17 @@ +package org.openapitools.codegen.cpptiny; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.ObjectSchema; +import io.swagger.v3.oas.models.media.Schema; +import org.openapitools.codegen.*; +import org.openapitools.codegen.languages.CppTinyClientCodegen; +import org.testng.Assert; +import org.testng.annotations.Test; + + + +public class CppTinyClientModelTest extends CppTinyBaseTest { + + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyServiceServiceTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyServiceServiceTest.java new file mode 100644 index 00000000000..f0cab70d635 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/cpptiny/CppTinyServiceServiceTest.java @@ -0,0 +1,25 @@ +package org.openapitools.codegen.cpptiny; + +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.Schema; +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.testng.Assert; +import org.testng.annotations.Test; + +@SuppressWarnings("rawtypes") +public class CppTinyServiceServiceTest extends CppTinyBaseTest { + @Test(description = "sets example value") + public void intTypeIsLong() { + // Arrange + Schema schema = testSchema(); + schema.addProperties("id", new IntegerSchema().format("int64")); + + // Act + CodegenModel model_to_be_generated = makeCodeGenFrom(schema); + + // Assert + CodegenProperty id_property = model_to_be_generated.vars.get(0); + Assert.assertEquals(id_property.dataType, "long"); + } +} diff --git a/samples/client/petstore/cpp-tiny/.openapi-generator/FILES b/samples/client/petstore/cpp-tiny/.openapi-generator/FILES index 1024d945064..638b6a51a62 100644 --- a/samples/client/petstore/cpp-tiny/.openapi-generator/FILES +++ b/samples/client/petstore/cpp-tiny/.openapi-generator/FILES @@ -1,5 +1,4 @@ README.md -README.md lib/Models/ApiResponse.cpp lib/Models/ApiResponse.h lib/Models/Category.cpp @@ -14,11 +13,11 @@ lib/Models/Tag.cpp lib/Models/Tag.h lib/Models/User.cpp lib/Models/User.h -lib/service/AbstractService.cpp -lib/service/AbstractService.h lib/service/PetApi.cpp lib/service/PetApi.h lib/service/Response.h +lib/service/Service.cpp +lib/service/Service.h lib/service/StoreApi.cpp lib/service/StoreApi.h lib/service/UserApi.cpp diff --git a/samples/client/petstore/cpp-tiny/lib/service/AbstractService.cpp b/samples/client/petstore/cpp-tiny/lib/service/AbstractService.cpp deleted file mode 100644 index ac7e992cecc..00000000000 --- a/samples/client/petstore/cpp-tiny/lib/service/AbstractService.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "AbstractService.h" -#include "Arduino.h" - - - -void Tiny::AbstractService::begin(std::string url){ - http.begin(String(url.c_str()), test_root_ca); //HTTPS connection -} diff --git a/samples/client/petstore/cpp-tiny/lib/service/AbstractService.h b/samples/client/petstore/cpp-tiny/lib/service/AbstractService.h deleted file mode 100644 index 07214896d7f..00000000000 --- a/samples/client/petstore/cpp-tiny/lib/service/AbstractService.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ -#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ - -#include "HTTPClient.h" -#include "Response.h" -namespace Tiny { - -/** -* Class -* Generated with openapi::tiny-cpp-client -*/ -class AbstractService { -public: -HTTPClient http; -std::string basepath = "https://petstore3.swagger.io/api/v3"; // TODO: change to your url - -void begin(std::string url); - -// Go and comment out a certificate in root.cert, if you get an error here -// Certificate from file -const char* test_root_ca = -#include "../../root.cert" -; - -}; // end class -}// namespace Tinyclient - -#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */ diff --git a/samples/client/petstore/cpp-tiny/lib/service/PetApi.cpp b/samples/client/petstore/cpp-tiny/lib/service/PetApi.cpp index 8bf9e141794..a62c6073b6e 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/PetApi.cpp +++ b/samples/client/petstore/cpp-tiny/lib/service/PetApi.cpp @@ -15,31 +15,34 @@ using namespace Tiny; ) { std::string url = basepath + "/pet"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | pet + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | POST - http.addHeader("Content-Type", "application/json"); + // Body | pet payload = pet.toJson().dump(); - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - @@ -64,10 +67,16 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/{petId}"; //petId + + + // Headers | apiKey + addHeader("api_key",apiKey); + // Query | - // Headers | apiKey + // Form | - // Body | + + std::string s_petId("{"); s_petId.append("petId"); @@ -78,19 +87,17 @@ using namespace Tiny; url.erase(pos, s_petId.length()); url.insert(pos, stringify(petId)); - begin(url); std::string payload = ""; // Send Request // METHOD | DELETE - int httpCode = http.sendRequest("DELETE", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "DELETE", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -107,25 +114,31 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/findByStatus"; // - // Query | status + + // Headers | + + // Query | status + for (auto &x : status){ + addQueryParam("status", std::string(x)); + } + // Form | - // Body | - begin(url); + + std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - std::list obj = std::list(); @@ -167,25 +180,31 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/findByTags"; // - // Query | tags + + // Headers | + + // Query | tags + for (auto &x : tags){ + addQueryParam("tags", std::string(x)); + } + // Form | - // Body | - begin(url); + + std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - std::list obj = std::list(); @@ -227,10 +246,15 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/{petId}"; //petId - // Query | + + // Headers | + + // Query | + // Form | - // Body | + + std::string s_petId("{"); s_petId.append("petId"); @@ -241,19 +265,17 @@ using namespace Tiny; url.erase(pos, s_petId.length()); url.insert(pos, stringify(petId)); - begin(url); std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - @@ -275,31 +297,34 @@ using namespace Tiny; ) { std::string url = basepath + "/pet"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | pet + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | PUT - http.addHeader("Content-Type", "application/json"); + // Body | pet payload = pet.toJson().dump(); - int httpCode = http.sendRequest("PUT", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "PUT", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - @@ -327,10 +352,18 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/{petId}"; //petId - // Query | + + // Headers | + + // Query | + // Form | name status - // Body | + addHeader("Content-Type", "application/x-www-form-urlencoded"); + + addFormParam("name",name); + addFormParam("status",status); + std::string s_petId("{"); s_petId.append("petId"); @@ -341,19 +374,17 @@ using namespace Tiny; url.erase(pos, s_petId.length()); url.insert(pos, stringify(petId)); - begin(url); std::string payload = ""; // Send Request // METHOD | POST - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -376,10 +407,18 @@ using namespace Tiny; ) { std::string url = basepath + "/pet/{petId}/uploadImage"; //petId - // Query | + + // Headers | + + // Query | + // Form | additionalMetadata file - // Body | + addHeader("Content-Type", "application/x-www-form-urlencoded"); + + addFormParam("additionalMetadata",additionalMetadata); + addFormParam("file",file); + std::string s_petId("{"); s_petId.append("petId"); @@ -390,19 +429,17 @@ using namespace Tiny; url.erase(pos, s_petId.length()); url.insert(pos, stringify(petId)); - begin(url); std::string payload = ""; // Send Request // METHOD | POST - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - diff --git a/samples/client/petstore/cpp-tiny/lib/service/PetApi.h b/samples/client/petstore/cpp-tiny/lib/service/PetApi.h index abcb0af0de9..b3839074033 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/PetApi.h +++ b/samples/client/petstore/cpp-tiny/lib/service/PetApi.h @@ -4,7 +4,7 @@ #include "Response.h" #include "Arduino.h" -#include "AbstractService.h" +#include "Service.h" #include "Helpers.h" #include @@ -18,7 +18,7 @@ namespace Tiny { * Generated with openapi::tiny-cpp-client */ -class PetApi : public AbstractService { +class PetApi : public Service { public: PetApi() = default; diff --git a/samples/client/petstore/cpp-tiny/lib/service/Service.cpp b/samples/client/petstore/cpp-tiny/lib/service/Service.cpp new file mode 100644 index 00000000000..fe6cf83bcc3 --- /dev/null +++ b/samples/client/petstore/cpp-tiny/lib/service/Service.cpp @@ -0,0 +1,64 @@ +#include "Service.h" +#include "Arduino.h" + +void Tiny::Service::begin(std::string url){ + http.begin(String(url.c_str()), test_root_ca); //HTTPS connection + + + // reset params + queryParams.begin(); + formParams.begin(); +} + +void Tiny::Service::addHeader(std::string key, std::string value){ + http.addHeader(String(key.c_str()), String(value.c_str())); +} + +void Tiny::Service::addQueryParam(std::string key, std::string value){ + formParams.push_back(std::make_tuple(key, value)); +} + +void Tiny::Service::addFormParam(std::string key, std::string value){ + formParams.push_back(std::make_tuple(key, value)); +} + +int Tiny::Service::sendRequest(std::string url, const char * type, uint8_t * payload, size_t size){ + this->url = url; + prepareRequest(); + + begin(this->url); + + int httpCode = http.sendRequest(type, payload, size); + return httpCode; +} + +void Tiny::Service::prepareRequest(){ + if (!queryParams.empty()){ + this->url += "?" + encodeKeyValueTuple(this->queryParams); + } + + if (!formParams.empty()){ + this->payload = encodeKeyValueTuple(this->formParams); + } +} + +std::string Tiny::Service::encodeKeyValueTuple(std::list> params){ + std::string encoded = ""; + for (auto const& tuple : params) { + encoded += std::get<0>(tuple) + "=" + std::get<1>(tuple) + "&"; + } + + // Remove last '&' char from url + if (! encoded.empty()) + encoded.pop_back(); + + return encoded; +} + + +String Tiny::Service::getResponseBody(){ + String respBody = http.getString(); + http.end(); + return respBody; +} + diff --git a/samples/client/petstore/cpp-tiny/lib/service/Service.h b/samples/client/petstore/cpp-tiny/lib/service/Service.h new file mode 100644 index 00000000000..c7f0634bd42 --- /dev/null +++ b/samples/client/petstore/cpp-tiny/lib/service/Service.h @@ -0,0 +1,43 @@ +#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ +#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ + +#include "HTTPClient.h" +#include "Response.h" +#include +namespace Tiny { + +/** +* Class +* Generated with openapi::tiny-cpp-client +*/ +class Service { + public: + HTTPClient http; + std::string basepath = "https://petstore3.swagger.io/api/v3"; // TODO: change to your url + std::string url = ""; + std::string payload = ""; + std::list> queryParams; + std::list> formParams; + + int sendRequest(std::string url, const char * type, uint8_t * payload, size_t size); + String getResponseBody(); + + void addQueryParam(std::string key, std::string value); + void addFormParam(std::string key, std::string value); + void addHeader(std::string key, std::string value); + + // Go and comment out a certificate in root.cert, if you get an error here + // Certificate from file + const char* test_root_ca = + #include "../../root.cert" + ; + + private: + void begin(std::string url); + void prepareRequest(); + std::string encodeKeyValueTuple(std::list> params); + +}; // end class +}// namespace Tinyclient + +#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */ diff --git a/samples/client/petstore/cpp-tiny/lib/service/StoreApi.cpp b/samples/client/petstore/cpp-tiny/lib/service/StoreApi.cpp index d52253f03fb..28f0e15ae17 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/StoreApi.cpp +++ b/samples/client/petstore/cpp-tiny/lib/service/StoreApi.cpp @@ -15,10 +15,15 @@ using namespace Tiny; ) { std::string url = basepath + "/store/order/{orderId}"; //orderId - // Query | + + // Headers | + + // Query | + // Form | - // Body | + + std::string s_orderId("{"); s_orderId.append("orderId"); @@ -29,19 +34,17 @@ using namespace Tiny; url.erase(pos, s_orderId.length()); url.insert(pos, stringify(orderId)); - begin(url); std::string payload = ""; // Send Request // METHOD | DELETE - int httpCode = http.sendRequest("DELETE", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "DELETE", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -55,25 +58,28 @@ using namespace Tiny; ) { std::string url = basepath + "/store/inventory"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | - begin(url); + + std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - //TODO: Implement map logic here @@ -97,10 +103,15 @@ using namespace Tiny; ) { std::string url = basepath + "/store/order/{orderId}"; //orderId - // Query | + + // Headers | + + // Query | + // Form | - // Body | + + std::string s_orderId("{"); s_orderId.append("orderId"); @@ -111,19 +122,17 @@ using namespace Tiny; url.erase(pos, s_orderId.length()); url.insert(pos, stringify(orderId)); - begin(url); std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - @@ -145,31 +154,34 @@ using namespace Tiny; ) { std::string url = basepath + "/store/order"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | order + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | POST - http.addHeader("Content-Type", "application/json"); + // Body | order payload = order.toJson().dump(); - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - diff --git a/samples/client/petstore/cpp-tiny/lib/service/StoreApi.h b/samples/client/petstore/cpp-tiny/lib/service/StoreApi.h index 1bf0db02373..b7a6781bd42 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/StoreApi.h +++ b/samples/client/petstore/cpp-tiny/lib/service/StoreApi.h @@ -4,7 +4,7 @@ #include "Response.h" #include "Arduino.h" -#include "AbstractService.h" +#include "Service.h" #include "Helpers.h" #include @@ -18,7 +18,7 @@ namespace Tiny { * Generated with openapi::tiny-cpp-client */ -class StoreApi : public AbstractService { +class StoreApi : public Service { public: StoreApi() = default; diff --git a/samples/client/petstore/cpp-tiny/lib/service/UserApi.cpp b/samples/client/petstore/cpp-tiny/lib/service/UserApi.cpp index 3323d805a0c..34564a9e7e4 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/UserApi.cpp +++ b/samples/client/petstore/cpp-tiny/lib/service/UserApi.cpp @@ -15,31 +15,34 @@ using namespace Tiny; ) { std::string url = basepath + "/user"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | user + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | POST - http.addHeader("Content-Type", "application/json"); + // Body | user payload = user.toJson().dump(); - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -56,18 +59,23 @@ using namespace Tiny; ) { std::string url = basepath + "/user/createWithArray"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | user + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | POST - http.addHeader("Content-Type", "application/json"); + // Body | user bourne::json tmp_arr = bourne::json::array(); @@ -80,14 +88,12 @@ using namespace Tiny; payload = tmp_arr.dump(); - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -104,18 +110,23 @@ using namespace Tiny; ) { std::string url = basepath + "/user/createWithList"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | user + addHeader("Content-Type", "application/json"); + + - begin(url); std::string payload = ""; // Send Request // METHOD | POST - http.addHeader("Content-Type", "application/json"); + // Body | user bourne::json tmp_arr = bourne::json::array(); @@ -128,14 +139,12 @@ using namespace Tiny; payload = tmp_arr.dump(); - int httpCode = http.sendRequest("POST", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "POST", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -152,10 +161,15 @@ using namespace Tiny; ) { std::string url = basepath + "/user/{username}"; //username - // Query | + + // Headers | + + // Query | + // Form | - // Body | + + std::string s_username("{"); s_username.append("username"); @@ -166,19 +180,17 @@ using namespace Tiny; url.erase(pos, s_username.length()); url.insert(pos, stringify(username)); - begin(url); std::string payload = ""; // Send Request // METHOD | DELETE - int httpCode = http.sendRequest("DELETE", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "DELETE", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -195,10 +207,15 @@ using namespace Tiny; ) { std::string url = basepath + "/user/{username}"; //username - // Query | + + // Headers | + + // Query | + // Form | - // Body | + + std::string s_username("{"); s_username.append("username"); @@ -209,19 +226,17 @@ using namespace Tiny; url.erase(pos, s_username.length()); url.insert(pos, stringify(username)); - begin(url); std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - @@ -246,25 +261,30 @@ using namespace Tiny; ) { std::string url = basepath + "/user/login"; // - // Query | username password + + // Headers | + + // Query | username password + addQueryParam("username",username); + addQueryParam("password",password); + // Form | - // Body | - begin(url); + + std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - bourne::json jsonPayload(output_string); @@ -285,25 +305,28 @@ using namespace Tiny; ) { std::string url = basepath + "/user/logout"; // - // Query | + + // Headers | + + // Query | + // Form | - // Body | - begin(url); + + std::string payload = ""; // Send Request // METHOD | GET - int httpCode = http.sendRequest("GET", reinterpret_cast(&payload[0]), payload.length()); + // Body | + int httpCode = sendRequest(url, "GET", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; @@ -323,10 +346,16 @@ using namespace Tiny; ) { std::string url = basepath + "/user/{username}"; //username - // Query | + + // Headers | + + // Query | + // Form | - // Body | user + addHeader("Content-Type", "application/json"); + + std::string s_username("{"); s_username.append("username"); @@ -337,25 +366,22 @@ using namespace Tiny; url.erase(pos, s_username.length()); url.insert(pos, stringify(username)); - begin(url); std::string payload = ""; // Send Request // METHOD | PUT - http.addHeader("Content-Type", "application/json"); + // Body | user payload = user.toJson().dump(); - int httpCode = http.sendRequest("PUT", reinterpret_cast(&payload[0]), payload.length()); + int httpCode = sendRequest(url, "PUT", reinterpret_cast(&payload[0]), payload.length()); // Handle Request - String output = http.getString(); + String output = getResponseBody(); std::string output_string = output.c_str(); - http.end(); - Response response(output, httpCode); return response; diff --git a/samples/client/petstore/cpp-tiny/lib/service/UserApi.h b/samples/client/petstore/cpp-tiny/lib/service/UserApi.h index afdaefa0f8c..b7a42ca811f 100644 --- a/samples/client/petstore/cpp-tiny/lib/service/UserApi.h +++ b/samples/client/petstore/cpp-tiny/lib/service/UserApi.h @@ -4,7 +4,7 @@ #include "Response.h" #include "Arduino.h" -#include "AbstractService.h" +#include "Service.h" #include "Helpers.h" #include @@ -18,7 +18,7 @@ namespace Tiny { * Generated with openapi::tiny-cpp-client */ -class UserApi : public AbstractService { +class UserApi : public Service { public: UserApi() = default; diff --git a/samples/client/petstore/cpp-tiny/platformio.ini b/samples/client/petstore/cpp-tiny/platformio.ini index 00781178d10..35562a0f6f4 100644 --- a/samples/client/petstore/cpp-tiny/platformio.ini +++ b/samples/client/petstore/cpp-tiny/platformio.ini @@ -2,6 +2,6 @@ platform = espressif32 board = nodemcu-32s framework = arduino -lib_deps = https://github.com/steinwurf/bourne.git +lib_deps = https://github.com/steinwurf/bourne.git#9.0.0 extra_scripts = pre_compiling_bourne.py diff --git a/samples/client/petstore/cpp-tiny/root.cert b/samples/client/petstore/cpp-tiny/root.cert index 59d760f1e16..bb78f18edc6 100644 --- a/samples/client/petstore/cpp-tiny/root.cert +++ b/samples/client/petstore/cpp-tiny/root.cert @@ -2,7 +2,7 @@ // Below is two examples of root certificates. // Let's encrypt root certificate -/** + "-----BEGIN CERTIFICATE-----\n" \ "MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ @@ -25,10 +25,10 @@ "YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \ "CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \ "-----END CERTIFICATE-----\n" -*/ + // Amazon_Root_CA_1.pem -/** + "-----BEGIN CERTIFICATE-----\n" \ "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" \ "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \ @@ -49,4 +49,5 @@ "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" \ "rqXRfboQnoZsG4q5WTP468SQvvG5\n" \ "-----END CERTIFICATE-----\n" \ -*/ + +