Commit 85fba134915f7d575335dcaa44e2ebda5e8f7d05
1 parent
a3900e59
Closes #9
- exported duplicate code to another function
Showing
1 changed file
with
26 additions
and
54 deletions
hueplusplus/include/IHttpHandler.h
100644 → 100755
| ... | ... | @@ -31,7 +31,6 @@ |
| 31 | 31 | class IHttpHandler |
| 32 | 32 | { |
| 33 | 33 | public: |
| 34 | - | |
| 35 | 34 | //! \brief Virtual dtor |
| 36 | 35 | virtual ~IHttpHandler() = default; |
| 37 | 36 | |
| ... | ... | @@ -174,19 +173,7 @@ public: |
| 174 | 173 | //! \return Json::Value containing the parsed body of the response of the host |
| 175 | 174 | virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const |
| 176 | 175 | { |
| 177 | - std::string response = GETString(uri, "application/json", body.toStyledString(), adr, port); | |
| 178 | - | |
| 179 | - std::string error; | |
| 180 | - Json::Value result; | |
| 181 | - Json::CharReaderBuilder builder; | |
| 182 | - builder["collectComments"] = false; | |
| 183 | - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 184 | - if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) | |
| 185 | - { | |
| 186 | - std::cout << "IHttpHandler: Error while parsing JSON in function GETJson(): " << error << std::endl; | |
| 187 | - throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function GETJson()")); | |
| 188 | - } | |
| 189 | - return result; | |
| 176 | + return strToJsonValue(GETString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 190 | 177 | }; |
| 191 | 178 | |
| 192 | 179 | //! \brief Virtual function that should send a HTTP POST request to the specified host and return the body of the response. |
| ... | ... | @@ -199,19 +186,7 @@ public: |
| 199 | 186 | //! \return Json::Value containing the parsed body of the response of the host |
| 200 | 187 | virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const |
| 201 | 188 | { |
| 202 | - std::string response = POSTString(uri, "application/json", body.toStyledString(), adr, port); | |
| 203 | - | |
| 204 | - std::string error; | |
| 205 | - Json::Value result; | |
| 206 | - Json::CharReaderBuilder builder; | |
| 207 | - builder["collectComments"] = false; | |
| 208 | - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 209 | - if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) | |
| 210 | - { | |
| 211 | - std::cout << "IHttpHandler: Error while parsing JSON in function POSTJson(): " << error << std::endl; | |
| 212 | - throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function POSTJson()")); | |
| 213 | - } | |
| 214 | - return result; | |
| 189 | + return strToJsonValue(POSTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 215 | 190 | } |
| 216 | 191 | |
| 217 | 192 | //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. |
| ... | ... | @@ -224,19 +199,7 @@ public: |
| 224 | 199 | //! \return Json::Value containing the parsed body of the response of the host |
| 225 | 200 | virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const |
| 226 | 201 | { |
| 227 | - std::string response = PUTString(uri, "application/json", body.toStyledString(), adr, port); | |
| 228 | - | |
| 229 | - std::string error; | |
| 230 | - Json::Value result; | |
| 231 | - Json::CharReaderBuilder builder; | |
| 232 | - builder["collectComments"] = false; | |
| 233 | - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 234 | - if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) | |
| 235 | - { | |
| 236 | - std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; | |
| 237 | - throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); | |
| 238 | - } | |
| 239 | - return result; | |
| 202 | + return strToJsonValue(PUTString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 240 | 203 | }; |
| 241 | 204 | |
| 242 | 205 | //! \brief Virtual function that should send a HTTP DELETE request to the specified host and return the body of the response. |
| ... | ... | @@ -248,21 +211,30 @@ public: |
| 248 | 211 | //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 |
| 249 | 212 | //! \return Json::Value containing the parsed body of the response of the host |
| 250 | 213 | virtual Json::Value DELETEJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const |
| 251 | - { | |
| 252 | - std::string response = DELETEString(uri, "application/json", body.toStyledString(), adr, port); | |
| 214 | + { | |
| 215 | + return strToJsonValue(DELETEString(uri, "application/json", body.toStyledString(), adr, port)); | |
| 216 | + }; | |
| 253 | 217 | |
| 254 | - std::string error; | |
| 255 | - Json::Value result; | |
| 256 | - Json::CharReaderBuilder builder; | |
| 257 | - builder["collectComments"] = false; | |
| 258 | - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 259 | - if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error)) | |
| 260 | - { | |
| 261 | - std::cout << "IHttpHandler: Error while parsing JSON in function PUTJson(): " << error << std::endl; | |
| 262 | - throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function PUTJson()")); | |
| 263 | - } | |
| 264 | - return result; | |
| 265 | - }; | |
| 218 | +private: | |
| 219 | + | |
| 220 | + //! \brief Function that converts a given string to a Json::Value | |
| 221 | + //! | |
| 222 | + //! \param str String that gets converted | |
| 223 | + //! \return Json::Value containing parsed string | |
| 224 | + Json::Value strToJsonValue(std::string str) const | |
| 225 | + { | |
| 226 | + std::string error; | |
| 227 | + Json::Value result; | |
| 228 | + Json::CharReaderBuilder builder; | |
| 229 | + builder["collectComments"] = false; | |
| 230 | + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader()); | |
| 231 | + if (!reader->parse(str.c_str(), str.c_str() + str.length(), &result, &error)) | |
| 232 | + { | |
| 233 | + std::cout << "IHttpHandler: Error while parsing JSON in function strToJsonValue(): " << error << std::endl; | |
| 234 | + throw(std::runtime_error("IHttpHandler: Error while parsing JSON in function strToJsonValue()")); | |
| 235 | + } | |
| 236 | + return result; | |
| 237 | + } | |
| 266 | 238 | }; |
| 267 | 239 | |
| 268 | 240 | #endif | ... | ... |