xref: /aosp_15_r20/external/federated-compute/fcp/client/http/curl/curl_http_client.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FCP_CLIENT_HTTP_CURL_CURL_HTTP_CLIENT_H_
18 #define FCP_CLIENT_HTTP_CURL_CURL_HTTP_CLIENT_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/synchronization/mutex.h"
26 #include "fcp/client/http/curl/curl_api.h"
27 #include "fcp/client/http/http_client.h"
28 
29 namespace fcp::client::http::curl {
30 
31 // A curl-based implementation of the HttpClient interface that uses
32 // CurlHttpRequestHandle underneath. The implementation assumes that
33 // CurlHttpClient lives longer than CurlHttpRequestHandle; and
34 // CurlApi lives longer than CurlHttpClient
35 class CurlHttpClient : public HttpClient {
36  public:
37   explicit CurlHttpClient(CurlApi* curl_api, std::string test_cert_path = "");
38   ~CurlHttpClient() override = default;
39   CurlHttpClient(const CurlHttpClient&) = delete;
40   CurlHttpClient& operator=(const CurlHttpClient&) = delete;
41 
42   // HttpClient overrides:
43   std::unique_ptr<HttpRequestHandle> EnqueueRequest(
44       std::unique_ptr<HttpRequest> request) override;
45 
46   // Performs the given requests while blocked. Results will be returned to each
47   // corresponding `HttpRequestCallback`.
48   absl::Status PerformRequests(
49       std::vector<std::pair<HttpRequestHandle*, HttpRequestCallback*>> requests)
50       override;
51 
52  private:
53   // Owned by the caller
54   const CurlApi* const curl_api_;
55   const std::string test_cert_path_;
56 };
57 
58 }  // namespace fcp::client::http::curl
59 
60 #endif  // FCP_CLIENT_HTTP_CURL_CURL_HTTP_CLIENT_H_
61