Changed the Azure::Core::Http::HttpMethod regular enum into an extensible enum class. (#2048)

* Changed the `Azure::Core::Http::HttpMethod` regular enum into an extensible enum class.

* Update CL to mention removal of HttpMethodToString.

* Delete the default http method ctor.
This commit is contained in:
Ahson Khan 2021-04-05 20:53:05 -07:00 committed by GitHub
parent a930b7a4f1
commit 38d3535a1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 56 deletions

View File

@ -21,6 +21,7 @@
- Removed `Azure::Core::PackageVersion`.
- Removed from `Azure::Core::Http::Policies` namespace: `HttpPolicyOrder`, `TransportPolicy`, `RetryPolicy`, `RequestIdPolicy`, `TelemetryPolicy`, `BearerTokenAuthenticationPolicy`, `LogPolicy`.
- Renamed `Azure::Core::Http::RawResponse::GetBodyStream()` to `ExtractBodyStream()`.
- Changed the `Azure::Core::Http::HttpMethod` regular enum into an extensible enum class and removed the `HttpMethodToString` helper method.
- Introduced `Azure::Core::Context::Key` class which takes place of `std::string` used for `Azure::Core::Context` keys previously.
## 1.0.0-beta.7 (2021-03-11)

View File

@ -84,43 +84,24 @@ namespace Azure { namespace Core { namespace Http {
/**
* HTTP request method.
*/
enum class HttpMethod
{
Get, ///< GET
Head, ///< HEAD
Post, ///< POST
Put, ///< PUT
Delete, ///< DELETE
Patch, ///< PATCH
};
class HttpMethod {
public:
HttpMethod() = delete;
explicit HttpMethod(std::string value) : m_value(std::move(value)) {}
bool operator==(const HttpMethod& other) const { return m_value == other.m_value; }
bool operator!=(const HttpMethod& other) const { return !(*this == other); }
const std::string& ToString() const { return m_value; }
/**
* @brief Get a string representation for a value of #HttpMethod.
*
* @param method A value of #Azure::Core::Http::HttpMethod value.
*
* @return String name that corresponds to a value of #Azure::Core::Http::HttpMethod type.
*/
inline std::string HttpMethodToString(const HttpMethod& method)
{
switch (method)
{
case HttpMethod::Get:
return "GET";
case HttpMethod::Head:
return "HEAD";
case HttpMethod::Post:
return "POST";
case HttpMethod::Put:
return "PUT";
case HttpMethod::Delete:
return "DELETE";
case HttpMethod::Patch:
return "PATCH";
default:
return "";
}
}
AZ_CORE_DLLEXPORT const static HttpMethod Get;
AZ_CORE_DLLEXPORT const static HttpMethod Head;
AZ_CORE_DLLEXPORT const static HttpMethod Post;
AZ_CORE_DLLEXPORT const static HttpMethod Put;
AZ_CORE_DLLEXPORT const static HttpMethod Delete;
AZ_CORE_DLLEXPORT const static HttpMethod Patch;
private:
std::string m_value;
}; // extensible enum HttpMethod
namespace Policies { namespace _internal {
class RetryPolicy;

View File

@ -154,7 +154,7 @@ void static inline SetHeader(Azure::Core::Http::RawResponse& response, std::stri
// https://tools.ietf.org/html/rfc7230#section-3.1.1
static inline std::string GetHTTPMessagePreBody(Azure::Core::Http::Request const& request)
{
std::string httpRequest(HttpMethodToString(request.GetMethod()));
std::string httpRequest(request.GetMethod().ToString());
// HTTP version hardcoded to 1.1
auto const url = request.GetUrl().GetRelativeUrl();
httpRequest += " /" + url + " HTTP/1.1\r\n";

View File

@ -18,6 +18,13 @@ char const Azure::Core::Http::_internal::HttpShared::Accept[] = "accept";
char const Azure::Core::Http::_internal::HttpShared::MsRequestId[] = "x-ms-request-id";
char const Azure::Core::Http::_internal::HttpShared::MsClientRequestId[] = "x-ms-client-request-id";
const HttpMethod HttpMethod::Get("GET");
const HttpMethod HttpMethod::Head("HEAD");
const HttpMethod HttpMethod::Post("POST");
const HttpMethod HttpMethod::Put("PUT");
const HttpMethod HttpMethod::Delete("DELETE");
const HttpMethod HttpMethod::Patch("PATCH");
void Azure::Core::Http::_detail::RawResponseHelpers::InsertHeaderWithValidation(
Azure::Core::CaseInsensitiveMap& headers,
std::string const& headerName,

View File

@ -42,7 +42,7 @@ inline std::string GetRequestLogMessage(LogOptions const& options, Request const
auto const& requestUrl = request.GetUrl();
std::ostringstream log;
log << "HTTP Request : " << HttpMethodToString(request.GetMethod()) << " "
log << "HTTP Request : " << request.GetMethod().ToString() << " "
<< requestUrl.GetUrlWithoutQuery();
{
auto encodedRequestQueryParams = requestUrl.GetQueryParameters();

View File

@ -22,23 +22,13 @@ inline std::wstring HttpMethodToWideString(HttpMethod method)
// This string should be all uppercase.
// Many servers treat HTTP verbs as case-sensitive, and the Internet Engineering Task Force (IETF)
// Requests for Comments (RFCs) spell these verbs using uppercase characters only.
switch (method)
{
case HttpMethod::Get:
return L"GET";
case HttpMethod::Head:
return L"HEAD";
case HttpMethod::Post:
return L"POST";
case HttpMethod::Put:
return L"PUT";
case HttpMethod::Delete:
return L"DELETE";
case HttpMethod::Patch:
return L"PATCH";
default:
throw Azure::Core::Http::TransportException("Invalid or unsupported HTTP method.");
}
std::string httpMethodString = method.ToString();
// Assuming ASCII here is OK since the input is expected to be an HTTP method string.
// Converting this way is only safe when the text is ASCII.
std::wstring wideStr(httpMethodString.begin(), httpMethodString.end());
return wideStr;
}
// Convert a UTF-8 string to a wide Unicode string.

View File

@ -16,7 +16,7 @@ namespace Azure { namespace Storage { namespace _internal {
std::string SharedKeyPolicy::GetSignature(const Core::Http::Request& request) const
{
std::string string_to_sign;
string_to_sign += Azure::Core::Http::HttpMethodToString(request.GetMethod()) + "\n";
string_to_sign += request.GetMethod().ToString() + "\n";
const auto& headers = request.GetHeaders();
for (std::string headerName :