Commit 66aa41c6298c9ac8491feae095f8f0cf5a5138f2
Committed by
Jan
1 parent
123166ee
Add message to HueException, change HttpHandler exceptions.
Showing
6 changed files
with
268 additions
and
213 deletions
hueplusplus/BaseHttpHandler.cpp
0 → 100644
| 1 | +/** | ||
| 2 | + \file BaseHttpHandler.cpp | ||
| 3 | + Copyright Notice\n | ||
| 4 | + Copyright (C) 2020 Jan Rogall - developer\n | ||
| 5 | + Copyright (C) 2020 Moritz Wirger - developer\n | ||
| 6 | + | ||
| 7 | + This program is free software; you can redistribute it and/or modify | ||
| 8 | + it under the terms of the GNU General Public License as published by | ||
| 9 | + the Free Software Foundation; either version 3 of the License, or | ||
| 10 | + (at your option) any later version. | ||
| 11 | + This program is distributed in the hope that it will be useful, | ||
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | + GNU General Public License for more details. | ||
| 15 | + You should have received a copy of the GNU General Public License | ||
| 16 | + along with this program; if not, write to the Free Software Foundation, | ||
| 17 | + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | +**/ | ||
| 19 | + | ||
| 20 | +#include "BaseHttpHandler.h" | ||
| 21 | + | ||
| 22 | +#include "include/HueExceptionMacro.h" | ||
| 23 | + | ||
| 24 | +std::string BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std::string& adr, int port) const | ||
| 25 | +{ | ||
| 26 | + std::string response = send(msg, adr, port); | ||
| 27 | + size_t start = response.find("\r\n\r\n"); | ||
| 28 | + if (start == std::string::npos) | ||
| 29 | + { | ||
| 30 | + std::cerr << "BaseHttpHandler: Failed to find body in response\n"; | ||
| 31 | + std::cerr << "Request:\n"; | ||
| 32 | + std::cerr << "\"" << msg << "\"\n"; | ||
| 33 | + std::cerr << "Response:\n"; | ||
| 34 | + std::cerr << "\"" << response << "\"\n"; | ||
| 35 | + throw HueException(CURRENT_FILE_INFO, "Failed to find body in response"); | ||
| 36 | + } | ||
| 37 | + response.erase(0, start + 4); | ||
| 38 | + return response; | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, | ||
| 42 | + const std::string& contentType, const std::string& body, const std::string& adr, int port) const | ||
| 43 | +{ | ||
| 44 | + std::string request; | ||
| 45 | + // Protocol reference: | ||
| 46 | + // https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html Request-Line | ||
| 47 | + request.append(method); // Method | ||
| 48 | + request.append(" "); // Separation | ||
| 49 | + request.append(uri); // Request-URI | ||
| 50 | + request.append(" "); // Separation | ||
| 51 | + request.append("HTTP/1.0"); // HTTP-Version | ||
| 52 | + request.append("\r\n"); // Ending | ||
| 53 | + // Entities | ||
| 54 | + request.append("Content-Type:"); // entity-header | ||
| 55 | + request.append(" "); // Separation | ||
| 56 | + request.append(contentType); // media-type | ||
| 57 | + request.append("\r\n"); // Entity ending | ||
| 58 | + request.append("Content-Length:"); // entity-header | ||
| 59 | + request.append(" "); // Separation | ||
| 60 | + request.append(std::to_string(body.size())); // length | ||
| 61 | + request.append("\r\n\r\n"); // Entity ending & Request-Line ending | ||
| 62 | + request.append(body); // message-body | ||
| 63 | + request.append("\r\n\r\n"); // Ending | ||
| 64 | + | ||
| 65 | + return sendGetHTTPBody(request.c_str(), adr, port); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +std::string BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 69 | + const std::string& adr, int port) const | ||
| 70 | +{ | ||
| 71 | + return sendHTTPRequest("GET", uri, contentType, body, adr, port); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +std::string BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 75 | + const std::string& adr, int port) const | ||
| 76 | +{ | ||
| 77 | + return sendHTTPRequest("POST", uri, contentType, body, adr, port); | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +std::string BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 81 | + const std::string& adr, int port) const | ||
| 82 | +{ | ||
| 83 | + return sendHTTPRequest("PUT", uri, contentType, body, adr, port); | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +std::string BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, | ||
| 87 | + const std::string& body, const std::string& adr, int port) const | ||
| 88 | +{ | ||
| 89 | + return sendHTTPRequest("DELETE", uri, contentType, body, adr, port); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +nlohmann::json BaseHttpHandler::GETJson( | ||
| 93 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const | ||
| 94 | +{ | ||
| 95 | + return nlohmann::json::parse(GETString(uri, "application/json", body.dump(), adr, port)); | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +nlohmann::json BaseHttpHandler::POSTJson( | ||
| 99 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const | ||
| 100 | +{ | ||
| 101 | + return nlohmann::json::parse(POSTString(uri, "application/json", body.dump(), adr, port)); | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +nlohmann::json BaseHttpHandler::PUTJson( | ||
| 105 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const | ||
| 106 | +{ | ||
| 107 | + return nlohmann::json::parse(PUTString(uri, "application/json", body.dump(), adr, port)); | ||
| 108 | +} | ||
| 109 | + | ||
| 110 | +nlohmann::json BaseHttpHandler::DELETEJson( | ||
| 111 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const | ||
| 112 | +{ | ||
| 113 | + return nlohmann::json::parse(DELETEString(uri, "application/json", body.dump(), adr, port)); | ||
| 114 | +} |
hueplusplus/CMakeLists.txt
| 1 | file(GLOB hueplusplus_HEADERS include/*.h include/*.hpp) | 1 | file(GLOB hueplusplus_HEADERS include/*.h include/*.hpp) |
| 2 | set(hueplusplus_SOURCES | 2 | set(hueplusplus_SOURCES |
| 3 | - ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorHueStrategy.cpp | 3 | + ${CMAKE_CURRENT_SOURCE_DIR}/BaseHttpHandler.cpp |
| 4 | + ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorHueStrategy.cpp | ||
| 4 | ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorTemperatureStrategy.cpp | 5 | ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorTemperatureStrategy.cpp |
| 5 | ${CMAKE_CURRENT_SOURCE_DIR}/Hue.cpp | 6 | ${CMAKE_CURRENT_SOURCE_DIR}/Hue.cpp |
| 6 | ${CMAKE_CURRENT_SOURCE_DIR}/HueCommandAPI.cpp | 7 | ${CMAKE_CURRENT_SOURCE_DIR}/HueCommandAPI.cpp |
hueplusplus/HueException.cpp
| @@ -19,7 +19,9 @@ | @@ -19,7 +19,9 @@ | ||
| 19 | 19 | ||
| 20 | #include "HueException.h" | 20 | #include "HueException.h" |
| 21 | 21 | ||
| 22 | -HueException::HueException(FileInfo fileInfo) : HueException("Hue exception", std::move(fileInfo)) {} | 22 | +HueException::HueException(FileInfo fileInfo, const std::string& message) |
| 23 | + : HueException("Hue exception", std::move(fileInfo), message) | ||
| 24 | +{} | ||
| 23 | 25 | ||
| 24 | const char* HueException::what() const | 26 | const char* HueException::what() const |
| 25 | { | 27 | { |
| @@ -31,27 +33,23 @@ const FileInfo& HueException::GetFile() const noexcept | @@ -31,27 +33,23 @@ const FileInfo& HueException::GetFile() const noexcept | ||
| 31 | return fileInfo; | 33 | return fileInfo; |
| 32 | } | 34 | } |
| 33 | 35 | ||
| 34 | -HueException::HueException(const char* exceptionName, FileInfo fileInfo) : fileInfo(std::move(fileInfo)) | 36 | +HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) |
| 37 | + : fileInfo(std::move(fileInfo)) | ||
| 35 | { | 38 | { |
| 36 | whatMessage = exceptionName; | 39 | whatMessage = exceptionName; |
| 37 | whatMessage.append(" from"); | 40 | whatMessage.append(" from"); |
| 38 | whatMessage.append(this->fileInfo.ToString()); | 41 | whatMessage.append(this->fileInfo.ToString()); |
| 42 | + whatMessage.append(" "); | ||
| 43 | + whatMessage.append(message); | ||
| 39 | } | 44 | } |
| 40 | 45 | ||
| 41 | HueAPIResponseException::HueAPIResponseException( | 46 | HueAPIResponseException::HueAPIResponseException( |
| 42 | FileInfo fileInfo, int error, std::string address, std::string description) | 47 | FileInfo fileInfo, int error, std::string address, std::string description) |
| 43 | - : HueException("Hue api response exception", std::move(fileInfo)), | 48 | + : HueException("Hue api response exception", std::move(fileInfo), GetMessage(error, address, description)), |
| 44 | error(error), | 49 | error(error), |
| 45 | address(std::move(address)), | 50 | address(std::move(address)), |
| 46 | description(std::move(description)) | 51 | description(std::move(description)) |
| 47 | -{ | ||
| 48 | - whatMessage.append(" "); | ||
| 49 | - whatMessage.append(std::to_string(error)); | ||
| 50 | - whatMessage.append(" "); | ||
| 51 | - whatMessage.append(this->address); | ||
| 52 | - whatMessage.append(" "); | ||
| 53 | - whatMessage.append(this->description); | ||
| 54 | -} | 52 | +{} |
| 55 | 53 | ||
| 56 | int HueAPIResponseException::GetErrorNumber() const noexcept | 54 | int HueAPIResponseException::GetErrorNumber() const noexcept |
| 57 | { | 55 | { |
| @@ -68,6 +66,16 @@ const std::string& HueAPIResponseException::GetDescription() const noexcept | @@ -68,6 +66,16 @@ const std::string& HueAPIResponseException::GetDescription() const noexcept | ||
| 68 | return description; | 66 | return description; |
| 69 | } | 67 | } |
| 70 | 68 | ||
| 69 | +std::string HueAPIResponseException::GetMessage(int error, const std::string& addr, const std::string& description) | ||
| 70 | +{ | ||
| 71 | + std::string result = std::to_string(error); | ||
| 72 | + result.append(" "); | ||
| 73 | + result.append(addr); | ||
| 74 | + result.append(" "); | ||
| 75 | + result.append(description); | ||
| 76 | + return result; | ||
| 77 | +} | ||
| 78 | + | ||
| 71 | std::string FileInfo::ToString() const | 79 | std::string FileInfo::ToString() const |
| 72 | { | 80 | { |
| 73 | if (filename.empty() || line < 0) | 81 | if (filename.empty() || line < 0) |
hueplusplus/include/BaseHttpHandler.h
100755 → 100644
| 1 | /** | 1 | /** |
| 2 | \file BaseHttpHandler.h | 2 | \file BaseHttpHandler.h |
| 3 | Copyright Notice\n | 3 | Copyright Notice\n |
| 4 | - Copyright (C) 2017 Jan Rogall - developer\n | ||
| 5 | - Copyright (C) 2017 Moritz Wirger - developer\n | 4 | + Copyright (C) 2020 Jan Rogall - developer\n |
| 5 | + Copyright (C) 2020 Moritz Wirger - developer\n | ||
| 6 | 6 | ||
| 7 | This file is part of hueplusplus. | 7 | This file is part of hueplusplus. |
| 8 | 8 | ||
| @@ -40,233 +40,162 @@ public: | @@ -40,233 +40,162 @@ public: | ||
| 40 | //! \brief Virtual dtor | 40 | //! \brief Virtual dtor |
| 41 | virtual ~BaseHttpHandler() = default; | 41 | virtual ~BaseHttpHandler() = default; |
| 42 | 42 | ||
| 43 | - //! \brief Virtual function that should send a given message to a specified | ||
| 44 | - //! host and return the response. | 43 | + //! \brief Send a message to a specified host and return the response. |
| 45 | //! | 44 | //! |
| 46 | - //! \param msg String that contains the message that should be sent to the | ||
| 47 | - //! specified address \param adr String that contains an ip or hostname in | ||
| 48 | - //! dotted decimal notation like "192.168.2.1" \param port Optional integer | ||
| 49 | - //! that specifies the port to which the request should be sent to. Default is | ||
| 50 | - //! 80 \return String containing the response of the host | 45 | + //! \param msg The message that should be sent to the specified address |
| 46 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 47 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 48 | + //! \return The response of the host as a string | ||
| 49 | + //! \throws std::system_error when system or socket operations fail | ||
| 51 | virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const = 0; | 50 | virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const = 0; |
| 52 | 51 | ||
| 53 | - //! \brief Virtual function that should given message to a specified host and | ||
| 54 | - //! return the body of the response. | 52 | + //! \brief Send a message to a specified host and return the body of the response. |
| 55 | //! | 53 | //! |
| 56 | - //! Note if no body is found a runtime error is thrown! | ||
| 57 | - //! \param msg String that contains the message that should sent to the | ||
| 58 | - //! specified address \param adr String that contains an ip or hostname in | ||
| 59 | - //! dotted decimal notation like "192.168.2.1" \param port Optional integer | ||
| 60 | - //! that specifies the port to which the request should be sent. Default is 80 | ||
| 61 | - //! \return String containing the body of the response of the host | ||
| 62 | - virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const | ||
| 63 | - { | ||
| 64 | - std::string response = send(msg, adr, port); | ||
| 65 | - size_t start = response.find("\r\n\r\n"); | ||
| 66 | - if (start == std::string::npos) | ||
| 67 | - { | ||
| 68 | - std::cerr << "BaseHttpHandler: Failed to find body in response\n"; | ||
| 69 | - std::cerr << "Request:\n"; | ||
| 70 | - std::cerr << "\"" << msg << "\"\n"; | ||
| 71 | - std::cerr << "Response:\n"; | ||
| 72 | - std::cerr << "\"" << response << "\"\n"; | ||
| 73 | - throw(std::runtime_error("BaseHttpHandler: Failed to find body in response")); | ||
| 74 | - } | ||
| 75 | - response.erase(0, start + 4); | ||
| 76 | - return response; | ||
| 77 | - }; | ||
| 78 | - | ||
| 79 | - //! \brief Virtual function that should send a multicast request with a | ||
| 80 | - //! specified message. | 54 | + //! \param msg The message that should sent to the specified address |
| 55 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 56 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 57 | + //! \return The body of the response of the host as a string | ||
| 58 | + //! \throws std::system_error when system or socket operations fail | ||
| 59 | + //! \throws HueException when response contained no body | ||
| 60 | + virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const; | ||
| 61 | + | ||
| 62 | + //! \brief Send a multicast request with a specified message. | ||
| 63 | + //! | ||
| 64 | + //! \param msg The message that should sent to the specified multicast address | ||
| 65 | + //! \param adr Optional ip or hostname in dotted decimal notation, default is "239.255.255.250" | ||
| 66 | + //! \param port Optional port the request is sent to, default is 1900 | ||
| 67 | + //! \param timeout Optional time to wait for responses in seconds, default is 5 | ||
| 81 | //! | 68 | //! |
| 82 | - //! \param msg String that contains the request that should be sent to the | ||
| 83 | - //! specified address \param adr Optional String that contains an ip or | ||
| 84 | - //! hostname in dotted decimal notation, default is "239.255.255.250" \param | ||
| 85 | - //! port Optional integer that specifies the port to which the request should | ||
| 86 | - //! be sent. Default is 1900 \param timeout Optional Integer that specifies | ||
| 87 | - //! the timeout of the request in seconds. Default is 5 \return Vector | ||
| 88 | - //! containing strings of each answer received | 69 | + //! Blocks for the duration of the timeout. |
| 70 | + //! | ||
| 71 | + //! \return vector of strings containing each received answer | ||
| 72 | + //! \throws std::system_error when system or socket operations fail | ||
| 89 | virtual std::vector<std::string> sendMulticast( | 73 | virtual std::vector<std::string> sendMulticast( |
| 90 | const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; | 74 | const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; |
| 91 | 75 | ||
| 92 | - //! \brief Virtual function that should send a HTTP request with the given | ||
| 93 | - //! method to the specified host and return the body of the response. | 76 | + //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. |
| 94 | //! | 77 | //! |
| 95 | - //! Note body can also be left empty! | ||
| 96 | - //! \param method String that contains the HTTP method type e.g. GET, HEAD, | ||
| 97 | - //! POST, PUT, DELETE, ... \param uri String that contains the uniform | ||
| 98 | - //! resource identifier \param content_type String that contains the | ||
| 99 | - //! type(MIME) of the body data e.g. "text/html", "application/json", ... | ||
| 100 | - //! \param body String that contains the data of the request | ||
| 101 | - //! \param adr String that contains an ip or hostname in dotted decimal | ||
| 102 | - //! notation like "192.168.2.1" \param port Optional integer that specifies | ||
| 103 | - //! the port to which the request is sent to. Default is 80 \return String | ||
| 104 | - //! containing the body of the response of the host | ||
| 105 | - virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, | ||
| 106 | - const std::string& adr, int port = 80) const | ||
| 107 | - { | ||
| 108 | - std::string request; | ||
| 109 | - // Protocol reference: | ||
| 110 | - // https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html Request-Line | ||
| 111 | - request.append(method); // Method | ||
| 112 | - request.append(" "); // Separation | ||
| 113 | - request.append(uri); // Request-URI | ||
| 114 | - request.append(" "); // Separation | ||
| 115 | - request.append("HTTP/1.0"); // HTTP-Version | ||
| 116 | - request.append("\r\n"); // Ending | ||
| 117 | - // Entities | ||
| 118 | - request.append("Content-Type:"); // entity-header | ||
| 119 | - request.append(" "); // Separation | ||
| 120 | - request.append(content_type); // media-type | ||
| 121 | - request.append("\r\n"); // Entity ending | ||
| 122 | - request.append("Content-Length:"); // entity-header | ||
| 123 | - request.append(" "); // Separation | ||
| 124 | - request.append(std::to_string(body.size())); // length | ||
| 125 | - request.append("\r\n\r\n"); // Entity ending & Request-Line ending | ||
| 126 | - request.append(body); // message-body | ||
| 127 | - request.append("\r\n\r\n"); // Ending | ||
| 128 | - | ||
| 129 | - return sendGetHTTPBody(request.c_str(), adr, port); | ||
| 130 | - }; | ||
| 131 | - | ||
| 132 | - //! \brief Virtual function that should send a HTTP GET request to the | ||
| 133 | - //! specified host and return the body of the response. | 78 | + //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... |
| 79 | + //! \param uri Uniform Resource Identifier in the request | ||
| 80 | + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... | ||
| 81 | + //! \param body Request body, may be empty | ||
| 82 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 83 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 84 | + //! \return Body of the response of the host | ||
| 85 | + //! \throws std::system_error when system or socket operations fail | ||
| 86 | + //! \throws HueException when response contained no body | ||
| 87 | + virtual std::string sendHTTPRequest(const std::string& method, const std::string& uri, | ||
| 88 | + const std::string& contentType, const std::string& body, const std::string& adr, int port = 80) const; | ||
| 89 | + | ||
| 90 | + //! \brief Send a HTTP GET request to the specified host and return the body of the response. | ||
| 134 | //! | 91 | //! |
| 135 | - //! Note body can also be left empty! | ||
| 136 | - //! \param uri String that contains the uniform resource identifier | ||
| 137 | - //! \param content_type String that contains the type(MIME) of the body data | ||
| 138 | - //! e.g. "text/html", "application/json", ... \param body String that contains | ||
| 139 | - //! the data of the request \param adr String that contains an ip or hostname | ||
| 140 | - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer | 92 | + //! \param uri Uniform Resource Identifier in the request |
| 93 | + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... | ||
| 94 | + //! \param body Request body, may be empty | ||
| 95 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 96 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 141 | //! that specifies the port to which the request is sent to. Default is 80 | 97 | //! that specifies the port to which the request is sent to. Default is 80 |
| 142 | - //! \return String containing the body of the response of the host | ||
| 143 | - virtual std::string GETString( | ||
| 144 | - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const | ||
| 145 | - { | ||
| 146 | - return sendHTTPRequest("GET", uri, content_type, body, adr, port); | ||
| 147 | - }; | 98 | + //! \return Body of the response of the host |
| 99 | + //! \throws std::system_error when system or socket operations fail | ||
| 100 | + //! \throws HueException when response contained no body | ||
| 101 | + virtual std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 102 | + const std::string& adr, int port = 80) const; | ||
| 148 | 103 | ||
| 149 | - //! \brief Virtual function that should send a HTTP POST request to the | ||
| 150 | - //! specified host and returns the body of the response. | 104 | + //! \brief Send a HTTP POST request to the specified host and return the body of the response. |
| 151 | //! | 105 | //! |
| 152 | - //! Note body can also be left empty! | ||
| 153 | - //! \param uri String that contains the uniform resource identifier | ||
| 154 | - //! \param content_type String that contains the type(MIME) of the body data | ||
| 155 | - //! e.g. "text/html", "application/json", ... \param body String that contains | ||
| 156 | - //! the data of the request \param adr String that contains an ip or hostname | ||
| 157 | - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer | 106 | + //! \param uri Uniform Resource Identifier in the request |
| 107 | + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... | ||
| 108 | + //! \param body Request body, may be empty | ||
| 109 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 110 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 158 | //! that specifies the port to which the request is sent to. Default is 80 | 111 | //! that specifies the port to which the request is sent to. Default is 80 |
| 159 | - //! \return String containing the body of the response of the host | ||
| 160 | - virtual std::string POSTString( | ||
| 161 | - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const | ||
| 162 | - { | ||
| 163 | - return sendHTTPRequest("POST", uri, content_type, body, adr, port); | ||
| 164 | - }; | 112 | + //! \return Body of the response of the host |
| 113 | + //! \throws std::system_error when system or socket operations fail | ||
| 114 | + //! \throws HueException when response contained no body | ||
| 115 | + virtual std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 116 | + const std::string& adr, int port = 80) const; | ||
| 165 | 117 | ||
| 166 | - //! \brief Virtual function that should send a HTTP PUT request to the | ||
| 167 | - //! specified host and return the body of the response. | 118 | + //! \brief Send a HTTP PUT request to the specified host and return the body of the response. |
| 168 | //! | 119 | //! |
| 169 | - //! Note body can also be left empty! | ||
| 170 | - //! \param uri String that contains the uniform resource identifier | ||
| 171 | - //! \param content_type String that contains the type(MIME) of the body data | ||
| 172 | - //! e.g. "text/html", "application/json", ... \param body String that contains | ||
| 173 | - //! the data of the request \param adr String that contains an ip or hostname | ||
| 174 | - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer | 120 | + //! \param uri Uniform Resource Identifier in the request |
| 121 | + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... | ||
| 122 | + //! \param body Request body, may be empty | ||
| 123 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 124 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 175 | //! that specifies the port to which the request is sent to. Default is 80 | 125 | //! that specifies the port to which the request is sent to. Default is 80 |
| 176 | - //! \return String containing the body of the response of the host | ||
| 177 | - virtual std::string PUTString( | ||
| 178 | - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const | ||
| 179 | - { | ||
| 180 | - return sendHTTPRequest("PUT", uri, content_type, body, adr, port); | ||
| 181 | - }; | 126 | + //! \return Body of the response of the host |
| 127 | + //! \throws std::system_error when system or socket operations fail | ||
| 128 | + //! \throws HueException when response contained no body | ||
| 129 | + virtual std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 130 | + const std::string& adr, int port = 80) const; | ||
| 182 | 131 | ||
| 183 | - //! \brief Virtual function that should send a HTTP DELETE request to the | ||
| 184 | - //! specified host and return the body of the response. | 132 | + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. |
| 185 | //! | 133 | //! |
| 186 | - //! Note body can also be left empty! | ||
| 187 | - //! \param uri String that contains the uniform resource identifier | ||
| 188 | - //! \param content_type String that contains the type(MIME) of the body data | ||
| 189 | - //! e.g. "text/html", "application/json", ... \param body String that contains | ||
| 190 | - //! the data of the request \param adr String that contains an ip or hostname | ||
| 191 | - //! in dotted decimal notation like "192.168.2.1" \param port Optional integer | 134 | + //! \param uri Uniform Resource Identifier in the request |
| 135 | + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... | ||
| 136 | + //! \param body Request body, may be empty | ||
| 137 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 138 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 192 | //! that specifies the port to which the request is sent to. Default is 80 | 139 | //! that specifies the port to which the request is sent to. Default is 80 |
| 193 | - //! \return String containing the body of the response of the host | ||
| 194 | - virtual std::string DELETEString( | ||
| 195 | - std::string uri, std::string content_type, std::string body, const std::string& adr, int port = 80) const | ||
| 196 | - { | ||
| 197 | - return sendHTTPRequest("DELETE", uri, content_type, body, adr, port); | ||
| 198 | - }; | 140 | + //! \return Body of the response of the host |
| 141 | + //! \throws std::system_error when system or socket operations fail | ||
| 142 | + //! \throws HueException when response contained no body | ||
| 143 | + virtual std::string DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, | ||
| 144 | + const std::string& adr, int port = 80) const; | ||
| 199 | 145 | ||
| 200 | - //! \brief Virtual function that should send a HTTP GET request to the | ||
| 201 | - //! specified host and return the body of the response. | 146 | + //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. |
| 202 | //! | 147 | //! |
| 203 | - //! Note body can also be left empty! | ||
| 204 | - //! \param uri String that contains the uniform resource identifier | ||
| 205 | - //! \param body nlohmann::json that contains the data of the request | ||
| 206 | - //! \param adr String that contains an ip or hostname in dotted decimal | ||
| 207 | - //! notation like "192.168.2.1" \param port Optional integer that specifies | ||
| 208 | - //! the port to which the request is sent to. Default is 80 \return | ||
| 209 | - //! nlohmann::json containing the parsed body of the response of the host | 148 | + //! \param uri Uniform Resource Identifier in the request |
| 149 | + //! \param body Request body, may be empty | ||
| 150 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 151 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 152 | + //! \return Parsed body of the response of the host | ||
| 153 | + //! \throws std::system_error when system or socket operations fail | ||
| 154 | + //! \throws HueException when response contained no body | ||
| 155 | + //! \throws nlohmann::json::parse_error when the body could not be parsed | ||
| 210 | virtual nlohmann::json GETJson( | 156 | virtual nlohmann::json GETJson( |
| 211 | - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const | ||
| 212 | - { | ||
| 213 | - return strToJsonValue(GETString(uri, "application/json", body.dump(), adr, port)); | ||
| 214 | - }; | 157 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; |
| 158 | + ; | ||
| 215 | 159 | ||
| 216 | - //! \brief Virtual function that should send a HTTP POST request to the | ||
| 217 | - //! specified host and return the body of the response. | 160 | + //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. |
| 218 | //! | 161 | //! |
| 219 | - //! Note body can also be left empty! | ||
| 220 | - //! \param uri String that contains the uniform resource identifier | ||
| 221 | - //! \param body nlohmann::json that contains the data of the request | ||
| 222 | - //! \param adr String that contains an ip or hostname in dotted decimal | ||
| 223 | - //! notation like "192.168.2.1" \param port Optional integer that specifies | ||
| 224 | - //! the port to which the request is sent to. Default is 80 \return | ||
| 225 | - //! nlohmann::json containing the parsed body of the response of the host | 162 | + //! \param uri Uniform Resource Identifier in the request |
| 163 | + //! \param body Request body, may be empty | ||
| 164 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 165 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 166 | + //! \return Parsed body of the response of the host | ||
| 167 | + //! \throws std::system_error when system or socket operations fail | ||
| 168 | + //! \throws HueException when response contained no body | ||
| 169 | + //! \throws nlohmann::json::parse_error when the body could not be parsed | ||
| 226 | virtual nlohmann::json POSTJson( | 170 | virtual nlohmann::json POSTJson( |
| 227 | - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const | ||
| 228 | - { | ||
| 229 | - return strToJsonValue(POSTString(uri, "application/json", body.dump(), adr, port)); | ||
| 230 | - } | 171 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; |
| 231 | 172 | ||
| 232 | - //! \brief Virtual function that should send a HTTP PUT request to the | ||
| 233 | - //! specified host and return the body of the response. | 173 | + //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. |
| 234 | //! | 174 | //! |
| 235 | - //! Note body can also be left empty! | ||
| 236 | - //! \param uri String that contains the uniform resource identifier | ||
| 237 | - //! \param body nlohmann::json that contains the data of the request | ||
| 238 | - //! \param adr String that contains an ip or hostname in dotted decimal | ||
| 239 | - //! notation like "192.168.2.1" \param port Optional integer that specifies | ||
| 240 | - //! the port to which the request is sent to. Default is 80 \return | ||
| 241 | - //! nlohmann::json containing the parsed body of the response of the host | 175 | + //! \param uri Uniform Resource Identifier in the request |
| 176 | + //! \param body Request body, may be empty | ||
| 177 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 178 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 179 | + //! \return Parsed body of the response of the host | ||
| 180 | + //! \throws std::system_error when system or socket operations fail | ||
| 181 | + //! \throws HueException when response contained no body | ||
| 182 | + //! \throws nlohmann::json::parse_error when the body could not be parsed | ||
| 242 | virtual nlohmann::json PUTJson( | 183 | virtual nlohmann::json PUTJson( |
| 243 | - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const | ||
| 244 | - { | ||
| 245 | - return strToJsonValue(PUTString(uri, "application/json", body.dump(), adr, port)); | ||
| 246 | - }; | 184 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; |
| 247 | 185 | ||
| 248 | - //! \brief Virtual function that should send a HTTP DELETE request to the | ||
| 249 | - //! specified host and return the body of the response. | 186 | + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. |
| 250 | //! | 187 | //! |
| 251 | - //! Note body can also be left empty! | ||
| 252 | - //! \param uri String that contains the uniform resource identifier | ||
| 253 | - //! \param body nlohmann::json that contains the data of the request | ||
| 254 | - //! \param adr String that contains an ip or hostname in dotted decimal | ||
| 255 | - //! notation like "192.168.2.1" \param port Optional integer that specifies | ||
| 256 | - //! the port to which the request is sent to. Default is 80 \return | ||
| 257 | - //! nlohmann::json containing the parsed body of the response of the host | 188 | + //! \param uri Uniform Resource Identifier in the request |
| 189 | + //! \param body Request body, may be empty | ||
| 190 | + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" | ||
| 191 | + //! \param port Optional port the request is sent to, default is 80 | ||
| 192 | + //! \return Parsed body of the response of the host | ||
| 193 | + //! \throws std::system_error when system or socket operations fail | ||
| 194 | + //! \throws HueException when response contained no body | ||
| 195 | + //! \throws nlohmann::json::parse_error when the body could not be parsed | ||
| 258 | virtual nlohmann::json DELETEJson( | 196 | virtual nlohmann::json DELETEJson( |
| 259 | - std::string uri, const nlohmann::json& body, const std::string& adr, int port = 80) const | ||
| 260 | - { | ||
| 261 | - return strToJsonValue(DELETEString(uri, "application/json", body.dump(), adr, port)); | ||
| 262 | - }; | ||
| 263 | - | ||
| 264 | -private: | ||
| 265 | - //! \brief Function that converts a given string to a nlohmann::json | ||
| 266 | - //! | ||
| 267 | - //! \param str String that gets converted | ||
| 268 | - //! \return nlohmann::json containing parsed string | ||
| 269 | - nlohmann::json strToJsonValue(std::string str) const { return nlohmann::json::parse(str); } | 197 | + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const; |
| 198 | + ; | ||
| 270 | }; | 199 | }; |
| 271 | 200 | ||
| 272 | #endif | 201 | #endif |
hueplusplus/include/HueException.h
| @@ -35,7 +35,7 @@ class HueException : public std::exception | @@ -35,7 +35,7 @@ class HueException : public std::exception | ||
| 35 | { | 35 | { |
| 36 | public: | 36 | public: |
| 37 | //! \brief Creates HueException with information about the thrown location | 37 | //! \brief Creates HueException with information about the thrown location |
| 38 | - HueException(FileInfo fileInfo); | 38 | + HueException(FileInfo fileInfo, const std::string& message); |
| 39 | 39 | ||
| 40 | const char* what() const override; | 40 | const char* what() const override; |
| 41 | 41 | ||
| @@ -46,10 +46,10 @@ protected: | @@ -46,10 +46,10 @@ protected: | ||
| 46 | //! \brief Creates HueException with child class name | 46 | //! \brief Creates HueException with child class name |
| 47 | //! | 47 | //! |
| 48 | //! Should be used by subclasses which can append additional information to the end of whatMessage. | 48 | //! Should be used by subclasses which can append additional information to the end of whatMessage. |
| 49 | - HueException(const char* exceptionName, FileInfo fileInfo); | ||
| 50 | - std::string whatMessage; | 49 | + HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message); |
| 51 | 50 | ||
| 52 | private: | 51 | private: |
| 52 | + std::string whatMessage; | ||
| 53 | FileInfo fileInfo; | 53 | FileInfo fileInfo; |
| 54 | }; | 54 | }; |
| 55 | 55 | ||
| @@ -63,6 +63,9 @@ public: | @@ -63,6 +63,9 @@ public: | ||
| 63 | const std::string& GetDescription() const noexcept; | 63 | const std::string& GetDescription() const noexcept; |
| 64 | 64 | ||
| 65 | private: | 65 | private: |
| 66 | + static std::string GetMessage(int error, const std::string& addr, const std::string& description); | ||
| 67 | + | ||
| 68 | +private: | ||
| 66 | int error; | 69 | int error; |
| 67 | std::string address; | 70 | std::string address; |
| 68 | std::string description; | 71 | std::string description; |
hueplusplus/include/HueLight.h
| @@ -649,7 +649,7 @@ protected: | @@ -649,7 +649,7 @@ protected: | ||
| 649 | 649 | ||
| 650 | //! \brief Utility function to send a put request to the light. | 650 | //! \brief Utility function to send a put request to the light. |
| 651 | //! | 651 | //! |
| 652 | - //! \throws std::runtime_error if the reply could not be parsed | 652 | + //! \throws nlohmann::json::parse_error if the reply could not be parsed |
| 653 | //! \param request A nlohmann::json aka the request to send | 653 | //! \param request A nlohmann::json aka the request to send |
| 654 | //! \param subPath A path that is appended to the uri, note it should always | 654 | //! \param subPath A path that is appended to the uri, note it should always |
| 655 | //! start with a slash ("/") \return The parsed reply | 655 | //! start with a slash ("/") \return The parsed reply |