cpp-tiny 3.0 (#10739)

* int get generated as long

* test revised

* moved network logic to abstract service

* New service hooks in services

* bourne version

* remove comment from cert

* sendRequest remove direct link to http request

* Network logic into AbstractService

* Rename AbstractService to Service

* add query params

* Remove unused comment

* Change files in openapi

* amazon root CA 1 default ssl certificate

* rename service

* duplicate line

* Set todo

* lint whitespace fix

* uncomment array querey paramns

* Form params in service body

* this is nice

* added form params

* remove random mustache variable

* change form map to list, as it would overwrite if there was duplicates

* maven build and doc gen
This commit is contained in:
Anders Aaen Springborg 2021-11-04 03:24:13 +01:00 committed by GitHub
parent a7242cc8d0
commit c741b10fb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 623 additions and 274 deletions

View File

@ -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<String>(
@ -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=<controllername>
if (additionalProperties.containsKey(MICROCONTROLLER)) {

View File

@ -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

View File

@ -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" \
*/

View File

@ -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}}

View File

@ -1,39 +0,0 @@
#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
{{#isESP8266}}
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
{{/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_ */

View File

@ -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<std::tuple<std::string, std::string>> 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;
}

View File

@ -0,0 +1,54 @@
#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
{{#isESP8266}}
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
{{/isESP8266}}
{{#isESP32}}
#include "HTTPClient.h"
{{/isESP32}}
#include "Response.h"
#include <list>
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<std::tuple<std::string, std::string>> queryParams;
std::list<std::tuple<std::string, std::string>> 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<std::tuple<std::string, std::string>> params);
}; // end class
}// namespace Tinyclient
#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */

View File

@ -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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "{{httpMethod}}", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
{{#returnType}}
{{#returnContainer}}

View File

@ -4,7 +4,7 @@
{{{defaultInclude}}}
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Service.h"
#include "Helpers.h"
#include <list>
@ -20,7 +20,7 @@ namespace Tiny {
* Generated with openapi::tiny-cpp-client
*/
class {{classname}} : public AbstractService {
class {{classname}} : public Service {
public:
{{! Constructor }}
{{classname}}() = default;

View File

@ -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;"
}

View File

@ -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 {
}

View File

@ -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");
}
}

View File

@ -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

View File

@ -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
}

View File

@ -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_ */

View File

@ -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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "DELETE", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
std::list<Pet> obj = std::list<Pet>();
@ -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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
std::list<Pet> obj = std::list<Pet>();
@ -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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "PUT", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();

View File

@ -4,7 +4,7 @@
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Service.h"
#include "Helpers.h"
#include <list>
@ -18,7 +18,7 @@ namespace Tiny {
* Generated with openapi::tiny-cpp-client
*/
class PetApi : public AbstractService {
class PetApi : public Service {
public:
PetApi() = default;

View File

@ -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<std::tuple<std::string, std::string>> 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;
}

View File

@ -0,0 +1,43 @@
#ifndef TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
#define TINY_CPP_CLIENT_ABSTRACTSERVICE_H_
#include "HTTPClient.h"
#include "Response.h"
#include <list>
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<std::tuple<std::string, std::string>> queryParams;
std::list<std::tuple<std::string, std::string>> 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<std::tuple<std::string, std::string>> params);
}; // end class
}// namespace Tinyclient
#endif /* TINY_CPP_CLIENT_ABSTRACTSERVICE_H_ */

View File

@ -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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "DELETE", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();

View File

@ -4,7 +4,7 @@
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Service.h"
#include "Helpers.h"
#include <list>
@ -18,7 +18,7 @@ namespace Tiny {
* Generated with openapi::tiny-cpp-client
*/
class StoreApi : public AbstractService {
class StoreApi : public Service {
public:
StoreApi() = default;

View File

@ -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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "DELETE", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&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<uint8_t*>(&payload[0]), payload.length());
// Body |
int httpCode = sendRequest(url, "GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> 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<uint8_t*>(&payload[0]), payload.length());
int httpCode = sendRequest(url, "PUT", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
String output = getResponseBody();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;

View File

@ -4,7 +4,7 @@
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Service.h"
#include "Helpers.h"
#include <list>
@ -18,7 +18,7 @@ namespace Tiny {
* Generated with openapi::tiny-cpp-client
*/
class UserApi : public AbstractService {
class UserApi : public Service {
public:
UserApi() = default;

View File

@ -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

View File

@ -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" \
*/