97 lines
2.4 KiB
C++
97 lines
2.4 KiB
C++
/*
|
|
* (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_METHOD_HPP
|
|
#define HTTP_METHOD_HPP
|
|
|
|
#include <string>
|
|
|
|
#include "rti/apputils/http/common/HttpException.hpp"
|
|
|
|
namespace rti {
|
|
namespace apputils { namespace http {
|
|
/**
|
|
* Enum class with every HTTP method
|
|
*/
|
|
enum class Method {
|
|
http_post,
|
|
http_put,
|
|
http_get,
|
|
http_delete,
|
|
http_options,
|
|
http_head,
|
|
http_patch
|
|
};
|
|
|
|
/**
|
|
* @brief Convert a Method enum to an std::string
|
|
*
|
|
* @param http_method Method enum to convert
|
|
* @return std::string with the method name
|
|
*/
|
|
inline std::string method_to_string(Method http_method)
|
|
{
|
|
switch (http_method) {
|
|
case Method::http_post:
|
|
return "POST";
|
|
case Method::http_put:
|
|
return "PUT";
|
|
case Method::http_get:
|
|
return "GET";
|
|
case Method::http_delete:
|
|
return "DELETE";
|
|
case Method::http_options:
|
|
return "OPTIONS";
|
|
case Method::http_head:
|
|
return "HEAD";
|
|
case Method::http_patch:
|
|
return "PATCH";
|
|
default:
|
|
RTI_THROW_HTTP_EXCEPTION(
|
|
StatusCode::bad_request,
|
|
&RTI_LOG_ANY_FAILURE_s,
|
|
"Invalid HTTP method");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Convert a std::string to a Method enum
|
|
*
|
|
* @param http_method String to convert
|
|
* @return Method enum
|
|
*/
|
|
inline Method method_from_string(const std::string& http_method)
|
|
{
|
|
if (http_method == "POST") {
|
|
return Method::http_post;
|
|
} else if (http_method == "PUT") {
|
|
return Method::http_put;
|
|
} else if (http_method == "GET") {
|
|
return Method::http_get;
|
|
} else if (http_method == "DELETE") {
|
|
return Method::http_delete;
|
|
} else if (http_method == "OPTIONS") {
|
|
return Method::http_options;
|
|
} else if (http_method == "HEAD") {
|
|
return Method::http_head;
|
|
} else if (http_method == "PATCH") {
|
|
return Method::http_patch;
|
|
} else {
|
|
RTI_THROW_HTTP_EXCEPTION(
|
|
StatusCode::bad_request,
|
|
&RTI_LOG_ANY_FAILURE_s,
|
|
"Invalid HTTP method");
|
|
}
|
|
}
|
|
|
|
}}} // namespace rti::apputils::http
|
|
|
|
#endif // HTTP_METHOD_HPP
|