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 {
35 namespace NetStack {
36 namespace HttpClient {
37 class HttpSession {
38 public:
39     /**
40      * Gets the singleton instance of HttpSession.
41      * @return The singleton instance of HttpSession.
42      */
43     static HttpSession &GetInstance();
44 
45     /**
46      * Creates an HTTP client task with the provided request.
47      * @param request The HTTP request to be executed.
48      * @return A shared pointer to the created HttpClientTask object.
49      */
50     [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request);
51 
52     /**
53      * Creates an HTTP client task with the provided request and file path.
54      * @param request The HTTP request to be executed.
55      * @param type The type of the task.
56      * @param filePath The file path to read the uploaded file (applicable for upload tasks).
57      * @return A shared pointer to the created HttpClientTask object.
58      */
59     [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request, TaskType type,
60                                                              const std::string &filePath);
61 
62 private:
63     friend class HttpClientTask;
64 
65     /**
66      * Default constructor.
67      */
68     HttpSession();
69     ~HttpSession();
70 
71     /**
72      * Starts the specified HTTP client task.
73      * @param ptr A shared pointer to the HttpClientTask object.
74      */
75     void StartTask(const std::shared_ptr<HttpClientTask> &ptr);
76 };
77 } // namespace HttpClient
78 } // namespace NetStack
79 } // namespace OHOS
80 
81 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_H