[cpp][tiny] rename generator, update samples (#9560)

* rename generator, update samples

* add doc

* update readme
This commit is contained in:
William Cheng
2021-05-25 23:55:16 +08:00
committed by GitHub
parent 6c40192706
commit ae430a8c14
61 changed files with 5070 additions and 38 deletions

View File

@@ -0,0 +1,8 @@
#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

@@ -0,0 +1,28 @@
#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

@@ -0,0 +1,418 @@
#include "PetApi.h"
using namespace Tiny;
Response<
Pet
>
PetApi::
addPet(
Pet pet
)
{
std::string url = basepath + "/pet"; //
// Query |
// Headers |
// Form |
// Body | pet
begin(url);
std::string payload = "";
// Send Request
// METHOD | POST
http.addHeader("Content-Type", "application/json");
payload = pet.toJson().dump();
int httpCode = http.sendRequest("POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Pet obj(output_string);
Response<Pet> response(obj, httpCode);
return response;
}
Response<
String
>
PetApi::
deletePet(
long petId
,
std::string apiKey
)
{
std::string url = basepath + "/pet/{petId}"; //petId
// Query |
// Headers | apiKey
// Form |
// Body |
std::string s_petId("{");
s_petId.append("petId");
s_petId.append("}");
int pos = url.find(s_petId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
std::list<Pet>
>
PetApi::
findPetsByStatus(
std::list<std::string> status
)
{
std::string url = basepath + "/pet/findByStatus"; //
// Query | status
// Headers |
// Form |
// Body |
begin(url);
std::string payload = "";
// Send Request
// METHOD | GET
int httpCode = http.sendRequest("GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
std::list<Pet> obj = std::list<Pet>();
bourne::json jsonPayload(output_string);
for(auto& var : jsonPayload.array_range())
{
Pet tmp(var.dump());
obj.push_back(tmp);
}
Response<std::list<Pet>> response(obj, httpCode);
return response;
}
Response<
std::list<Pet>
>
PetApi::
findPetsByTags(
std::list<std::string> tags
)
{
std::string url = basepath + "/pet/findByTags"; //
// Query | tags
// Headers |
// Form |
// Body |
begin(url);
std::string payload = "";
// Send Request
// METHOD | GET
int httpCode = http.sendRequest("GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
std::list<Pet> obj = std::list<Pet>();
bourne::json jsonPayload(output_string);
for(auto& var : jsonPayload.array_range())
{
Pet tmp(var.dump());
obj.push_back(tmp);
}
Response<std::list<Pet>> response(obj, httpCode);
return response;
}
Response<
Pet
>
PetApi::
getPetById(
long petId
)
{
std::string url = basepath + "/pet/{petId}"; //petId
// Query |
// Headers |
// Form |
// Body |
std::string s_petId("{");
s_petId.append("petId");
s_petId.append("}");
int pos = url.find(s_petId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Pet obj(output_string);
Response<Pet> response(obj, httpCode);
return response;
}
Response<
Pet
>
PetApi::
updatePet(
Pet pet
)
{
std::string url = basepath + "/pet"; //
// Query |
// Headers |
// Form |
// Body | pet
begin(url);
std::string payload = "";
// Send Request
// METHOD | PUT
http.addHeader("Content-Type", "application/json");
payload = pet.toJson().dump();
int httpCode = http.sendRequest("PUT", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Pet obj(output_string);
Response<Pet> response(obj, httpCode);
return response;
}
Response<
String
>
PetApi::
updatePetWithForm(
long petId
,
std::string name
,
std::string status
)
{
std::string url = basepath + "/pet/{petId}"; //petId
// Query |
// Headers |
// Form | name status
// Body |
std::string s_petId("{");
s_petId.append("petId");
s_petId.append("}");
int pos = url.find(s_petId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
ApiResponse
>
PetApi::
uploadFile(
long petId
,
std::string additionalMetadata
,
std::string file
)
{
std::string url = basepath + "/pet/{petId}/uploadImage"; //petId
// Query |
// Headers |
// Form | additionalMetadata file
// Body |
std::string s_petId("{");
s_petId.append("petId");
s_petId.append("}");
int pos = url.find(s_petId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
ApiResponse obj(output_string);
Response<ApiResponse> response(obj, httpCode);
return response;
}

View File

@@ -0,0 +1,163 @@
#ifndef TINY_CPP_CLIENT_PetApi_H_
#define TINY_CPP_CLIENT_PetApi_H_
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Helpers.h"
#include <list>
#include "ApiResponse.h"
#include "Pet.h"
namespace Tiny {
/**
* Class
* Generated with openapi::tiny-cpp-client
*/
class PetApi : public AbstractService {
public:
PetApi() = default;
virtual ~PetApi() = default;
/**
* Add a new pet to the store.
*
*
* \param pet Pet object that needs to be added to the store *Required*
*/
Response<
Pet
>
addPet(
Pet pet
);
/**
* Deletes a pet.
*
*
* \param petId Pet id to delete *Required*
* \param apiKey
*/
Response<
String
>
deletePet(
long petId
,
std::string apiKey
);
/**
* Finds Pets by status.
*
* Multiple status values can be provided with comma separated strings
* \param status Status values that need to be considered for filter *Required*
*/
Response<
std::list<Pet>
>
findPetsByStatus(
std::list<std::string> status
);
/**
* Finds Pets by tags.
*
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* \param tags Tags to filter by *Required*
*/
Response<
std::list<Pet>
>
findPetsByTags(
std::list<std::string> tags
);
/**
* Find pet by ID.
*
* Returns a single pet
* \param petId ID of pet to return *Required*
*/
Response<
Pet
>
getPetById(
long petId
);
/**
* Update an existing pet.
*
*
* \param pet Pet object that needs to be added to the store *Required*
*/
Response<
Pet
>
updatePet(
Pet pet
);
/**
* Updates a pet in the store with form data.
*
*
* \param petId ID of pet that needs to be updated *Required*
* \param name Updated name of the pet
* \param status Updated status of the pet
*/
Response<
String
>
updatePetWithForm(
long petId
,
std::string name
,
std::string status
);
/**
* uploads an image.
*
*
* \param petId ID of pet to update *Required*
* \param additionalMetadata Additional data to pass to server
* \param file file to upload
*/
Response<
ApiResponse
>
uploadFile(
long petId
,
std::string additionalMetadata
,
std::string file
);
};
}
#endif /* TINY_CPP_CLIENT_PetApi_H_ */

View File

@@ -0,0 +1,25 @@
#ifndef TINY_CPP_CLIENT_RESPONSE_H_
#define TINY_CPP_CLIENT_RESPONSE_H_
#include <string>
namespace Tiny {
/**
* Class
* Generated with openapi::tiny-cpp-client
*/
template <typename T = std::string>
class Response {
public:
Response(T _obj, int _code){
obj = _obj;
code = _code;
}
int code;
T obj;
};
} // namespace Tinyclient
#endif /* TINY_CPP_CLIENT_RESPONSE_H_ */

View File

@@ -0,0 +1,185 @@
#include "StoreApi.h"
using namespace Tiny;
Response<
String
>
StoreApi::
deleteOrder(
std::string orderId
)
{
std::string url = basepath + "/store/order/{orderId}"; //orderId
// Query |
// Headers |
// Form |
// Body |
std::string s_orderId("{");
s_orderId.append("orderId");
s_orderId.append("}");
int pos = url.find(s_orderId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
String
>
StoreApi::
getInventory(
)
{
std::string url = basepath + "/store/inventory"; //
// Query |
// Headers |
// Form |
// Body |
begin(url);
std::string payload = "";
// Send Request
// METHOD | GET
int httpCode = http.sendRequest("GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
//TODO: Implement map logic here
//TODO: No support for maps.
Response<String> response(output, httpCode);
return response;
}
Response<
Order
>
StoreApi::
getOrderById(
long orderId
)
{
std::string url = basepath + "/store/order/{orderId}"; //orderId
// Query |
// Headers |
// Form |
// Body |
std::string s_orderId("{");
s_orderId.append("orderId");
s_orderId.append("}");
int pos = url.find(s_orderId);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Order obj(output_string);
Response<Order> response(obj, httpCode);
return response;
}
Response<
Order
>
StoreApi::
placeOrder(
Order order
)
{
std::string url = basepath + "/store/order"; //
// Query |
// Headers |
// Form |
// Body | order
begin(url);
std::string payload = "";
// Send Request
// METHOD | POST
http.addHeader("Content-Type", "application/json");
payload = order.toJson().dump();
int httpCode = http.sendRequest("POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Order obj(output_string);
Response<Order> response(obj, httpCode);
return response;
}

View File

@@ -0,0 +1,83 @@
#ifndef TINY_CPP_CLIENT_StoreApi_H_
#define TINY_CPP_CLIENT_StoreApi_H_
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Helpers.h"
#include <list>
#include <map>
#include "Order.h"
namespace Tiny {
/**
* Class
* Generated with openapi::tiny-cpp-client
*/
class StoreApi : public AbstractService {
public:
StoreApi() = default;
virtual ~StoreApi() = default;
/**
* Delete purchase order by ID.
*
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
* \param orderId ID of the order that needs to be deleted *Required*
*/
Response<
String
>
deleteOrder(
std::string orderId
);
/**
* Returns pet inventories by status.
*
* Returns a map of status codes to quantities
*/
Response<
String
>
getInventory(
);
/**
* Find purchase order by ID.
*
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
* \param orderId ID of pet that needs to be fetched *Required*
*/
Response<
Order
>
getOrderById(
long orderId
);
/**
* Place an order for a pet.
*
*
* \param order order placed for purchasing the pet *Required*
*/
Response<
Order
>
placeOrder(
Order order
);
};
}
#endif /* TINY_CPP_CLIENT_StoreApi_H_ */

View File

@@ -0,0 +1,366 @@
#include "UserApi.h"
using namespace Tiny;
Response<
String
>
UserApi::
createUser(
User user
)
{
std::string url = basepath + "/user"; //
// Query |
// Headers |
// Form |
// Body | user
begin(url);
std::string payload = "";
// Send Request
// METHOD | POST
http.addHeader("Content-Type", "application/json");
payload = user.toJson().dump();
int httpCode = http.sendRequest("POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
String
>
UserApi::
createUsersWithArrayInput(
std::list<User> user
)
{
std::string url = basepath + "/user/createWithArray"; //
// Query |
// Headers |
// Form |
// Body | user
begin(url);
std::string payload = "";
// Send Request
// METHOD | POST
http.addHeader("Content-Type", "application/json");
bourne::json tmp_arr = bourne::json::array();
for(auto& var : user)
{
auto tmp = var.toJson();
tmp_arr.append(tmp);
}
payload = tmp_arr.dump();
int httpCode = http.sendRequest("POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
String
>
UserApi::
createUsersWithListInput(
std::list<User> user
)
{
std::string url = basepath + "/user/createWithList"; //
// Query |
// Headers |
// Form |
// Body | user
begin(url);
std::string payload = "";
// Send Request
// METHOD | POST
http.addHeader("Content-Type", "application/json");
bourne::json tmp_arr = bourne::json::array();
for(auto& var : user)
{
auto tmp = var.toJson();
tmp_arr.append(tmp);
}
payload = tmp_arr.dump();
int httpCode = http.sendRequest("POST", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
String
>
UserApi::
deleteUser(
std::string username
)
{
std::string url = basepath + "/user/{username}"; //username
// Query |
// Headers |
// Form |
// Body |
std::string s_username("{");
s_username.append("username");
s_username.append("}");
int pos = url.find(s_username);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
User
>
UserApi::
getUserByName(
std::string username
)
{
std::string url = basepath + "/user/{username}"; //username
// Query |
// Headers |
// Form |
// Body |
std::string s_username("{");
s_username.append("username");
s_username.append("}");
int pos = url.find(s_username);
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());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
User obj(output_string);
Response<User> response(obj, httpCode);
return response;
}
Response<
std::string
>
UserApi::
loginUser(
std::string username
,
std::string password
)
{
std::string url = basepath + "/user/login"; //
// Query | username password
// Headers |
// Form |
// Body |
begin(url);
std::string payload = "";
// Send Request
// METHOD | GET
int httpCode = http.sendRequest("GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
bourne::json jsonPayload(output_string);
std::string obj;
jsonToValue(&obj, jsonPayload, "std::string");
Response<std::string> response(obj, httpCode);
return response;
}
Response<
String
>
UserApi::
logoutUser(
)
{
std::string url = basepath + "/user/logout"; //
// Query |
// Headers |
// Form |
// Body |
begin(url);
std::string payload = "";
// Send Request
// METHOD | GET
int httpCode = http.sendRequest("GET", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}
Response<
String
>
UserApi::
updateUser(
std::string username
,
User user
)
{
std::string url = basepath + "/user/{username}"; //username
// Query |
// Headers |
// Form |
// Body | user
std::string s_username("{");
s_username.append("username");
s_username.append("}");
int pos = url.find(s_username);
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");
payload = user.toJson().dump();
int httpCode = http.sendRequest("PUT", reinterpret_cast<uint8_t*>(&payload[0]), payload.length());
// Handle Request
String output = http.getString();
std::string output_string = output.c_str();
http.end();
Response<String> response(output, httpCode);
return response;
}

View File

@@ -0,0 +1,147 @@
#ifndef TINY_CPP_CLIENT_UserApi_H_
#define TINY_CPP_CLIENT_UserApi_H_
#include "Response.h"
#include "Arduino.h"
#include "AbstractService.h"
#include "Helpers.h"
#include <list>
#include "User.h"
#include <list>
namespace Tiny {
/**
* Class
* Generated with openapi::tiny-cpp-client
*/
class UserApi : public AbstractService {
public:
UserApi() = default;
virtual ~UserApi() = default;
/**
* Create user.
*
* This can only be done by the logged in user.
* \param user Created user object *Required*
*/
Response<
String
>
createUser(
User user
);
/**
* Creates list of users with given input array.
*
*
* \param user List of user object *Required*
*/
Response<
String
>
createUsersWithArrayInput(
std::list<User> user
);
/**
* Creates list of users with given input array.
*
*
* \param user List of user object *Required*
*/
Response<
String
>
createUsersWithListInput(
std::list<User> user
);
/**
* Delete user.
*
* This can only be done by the logged in user.
* \param username The name that needs to be deleted *Required*
*/
Response<
String
>
deleteUser(
std::string username
);
/**
* Get user by user name.
*
*
* \param username The name that needs to be fetched. Use user1 for testing. *Required*
*/
Response<
User
>
getUserByName(
std::string username
);
/**
* Logs user into the system.
*
*
* \param username The user name for login *Required*
* \param password The password for login in clear text *Required*
*/
Response<
std::string
>
loginUser(
std::string username
,
std::string password
);
/**
* Logs out current logged in user session.
*
*
*/
Response<
String
>
logoutUser(
);
/**
* Updated user.
*
* This can only be done by the logged in user.
* \param username name that need to be deleted *Required*
* \param user Updated user object *Required*
*/
Response<
String
>
updateUser(
std::string username
,
User user
);
};
}
#endif /* TINY_CPP_CLIENT_UserApi_H_ */