#pragma once #include "ehs/EHS.h" #include "ehs/Vector.h" #include "ehs/Str.h" #include "ehs/json/Json.h" #include "Socket.h" #include "QueryVar.h" #include "HeaderVar.h" namespace ehs { enum class Verb { POST, GET, PUT, DEL }; class EHS_LIB_IO Request { private: Verb verb; Str_8 rsrc; Vector queries; Vector header; ContentType cType; Str_8 body; public: /// Default member initialization. Request(); /// Initializes this request with a given verb and URI resource. /// @param [in] verb The type of request to make. /// @param [in] rsrc The URI endpoint to make the request at. Request(const Verb &verb, Str_8 rsrc); /// Initializes this request with the raw request data. /// @param [in] data The C-style string of the request. /// @param [in] size The size of the given C-style string. Request(const char *data, const UInt_64 &size); /// Initializes this request with the raw request data. /// @param [in] data The string of the request. Request(const Str_8 &data); /// Moves members from another object of the same type. /// @param [in] other The object to move from. Request(Request &&other) noexcept; /// Copies members from another object of the same type. /// @param [in] other The object to copy from. Request(const Request &other); /// Moves members from another object of the same type. /// @param [in] other The object to move from. /// @returns The request that has been assigned to. Request& operator=(Request &&other) noexcept; /// Copies members from another object of the same type. /// @param [in] other The object to copy from. /// @returns The request that has been assigned to. Request& operator=(const Request &other); /// Retrieves the verb for the request. /// @returns The result. Verb GetVerb() const; /// Sets the content type for the body. /// @param [in] cType The content type to use. void SetContentType(const ContentType &cType); /// Retrieves the content type for the body. /// @returns The result. ContentType GetContentType() const; /// Sets the URI resource. /// @param [in] rsrc The resource. void SetResource(Str_8 rsrc); /// Retrieves the URI resource. /// @returns The result. Str_8 GetResource() const; /// Checks whether a header variable exists using the id. /// @param [in] id The variable id to look for. /// @returns True if the variable exists. False otherwise. bool HasQueryVar(const UInt_64 &id) const; /// Checks whether a header variable exists using the name. /// @param [in] name The variable name to look for. /// @returns True if the variable exists. False otherwise. bool HasQueryVar(const Str_8 &name) const; /// Adds a header variable. /// @param [in] var The variable to add to the header. /// @returns False if the given variable name already exists. True otherwise. bool AddQueryVar(QueryVar var); /// Removes a header variable using the id. /// @param [in] id The variable id to look for. /// @returns True if the variable was found. False otherwise. bool RemoveQueryVar(const UInt_64 &id); /// Removes a header variable using the name. /// @param [in] name The variable name to look for. /// @returns True if the variable was found. False otherwise. bool RemoveQueryVar(const Str_8 &name); /// Retrieves a header variable using the id. /// @param [in] id The variable id to look for. /// @returns The variable object if found. Nullptr otherwise. QueryVar *GetQueryVar(const UInt_64 &id) const; /// Retrieves a header variable using the name. /// @param [in] name The variable name to look for. /// @returns The variable object if found. Nullptr otherwise. QueryVar *GetQueryVar(const Str_8& name) const; /// Retrieves a header variable value using the id. /// @param [in] id The variable id to look for. /// @returns The variable's value if found. Empty otherwise. Str_8 GetQueryValue(const UInt_64 &id) const; /// Retrieves a header variable value using the name. /// @param [in] name The variable name to look for. /// @returns The variable's value if found. Empty otherwise. Str_8 GetQueryValue(const Str_8& name) const; /// Retrieves all the query variables from the URI in a vector object. /// @returns The result. Vector GetQueries() const; /// A helper method to automatically add the required header variables for basic authentication. /// @param [in] id The username or id. /// @param [in] secret The secret given by an API. void BasicAuth(const Str_8 &id, const Str_8 &secret); /// A helper method to automatically add the required header variables for bearer authentication. /// @param [in] token The token given by an API. void BearerAuth(const Str_8 &token); /// A helper method to automatically add the required header variables for bearer authentication. /// @param [in] token The token given by an API. /// @param [in] clientId The client id given by an API. void BearerAuth(const Str_8 &token, const Str_8 &clientId); /// A helper method to automatically add the required header variables for bot authentication. /// @param [in] token The token given by an API. void BotAuth(const Str_8 &token); /// Checks whether a header variable exists using the id. /// @param [in] id The variable id to look for. /// @returns True if the variable exists. False otherwise. bool HasHeaderVar(const UInt_64 &id) const; /// Checks whether a header variable exists using the name. /// @param [in] name The variable name to look for. /// @returns True if the variable exists. False otherwise. bool HasHeaderVar(const Str_8 &name) const; /// Adds a header variable. /// @param [in] var The variable to add to the header. /// @returns False if the given variable name already exists. True otherwise. bool AddHeaderVar(HeaderVar var); /// Removes a header variable using the id. /// @param [in] id The variable id to look for. /// @returns True if the variable was found. False otherwise. bool RemoveHeaderVar(const UInt_64 &id); /// Removes a header variable using the name. /// @param [in] name The variable name to look for. /// @returns True if the variable was found. False otherwise. bool RemoveHeaderVar(const Str_8 &name); /// Retrieves a header variable using the id. /// @param [in] id The variable id to look for. /// @returns The variable object if found. Nullptr otherwise. HeaderVar *GetHeaderVar(const UInt_64 &id) const; /// Retrieves a header variable using the name. /// @param [in] name The variable name to look for. /// @returns The variable object if found. Nullptr otherwise. HeaderVar *GetHeaderVar(const Str_8& name) const; /// Retrieves a header variable value using the id. /// @param [in] id The variable id to look for. /// @returns The variable's value if found. Empty otherwise. Str_8 GetHeaderValue(const UInt_64 &id) const; /// Retrieves a header variable value using the name. /// @param [in] name The variable name to look for. /// @returns The variable's value if found. Empty otherwise. Str_8 GetHeaderValue(const Str_8& name) const; /// Retrieves all the header variables in a vector object. /// @returns The result. Vector GetHeader() const; /// Adds a body variable. /// @param [in] var The variable identifier. /// @param [in] value The value of the variable. void AddToBody(const Str_8 &var, const Str_8 &value); /// Adds a value to the body. /// @param [in] data The value to add. void AddToBody(const Str_8 &data); /// Sets the entire body. /// @param [in] body The body to use. void SetBody(const Str_8 &body); /// Retrieves a body variable. /// @param [in] var The variable identifier to look for. /// @returns The value of the body variable. Empty if it was not found. Str_8 GetVar(const Str_8 &var) const; /// Retrieves the entire body. /// @returns The result. Str_8 GetBody() const; /// Retrieves the entire body as a Json. /// @returns The result. Json GetJson() const; /// Forms the raw result of the request to be sent. /// @returns The result. Str_8 FormResult() const; bool IsValid() const; private: static Str_8 VerbToStr(const Verb &verb); static Str_8 ContentTypeToStr(const ContentType &cType); static ContentType StrToContentType(const Str_8 &value); void ReadData(const Str_8 &data); }; }