/* * (c) Copyright, Real-Time Innovations, 2022. * All rights reserved. * * No duplications, whole or partial, manual or electronic, may be made * without express written permission. Any such copies, or * revisions thereof, must display this notice unaltered. * This code contains trade secrets of Real-Time Innovations, Inc. */ #ifndef HTTP_CLIENT_REQUEST_HPP #define HTTP_CLIENT_REQUEST_HPP #include #include #include #include #include "rti/apputils/http/common/Method.hpp" #include "rti/apputils/http/common/HttpUtils.hpp" namespace rti { namespace apputils { namespace http { namespace client { /** * @brief Request class allow users to define their own requests in order to * send them to the server trough the Connection class. */ class Request { public: /** * @brief Construct a new Request empty object */ Request() { uri_.fill('\0'); } /** * @brief Getter for the HTTP method. * * @return Method object associated to the request. */ Method http_method() const { return http_method_; } /** * @brief Setter for the HTTP method. * * @param http_method HTTP method (POST, PUT, GET, DELETE, HEAD, PATCH, * OPTIONS). * @return Reference to the Request object. */ Request& http_method(Method http_method) { http_method_ = http_method; return *this; } /** * @brief Gets the URL of the HTTP request. * * @return URL of the HTTP request. */ const char *uri() const { return uri_.data(); } /** * @brief Sets the URL of the HTTP request. * * @param uri URL of the HTTP request. * @return Reference to the Request object. */ Request& uri(const char *uri) { std::copy(uri, uri + std::strlen(uri), uri_.data()); return *this; } /** * @brief Sets the URL of the HTTP request. * * @param uri URL of the HTTP request. * @return Reference to the Request object. */ Request& uri(const std::string& uri) { return this->uri(uri.c_str()); } /** * @brief Gets the value of an uri parameter. * * @param name Name of the parameter. * @return const std::string* */ const std::string *uri_parameter(const std::string& name) const { const auto it = uri_parameters_.find(name); if (it == uri_parameters_.end()) { return nullptr; } return &it->second; } /** * @brief Gets all the uri parameters. * * @return const StringMap& */ const StringMap& uri_parameters() const { return uri_parameters_; } /** * @brief Sets all the uri parameters. * * @param uri_parameters StringMap with all the uri parameters. * @return Request& */ Request& uri_parameters(const StringMap& uri_parameters) { uri_parameters_ = uri_parameters; return *this; } /** * @brief Gets the value of a header.\ * * @param name Name of the parameter. * @return const std::string* */ const std::string *header(const std::string& name) const { const auto it = headers_.find(name); if (it == headers_.end()) { return nullptr; } return &it->second; } /** * @brief Gets all the headers. * * @return const StringMap& */ const StringMap& headers() const { return headers_; } /** * @brief Sets all the headers. * * @param headers StringMap with all the headers. * @return Request& */ Request& headers(const StringMap& headers) { headers_ = headers; return *this; } /** * @brief Gets the body of the request. * * @return const std::string& */ const std::string& body() const { return body_; } /** * @brief Sets the body of the request. * * @param body Body of the request. * @return Request& */ Request& body(const std::string& body) { body_ = body; return *this; } private: Method http_method_ = Method::http_get; std::array uri_; StringMap uri_parameters_; StringMap headers_; std::string body_; }; }}}} // namespace rti::apputils::http::client #endif // HTTP_CLIENT_REQUEST_HPP