1 /*
2  * Copyright 1999-2019 Alibaba Cloud All rights reserved.
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 CORE_INCLUDE_ALIBABACLOUD_CORE_COMMONCLIENT_H_
18 #define CORE_INCLUDE_ALIBABACLOUD_CORE_COMMONCLIENT_H_
19 
20 #include "AsyncCallerContext.h"
21 #include "ClientConfiguration.h"
22 #include "CommonRequest.h"
23 #include "CommonResponse.h"
24 #include "CoreClient.h"
25 #include "CoreExport.h"
26 #include "CredentialsProvider.h"
27 #include <future>
28 #include <memory>
29 #include <string>
30 
31 namespace AlibabaCloud {
32 class ALIBABACLOUD_CORE_EXPORT CommonClient : public CoreClient {
33 public:
34   typedef Outcome<Error, CommonResponse> CommonResponseOutcome;
35   typedef std::future<CommonResponseOutcome> CommonResponseOutcomeCallable;
36   typedef std::function<void(const CommonClient *, const CommonRequest &,
37                              const CommonResponseOutcome &,
38                              const std::shared_ptr<const AsyncCallerContext> &)>
39       CommonResponseAsyncHandler;
40   typedef Outcome<Error, std::string> JsonOutcome;
41 
42   CommonClient(const Credentials &credentials,
43                const ClientConfiguration &configuration);
44   CommonClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider,
45                const ClientConfiguration &configuration);
46   CommonClient(const std::string &accessKeyId,
47                const std::string &accessKeySecret,
48                const ClientConfiguration &configuration);
49   ~CommonClient();
50 
51   CommonResponseOutcome commonResponse(const CommonRequest &request) const;
52   void commonResponseAsync(
53       const CommonRequest &request, const CommonResponseAsyncHandler &handler,
54       const std::shared_ptr<const AsyncCallerContext> &context = nullptr) const;
55   CommonResponseOutcomeCallable
56   commonResponseCallable(const CommonRequest &request) const;
57 
58 protected:
59   HttpRequest buildHttpRequest(const std::string &endpoint,
60                                const ServiceRequest &msg,
61                                HttpRequest::Method method) const override;
62   HttpRequest buildHttpRequest(const std::string &endpoint,
63                                const CommonRequest &msg,
64                                HttpRequest::Method method) const;
65   HttpRequest buildRoaHttpRequest(const std::string &endpoint,
66                                   const CommonRequest &msg,
67                                   HttpRequest::Method method) const;
68   HttpRequest buildRpcHttpRequest(const std::string &endpoint,
69                                   const CommonRequest &msg,
70                                   HttpRequest::Method method) const;
71   JsonOutcome
72   makeRequest(const std::string &endpoint, const CommonRequest &msg,
73               HttpRequest::Method method = HttpRequest::Method::Get) const;
74   using CoreClient::asyncExecute;
75 
76 private:
77   std::shared_ptr<CredentialsProvider> credentialsProvider_;
78   std::shared_ptr<Signer> signer_;
79 };
80 } // namespace AlibabaCloud
81 #endif // CORE_INCLUDE_ALIBABACLOUD_CORE_COMMONCLIENT_H_
82