/** * OpenAPI Petstore * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * The version of the OpenAPI document: 1.0.0 * * * NOTE: This class is auto generated by OpenAPI-Generator unset. * https://openapi-generator.tech * Do not edit the class manually. */ #include #include #include #include #include #include #include #include #include #include "PetApi.h" namespace org { namespace openapitools { namespace server { namespace api { using namespace org::openapitools::server::model; PetApiException::PetApiException(int status_code, std::string what) : m_status(status_code), m_what(what) { } int PetApiException::getStatus() const { return m_status; } const char* PetApiException::what() const noexcept { return m_what.c_str(); } template std::shared_ptr extractJsonModelBodyParam(const std::string& bodyContent) { std::stringstream sstream(bodyContent); boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json(sstream, pt); auto model = std::make_shared(pt); return model; } template std::vector> extractJsonArrayBodyParam(const std::string& bodyContent) { std::stringstream sstream(bodyContent); boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json(sstream, pt); auto arrayRet = std::vector>(); for (const auto& child: pt) { arrayRet.emplace_back(std::make_shared(child.second)); } return arrayRet; } template std::string convertMapResponse(const std::map& map) { boost::property_tree::ptree pt; for(const auto &kv: map) { pt.push_back(boost::property_tree::ptree::value_type( boost::lexical_cast(kv.first), boost::property_tree::ptree( boost::lexical_cast(kv.second)))); } std::stringstream sstream; write_json(sstream, pt); std::string result = sstream.str(); return result; } PetApiPetResource::PetApiPetResource(const std::string& context /* = "/v2" */) { this->set_path(context + "/pet/"); this->set_method_handler("POST", std::bind(&PetApiPetResource::handler_POST_internal, this, std::placeholders::_1)); this->set_method_handler("PUT", std::bind(&PetApiPetResource::handler_PUT_internal, this, std::placeholders::_1)); } PetApiPetResource::~PetApiPetResource() { } std::pair PetApiPetResource::handlePetApiException(const PetApiException& e) { return std::make_pair(e.getStatus(), e.what()); } std::pair PetApiPetResource::handleStdException(const std::exception& e) { return std::make_pair(500, e.what()); } std::pair PetApiPetResource::handleUnspecifiedException() { return std::make_pair(500, "Unknown exception occurred"); } void PetApiPetResource::setResponseHeader(const std::shared_ptr& session, const std::string& header) { session->set_header(header, ""); } void PetApiPetResource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, const std::string& contentType) { session->close(status, result, { {"Connection", "close"}, {"Content-Type", contentType} }); } void PetApiPetResource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result) { session->close(status, result, { {"Connection", "close"} }); } void PetApiPetResource::handler_POST_internal(const std::shared_ptr session) { const auto request = session->get_request(); std::string bodyContent = extractBodyContent(session); // Get body params or form params here from the body content string auto pet = extractJsonModelBodyParam(bodyContent); int status_code = 500; std::shared_ptr resultObject = std::make_shared(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_POST(pet); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { result = resultObject->toJsonString(); const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } if (status_code == 405) { const constexpr auto contentType = "text/plain"; returnResponse(session, 405, result.empty() ? "Invalid input" : result, contentType); return; } defaultSessionClose(session, status_code, result); } // x-extension void PetApiPetResource::handler_PUT_internal(const std::shared_ptr session) { const auto request = session->get_request(); std::string bodyContent = extractBodyContent(session); // body params or form params here from the body content string auto pet = extractJsonModelBodyParam(bodyContent); int status_code = 500; std::shared_ptr resultObject = std::make_shared(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_PUT(pet); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { result = resultObject->toJsonString(); const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } if (status_code == 400) { const constexpr auto contentType = "text/plain"; returnResponse(session, 400, result.empty() ? "Invalid ID supplied" : result, contentType); return; } if (status_code == 404) { const constexpr auto contentType = "text/plain"; returnResponse(session, 404, result.empty() ? "Pet not found" : result, contentType); return; } if (status_code == 405) { const constexpr auto contentType = "text/plain"; returnResponse(session, 405, result.empty() ? "Validation exception" : result, contentType); return; } defaultSessionClose(session, status_code, result); } std::pair> PetApiPetResource::handler_POST( std::shared_ptr const & pet) { throw PetApiException(501, "Not implemented"); } std::pair> PetApiPetResource::handler_PUT( std::shared_ptr const & pet) { throw PetApiException(501, "Not implemented"); } std::string PetApiPetResource::extractBodyContent(const std::shared_ptr& session) { const auto request = session->get_request(); int content_length = request->get_header("Content-Length", 0); std::string bodyContent; session->fetch(content_length, [&bodyContent](const std::shared_ptr session, const restbed::Bytes &body) { bodyContent = restbed::String::format( "%.*s\n", (int)body.size(), body.data()); }); return bodyContent; } PetApiPetPetIdResource::PetApiPetPetIdResource(const std::string& context /* = "/v2" */) { this->set_path(context + "/pet/{petId: .*}/"); this->set_method_handler("DELETE", std::bind(&PetApiPetPetIdResource::handler_DELETE_internal, this, std::placeholders::_1)); this->set_method_handler("GET", std::bind(&PetApiPetPetIdResource::handler_GET_internal, this, std::placeholders::_1)); this->set_method_handler("POST", std::bind(&PetApiPetPetIdResource::handler_POST_internal, this, std::placeholders::_1)); } PetApiPetPetIdResource::~PetApiPetPetIdResource() { } std::pair PetApiPetPetIdResource::handlePetApiException(const PetApiException& e) { return std::make_pair(e.getStatus(), e.what()); } std::pair PetApiPetPetIdResource::handleStdException(const std::exception& e) { return std::make_pair(500, e.what()); } std::pair PetApiPetPetIdResource::handleUnspecifiedException() { return std::make_pair(500, "Unknown exception occurred"); } void PetApiPetPetIdResource::setResponseHeader(const std::shared_ptr& session, const std::string& header) { session->set_header(header, ""); } void PetApiPetPetIdResource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, const std::string& contentType) { session->close(status, result, { {"Connection", "close"}, {"Content-Type", contentType} }); } void PetApiPetPetIdResource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result) { session->close(status, result, { {"Connection", "close"} }); } void PetApiPetPetIdResource::handler_DELETE_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the path params const int64_t petId = getPathParam_petId(request); // Getting the headers const std::string apiKey = getHeader_api_key(request); int status_code = 500; std::string result = ""; try { status_code = handler_DELETE(petId, apiKey); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 400) { const constexpr auto contentType = "text/plain"; returnResponse(session, 400, result.empty() ? "Invalid pet value" : result, contentType); return; } defaultSessionClose(session, status_code, result); } // x-extension void PetApiPetPetIdResource::handler_GET_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the path params const int64_t petId = getPathParam_petId_x_extension(request); int status_code = 500; std::shared_ptr resultObject = std::make_shared(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_GET(petId); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { result = resultObject->toJsonString(); const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } if (status_code == 400) { const constexpr auto contentType = "text/plain"; returnResponse(session, 400, result.empty() ? "Invalid ID supplied" : result, contentType); return; } if (status_code == 404) { const constexpr auto contentType = "text/plain"; returnResponse(session, 404, result.empty() ? "Pet not found" : result, contentType); return; } defaultSessionClose(session, status_code, result); } // x-extension void PetApiPetPetIdResource::handler_POST_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the path params const int64_t petId = getPathParam_petId_x_extension(request); int status_code = 500; std::string result = ""; try { status_code = handler_POST(petId, name, status); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 405) { const constexpr auto contentType = "text/plain"; returnResponse(session, 405, result.empty() ? "Invalid input" : result, contentType); return; } defaultSessionClose(session, status_code, result); } int PetApiPetPetIdResource::handler_DELETE( int64_t const & petId, std::string const & apiKey) { throw PetApiException(501, "Not implemented"); } std::pair> PetApiPetPetIdResource::handler_GET( int64_t const & petId) { throw PetApiException(501, "Not implemented"); } int PetApiPetPetIdResource::handler_POST( int64_t const & petId, std::string const & name, std::string const & status) { throw PetApiException(501, "Not implemented"); } std::string PetApiPetPetIdResource::extractBodyContent(const std::shared_ptr& session) { const auto request = session->get_request(); int content_length = request->get_header("Content-Length", 0); std::string bodyContent; session->fetch(content_length, [&bodyContent](const std::shared_ptr session, const restbed::Bytes &body) { bodyContent = restbed::String::format( "%.*s\n", (int)body.size(), body.data()); }); return bodyContent; } PetApiPetFindByStatusResource::PetApiPetFindByStatusResource(const std::string& context /* = "/v2" */) { this->set_path(context + "/pet/findByStatus/"); this->set_method_handler("GET", std::bind(&PetApiPetFindByStatusResource::handler_GET_internal, this, std::placeholders::_1)); } PetApiPetFindByStatusResource::~PetApiPetFindByStatusResource() { } std::pair PetApiPetFindByStatusResource::handlePetApiException(const PetApiException& e) { return std::make_pair(e.getStatus(), e.what()); } std::pair PetApiPetFindByStatusResource::handleStdException(const std::exception& e) { return std::make_pair(500, e.what()); } std::pair PetApiPetFindByStatusResource::handleUnspecifiedException() { return std::make_pair(500, "Unknown exception occurred"); } void PetApiPetFindByStatusResource::setResponseHeader(const std::shared_ptr& session, const std::string& header) { session->set_header(header, ""); } void PetApiPetFindByStatusResource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, const std::string& contentType) { session->close(status, result, { {"Connection", "close"}, {"Content-Type", contentType} }); } void PetApiPetFindByStatusResource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result) { session->close(status, result, { {"Connection", "close"} }); } void PetApiPetFindByStatusResource::handler_GET_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the query params int status_code = 500; std::vector> resultObject = std::vector>(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_GET(status); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } if (status_code == 400) { const constexpr auto contentType = "text/plain"; returnResponse(session, 400, result.empty() ? "Invalid status value" : result, contentType); return; } defaultSessionClose(session, status_code, result); } std::pair>> PetApiPetFindByStatusResource::handler_GET( std::vector const & status) { throw PetApiException(501, "Not implemented"); } std::string PetApiPetFindByStatusResource::extractBodyContent(const std::shared_ptr& session) { const auto request = session->get_request(); int content_length = request->get_header("Content-Length", 0); std::string bodyContent; session->fetch(content_length, [&bodyContent](const std::shared_ptr session, const restbed::Bytes &body) { bodyContent = restbed::String::format( "%.*s\n", (int)body.size(), body.data()); }); return bodyContent; } PetApiPetFindByTagsResource::PetApiPetFindByTagsResource(const std::string& context /* = "/v2" */) { this->set_path(context + "/pet/findByTags/"); this->set_method_handler("GET", std::bind(&PetApiPetFindByTagsResource::handler_GET_internal, this, std::placeholders::_1)); } PetApiPetFindByTagsResource::~PetApiPetFindByTagsResource() { } std::pair PetApiPetFindByTagsResource::handlePetApiException(const PetApiException& e) { return std::make_pair(e.getStatus(), e.what()); } std::pair PetApiPetFindByTagsResource::handleStdException(const std::exception& e) { return std::make_pair(500, e.what()); } std::pair PetApiPetFindByTagsResource::handleUnspecifiedException() { return std::make_pair(500, "Unknown exception occurred"); } void PetApiPetFindByTagsResource::setResponseHeader(const std::shared_ptr& session, const std::string& header) { session->set_header(header, ""); } void PetApiPetFindByTagsResource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, const std::string& contentType) { session->close(status, result, { {"Connection", "close"}, {"Content-Type", contentType} }); } void PetApiPetFindByTagsResource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result) { session->close(status, result, { {"Connection", "close"} }); } void PetApiPetFindByTagsResource::handler_GET_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the query params int status_code = 500; std::vector> resultObject = std::vector>(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_GET(tags); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } if (status_code == 400) { const constexpr auto contentType = "text/plain"; returnResponse(session, 400, result.empty() ? "Invalid tag value" : result, contentType); return; } defaultSessionClose(session, status_code, result); } std::pair>> PetApiPetFindByTagsResource::handler_GET( std::vector const & tags) { throw PetApiException(501, "Not implemented"); } std::string PetApiPetFindByTagsResource::extractBodyContent(const std::shared_ptr& session) { const auto request = session->get_request(); int content_length = request->get_header("Content-Length", 0); std::string bodyContent; session->fetch(content_length, [&bodyContent](const std::shared_ptr session, const restbed::Bytes &body) { bodyContent = restbed::String::format( "%.*s\n", (int)body.size(), body.data()); }); return bodyContent; } PetApiPetPetIdUploadImageResource::PetApiPetPetIdUploadImageResource(const std::string& context /* = "/v2" */) { this->set_path(context + "/pet/{petId: .*}/uploadImage/"); this->set_method_handler("POST", std::bind(&PetApiPetPetIdUploadImageResource::handler_POST_internal, this, std::placeholders::_1)); } PetApiPetPetIdUploadImageResource::~PetApiPetPetIdUploadImageResource() { } std::pair PetApiPetPetIdUploadImageResource::handlePetApiException(const PetApiException& e) { return std::make_pair(e.getStatus(), e.what()); } std::pair PetApiPetPetIdUploadImageResource::handleStdException(const std::exception& e) { return std::make_pair(500, e.what()); } std::pair PetApiPetPetIdUploadImageResource::handleUnspecifiedException() { return std::make_pair(500, "Unknown exception occurred"); } void PetApiPetPetIdUploadImageResource::setResponseHeader(const std::shared_ptr& session, const std::string& header) { session->set_header(header, ""); } void PetApiPetPetIdUploadImageResource::returnResponse(const std::shared_ptr& session, const int status, const std::string& result, const std::string& contentType) { session->close(status, result, { {"Connection", "close"}, {"Content-Type", contentType} }); } void PetApiPetPetIdUploadImageResource::defaultSessionClose(const std::shared_ptr& session, const int status, const std::string& result) { session->close(status, result, { {"Connection", "close"} }); } void PetApiPetPetIdUploadImageResource::handler_POST_internal(const std::shared_ptr session) { const auto request = session->get_request(); // Getting the path params const int64_t petId = getPathParam_petId(request); int status_code = 500; std::shared_ptr resultObject = std::make_shared(); std::string result = ""; try { std::tie(status_code, resultObject) = handler_POST(petId, additionalMetadata, file); } catch(const PetApiException& e) { std::tie(status_code, result) = handlePetApiException(e); } catch(const std::exception& e) { std::tie(status_code, result) = handleStdException(e); } catch(...) { std::tie(status_code, result) = handleUnspecifiedException(); } if (status_code == 200) { result = resultObject->toJsonString(); const constexpr auto contentType = "application/json"; returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType); return; } defaultSessionClose(session, status_code, result); } std::pair> PetApiPetPetIdUploadImageResource::handler_POST( int64_t const & petId, std::string const & additionalMetadata, std::string const & file) { throw PetApiException(501, "Not implemented"); } std::string PetApiPetPetIdUploadImageResource::extractBodyContent(const std::shared_ptr& session) { const auto request = session->get_request(); int content_length = request->get_header("Content-Length", 0); std::string bodyContent; session->fetch(content_length, [&bodyContent](const std::shared_ptr session, const restbed::Bytes &body) { bodyContent = restbed::String::format( "%.*s\n", (int)body.size(), body.data()); }); return bodyContent; } PetApi::PetApi(std::shared_ptr const& restbedService) : m_service(restbedService) { } PetApi::~PetApi() {} void PetApi::setPetApiPetResource(std::shared_ptr spPetApiPetResource) { m_spPetApiPetResource = spPetApiPetResource; m_service->publish(m_spPetApiPetResource); } void PetApi::setPetApiPetPetIdResource(std::shared_ptr spPetApiPetPetIdResource) { m_spPetApiPetPetIdResource = spPetApiPetPetIdResource; m_service->publish(m_spPetApiPetPetIdResource); } void PetApi::setPetApiPetFindByStatusResource(std::shared_ptr spPetApiPetFindByStatusResource) { m_spPetApiPetFindByStatusResource = spPetApiPetFindByStatusResource; m_service->publish(m_spPetApiPetFindByStatusResource); } void PetApi::setPetApiPetFindByTagsResource(std::shared_ptr spPetApiPetFindByTagsResource) { m_spPetApiPetFindByTagsResource = spPetApiPetFindByTagsResource; m_service->publish(m_spPetApiPetFindByTagsResource); } void PetApi::setPetApiPetPetIdUploadImageResource(std::shared_ptr spPetApiPetPetIdUploadImageResource) { m_spPetApiPetPetIdUploadImageResource = spPetApiPetPetIdUploadImageResource; m_service->publish(m_spPetApiPetPetIdUploadImageResource); } void PetApi::publishDefaultResources() { if (!m_spPetApiPetResource) { setPetApiPetResource(std::make_shared()); } if (!m_spPetApiPetPetIdResource) { setPetApiPetPetIdResource(std::make_shared()); } if (!m_spPetApiPetFindByStatusResource) { setPetApiPetFindByStatusResource(std::make_shared()); } if (!m_spPetApiPetFindByTagsResource) { setPetApiPetFindByTagsResource(std::make_shared()); } if (!m_spPetApiPetPetIdUploadImageResource) { setPetApiPetPetIdUploadImageResource(std::make_shared()); } } std::shared_ptr PetApi::service() { return m_service; } } } } }