Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_EXAMPLES_PROVIDER_CURL_HTTP_CLIENT_H_ |
| 6 | #define LIBWEAVE_EXAMPLES_PROVIDER_CURL_HTTP_CLIENT_H_ |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 7 | |
Vitaly Buka | a627e12 | 2015-11-06 14:55:53 -0800 | [diff] [blame] | 8 | #include <future> |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 9 | #include <string> |
Vitaly Buka | a627e12 | 2015-11-06 14:55:53 -0800 | [diff] [blame] | 10 | #include <utility> |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 11 | |
| 12 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 13 | #include <weave/provider/http_client.h> |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 14 | |
| 15 | namespace weave { |
| 16 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 17 | namespace provider { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 18 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 19 | } |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 20 | |
| 21 | namespace examples { |
| 22 | |
| 23 | // Basic implementation of weave::HttpClient using libcurl. Should be used in |
| 24 | // production code as it's blocking and does not validate server certificates. |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 25 | class CurlHttpClient : public provider::HttpClient { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 26 | public: |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 27 | explicit CurlHttpClient(provider::TaskRunner* task_runner); |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 28 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 29 | void SendRequest(Method method, |
Vitaly Buka | 866b60a | 2015-10-09 14:24:55 -0700 | [diff] [blame] | 30 | const std::string& url, |
| 31 | const Headers& headers, |
| 32 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 33 | const SendRequestCallback& callback) override; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 34 | |
| 35 | private: |
Vitaly Buka | a627e12 | 2015-11-06 14:55:53 -0800 | [diff] [blame] | 36 | void CheckTasks(); |
| 37 | |
| 38 | std::vector< |
| 39 | std::pair<std::future<std::pair<std::unique_ptr<Response>, ErrorPtr>>, |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 40 | SendRequestCallback>> |
| 41 | pending_tasks_; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 42 | provider::TaskRunner* task_runner_{nullptr}; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 43 | |
| 44 | base::WeakPtrFactory<CurlHttpClient> weak_ptr_factory_{this}; |
| 45 | }; |
| 46 | |
| 47 | } // namespace examples |
| 48 | } // namespace weave |
| 49 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 50 | #endif // LIBWEAVE_EXAMPLES_PROVIDER_CURL_HTTP_CLIENT_H_ |