Commit 54cb817fcd12b1a8678b98092f8fc335857e10f9
Committed by
Moritz Wirger
1 parent
c34b3fa1
Fix documentation, add new functions for DELETE request, cleanup
Showing
2 changed files
with
45 additions
and
12 deletions
hueplusplus/HttpHandler.cpp
| ... | ... | @@ -20,12 +20,12 @@ |
| 20 | 20 | #include "include/HttpHandler.h" |
| 21 | 21 | |
| 22 | 22 | #include <chrono> |
| 23 | -#include <netinet/in.h> // struct sockaddr_in, struct sockaddr | |
| 23 | +#include <netinet/in.h> // struct sockaddr_in, struct sockaddr | |
| 24 | 24 | #include <arpa/inet.h> |
| 25 | 25 | #include <iostream> |
| 26 | 26 | #include <memory> |
| 27 | 27 | #include <netdb.h> // struct hostent, gethostbyname |
| 28 | -#include <sys/socket.h> // socket, connect | |
| 28 | +#include <sys/socket.h> // socket, connect | |
| 29 | 29 | #include <stdexcept> |
| 30 | 30 | #include <stdio.h> // printf, sprintf |
| 31 | 31 | #include <stdlib.h> // exit |
| ... | ... | @@ -260,6 +260,11 @@ std::string HttpHandler::PUTString(std::string uri, std::string content_type, st |
| 260 | 260 | return sendHTTPRequest("PUT", uri, content_type, body, adr, port); |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | +std::string HttpHandler::DELETEString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const | |
| 264 | +{ | |
| 265 | + return sendHTTPRequest("DELETE", uri, content_type, body, adr, port); | |
| 266 | +} | |
| 267 | + | |
| 263 | 268 | //! \todo Get rid of duplicate code in GETJson, POSTJson and PUTJson |
| 264 | 269 | Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const |
| 265 | 270 | { |
| ... | ... | @@ -311,3 +316,20 @@ Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const |
| 311 | 316 | } |
| 312 | 317 | return result; |
| 313 | 318 | } |
| 319 | + | |
| 320 | +Json::Value HttpHandler::DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const | |
| 321 | +{ | |
| 322 | + std::string response = DELETEString(uri, "application/json", body.toStyledString(), adr, port); | |
| 323 | + | |
| 324 | + std::string error; | |
| 325 | + Json::Value result; | |
| 326 | + Json::CharReaderBuilder builder; | |
| 327 | + builder["collectComments"] = false; | |
| 328 | + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 329 | + if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) | |
| 330 | + { | |
| 331 | + std::cout << "Error while parsing JSON in function PUTJson() of HttpHandler: " << error << std::endl; | |
| 332 | + throw(std::runtime_error("Error while parsing JSON in function PUTJson() of HttpHandler")); | |
| 333 | + } | |
| 334 | + return result; | |
| 335 | +} | ... | ... |
hueplusplus/include/HttpHandler.h
| ... | ... | @@ -34,7 +34,6 @@ class HttpHandler : public IHttpHandler |
| 34 | 34 | public: |
| 35 | 35 | //! \brief Function that sends a given message to the specified host and returns the response. |
| 36 | 36 | //! |
| 37 | - //! It returns a string containing the response of the host. | |
| 38 | 37 | //! \param msg String that contains the message that is sent to the specified address |
| 39 | 38 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| 40 | 39 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| ... | ... | @@ -43,7 +42,6 @@ public: |
| 43 | 42 | |
| 44 | 43 | //! \brief Function that sends a given message to the specified host and returns the body of the response. |
| 45 | 44 | //! |
| 46 | - //! It returns a string containing only the body of the response of the host. | |
| 47 | 45 | //! Note if no body is found a runtime error is thrown! |
| 48 | 46 | //! \param msg String that contains the message that is sent to the specified address |
| 49 | 47 | //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" |
| ... | ... | @@ -53,7 +51,6 @@ public: |
| 53 | 51 | |
| 54 | 52 | //! \brief Function that sends a multicast request with the specified message. |
| 55 | 53 | //! |
| 56 | - //! It returns a vector containing all responses the multicast request got back | |
| 57 | 54 | //! \param msg String that contains the request that is sent to the specified address |
| 58 | 55 | //! \param adr Optional String that contains an ip or hostname in dotted decimal notation, default is "239.255.255.250" |
| 59 | 56 | //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900 |
| ... | ... | @@ -63,7 +60,6 @@ public: |
| 63 | 60 | |
| 64 | 61 | //! \brief Function that sends a HTTP request with the given method to the specified host and returns the body of the response. |
| 65 | 62 | //! |
| 66 | - //! It returns a string containing only the body of the response of the host. | |
| 67 | 63 | //! Note body can also be left empty! |
| 68 | 64 | //! \param method String that contains the HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... |
| 69 | 65 | //! \param uri String that contains the uniform resource identifier |
| ... | ... | @@ -76,7 +72,6 @@ public: |
| 76 | 72 | |
| 77 | 73 | //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response. |
| 78 | 74 | //! |
| 79 | - //! It returns a string containing only the body of the response of the host. | |
| 80 | 75 | //! Note body can also be left empty! |
| 81 | 76 | //! \param uri String that contains the uniform resource identifier |
| 82 | 77 | //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... |
| ... | ... | @@ -88,7 +83,6 @@ public: |
| 88 | 83 | |
| 89 | 84 | //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response. |
| 90 | 85 | //! |
| 91 | - //! It returns a string containing only the body of the response of the host. | |
| 92 | 86 | //! Note body can also be left empty! |
| 93 | 87 | //! \param uri String that contains the uniform resource identifier |
| 94 | 88 | //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... |
| ... | ... | @@ -100,7 +94,6 @@ public: |
| 100 | 94 | |
| 101 | 95 | //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response. |
| 102 | 96 | //! |
| 103 | - //! It returns a string containing only the body of the response of the host. | |
| 104 | 97 | //! Note body can also be left empty! |
| 105 | 98 | //! \param uri String that contains the uniform resource identifier |
| 106 | 99 | //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... |
| ... | ... | @@ -110,9 +103,19 @@ public: |
| 110 | 103 | //! \return String containing the body of the response of the host |
| 111 | 104 | std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; |
| 112 | 105 | |
| 106 | + //! \brief Function that sends a HTTP DELETE request to the specified host and returns the body of the response. | |
| 107 | + //! | |
| 108 | + //! Note body can also be left empty! | |
| 109 | + //! \param uri String that contains the uniform resource identifier | |
| 110 | + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... | |
| 111 | + //! \param body String that contains the data of the request | |
| 112 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 113 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 114 | + //! \return String containing the body of the response of the host | |
| 115 | + std::string DELETEString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; | |
| 116 | + | |
| 113 | 117 | //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response. |
| 114 | 118 | //! |
| 115 | - //! It returns a Json::Value parsed from the body of the response of the host. | |
| 116 | 119 | //! Note body can also be left empty! |
| 117 | 120 | //! \param uri String that contains the uniform resource identifier |
| 118 | 121 | //! \param body Json::Value that contains the data of the request |
| ... | ... | @@ -123,7 +126,6 @@ public: |
| 123 | 126 | |
| 124 | 127 | //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response. |
| 125 | 128 | //! |
| 126 | - //! It returns a Json::Value parsed from the body of the response of the host. | |
| 127 | 129 | //! Note body can also be left empty! |
| 128 | 130 | //! \param uri String that contains the uniform resource identifier |
| 129 | 131 | //! \param body Json::Value that contains the data of the request |
| ... | ... | @@ -134,7 +136,6 @@ public: |
| 134 | 136 | |
| 135 | 137 | //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response. |
| 136 | 138 | //! |
| 137 | - //! It returns a Json::Value parsed from the body of the response of the host. | |
| 138 | 139 | //! Note body can also be left empty! |
| 139 | 140 | //! \param uri String that contains the uniform resource identifier |
| 140 | 141 | //! \param body Json::Value that contains the data of the request |
| ... | ... | @@ -142,6 +143,16 @@ public: |
| 142 | 143 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 143 | 144 | //! \return Json::Value containing the parsed body of the response of the host |
| 144 | 145 | Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const; |
| 146 | + | |
| 147 | + //! \brief Function that sends a HTTP DELETE request to the specified host and returns the body of the response. | |
| 148 | + //! | |
| 149 | + //! Note body can also be left empty! | |
| 150 | + //! \param uri String that contains the uniform resource identifier | |
| 151 | + //! \param body Json::Value that contains the data of the request | |
| 152 | + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" | |
| 153 | + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 | |
| 154 | + //! \return Json::Value containing the parsed body of the response of the host | |
| 155 | + Json::Value DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const; | |
| 145 | 156 | }; |
| 146 | 157 | |
| 147 | 158 | #endif | ... | ... |