1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMUNICATIONNETSTACK_HTTP_CLIENT_H 17 #define COMMUNICATIONNETSTACK_HTTP_CLIENT_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <iostream> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 #include <queue> 26 #include <string> 27 #include <thread> 28 #include <vector> 29 30 #include "http_client_error.h" 31 #include "http_client_request.h" 32 #include "http_client_task.h" 33 34 namespace OHOS::NetStack::HttpOverCurl { 35 struct TransferCallbacks; 36 } 37 38 namespace OHOS { 39 namespace NetStack { 40 namespace HttpClient { 41 class HttpSession { 42 public: 43 /** 44 * Gets the singleton instance of HttpSession. 45 * @return The singleton instance of HttpSession. 46 */ 47 static HttpSession &GetInstance(); 48 49 /** 50 * Creates an HTTP client task with the provided request. 51 * @param request The HTTP request to be executed. 52 * @return A shared pointer to the created HttpClientTask object. 53 */ 54 [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request); 55 56 /** 57 * Creates an HTTP client task with the provided request and file path. 58 * @param request The HTTP request to be executed. 59 * @param type The type of the task. 60 * @param filePath The file path to read the uploaded file (applicable for upload tasks). 61 * @return A shared pointer to the created HttpClientTask object. 62 */ 63 [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request, TaskType type, 64 const std::string &filePath); 65 66 private: 67 friend class HttpClientTask; 68 69 /** 70 * Default constructor. 71 */ 72 HttpSession(); 73 ~HttpSession(); 74 75 /** 76 * Starts the specified HTTP client task. 77 * @param ptr A shared pointer to the HttpClientTask object. 78 */ 79 void StartTask(const std::shared_ptr<HttpClientTask> &ptr); 80 81 /** 82 * Set RequestInfo callbacks. 83 * @param callbacks A structure object of callback functions for RequestInfo. 84 * @param ptr A shared pointer to the HttpClientTask object. 85 */ 86 void SetRequestInfoCallbacks( 87 HttpOverCurl::TransferCallbacks &callbacks, const std::shared_ptr<HttpClientTask> &ptr); 88 }; 89 } // namespace HttpClient 90 } // namespace NetStack 91 } // namespace OHOS 92 93 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_H