-
Notifications
You must be signed in to change notification settings - Fork 1
[OS-1068] Provide a timeout to the Http class #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,17 +21,19 @@ | |||||
|
|
||||||
| #include "mercury/http/http_client_interface.h" | ||||||
|
|
||||||
|
|
||||||
| namespace Mercury { | ||||||
| namespace HTTP { | ||||||
|
|
||||||
| #define DEFAULT_HTTP_CLIENT_TIMEOUT 15 // seconds | ||||||
|
|
||||||
| /** | ||||||
| * \class HTTPClient | ||||||
| * \brief Simple implementation of IHTTPClient | ||||||
| */ | ||||||
| class HTTPClient : public IHTTPClient { | ||||||
| public: | ||||||
| HTTPClient(); | ||||||
| HTTPClient(int timeout_secs); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine these two constructors into one?
Suggested change
|
||||||
| std::shared_ptr<JSON_Value> POST( | ||||||
| const std::string& url, | ||||||
| const std::string& body, | ||||||
|
|
@@ -50,6 +52,9 @@ class HTTPClient : public IHTTPClient { | |||||
| const std::string& url, | ||||||
| const std::string& path) override; | ||||||
|
|
||||||
| void set_http_client_timeout(int seconds); | ||||||
| int get_http_client_timeout(); | ||||||
|
|
||||||
| protected: | ||||||
| /** | ||||||
| * \brief Builds and sends a request to a server | ||||||
|
|
@@ -69,6 +74,8 @@ class HTTPClient : public IHTTPClient { | |||||
| const std::map<std::string, std::string>& headers = | ||||||
| std::map<std::string, std::string>(), | ||||||
| const std::string& body = ""); | ||||||
|
|
||||||
| int http_client_timeout; | ||||||
| }; | ||||||
|
|
||||||
| } // namespace HTTP | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,10 +48,13 @@ using Poco::Net::SSLManager; | |
| using Mercury::HTTP::HTTPClient; | ||
|
|
||
|
|
||
| HTTPClient::HTTPClient() { | ||
| HTTPClient::HTTPClient(int timeout_secs = DEFAULT_HTTP_CLIENT_TIMEOUT) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default arguments in header definition, not implementation. |
||
| HTTPSessionInstantiator::registerInstantiator(); | ||
| HTTPSSessionInstantiator::registerInstantiator(); | ||
|
|
||
| // set a default timeout, can be overriden after class instantiation | ||
| set_http_client_timeout(timeout_secs); | ||
|
|
||
| // Prepare for SSLManager | ||
| Poco::SharedPtr<AcceptCertificateHandler> cert = | ||
| new AcceptCertificateHandler(false); | ||
|
|
@@ -62,6 +65,18 @@ HTTPClient::HTTPClient() { | |
| } | ||
|
|
||
|
|
||
| void HTTPClient::set_http_client_timeout(int seconds) | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| http_client_timeout = seconds; | ||
| } | ||
|
|
||
|
|
||
| int HTTPClient::get_http_client_timeout() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise here |
||
| { | ||
| return http_client_timeout; | ||
| } | ||
|
|
||
|
|
||
| std::shared_ptr<JSON_Value> HTTPClient::send_request( | ||
| const std::string& method, | ||
| const std::string& url, | ||
|
|
@@ -89,6 +104,8 @@ std::shared_ptr<JSON_Value> HTTPClient::send_request( | |
| request.set(header.first, header.second); | ||
| } | ||
|
|
||
| session->setTimeout(Poco::Timespan(this->get_http_client_timeout(), 0)); | ||
|
|
||
| std::ostream& os = session->sendRequest(request); | ||
|
|
||
| if (!body.empty()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is 15 seconds too long?