xref: /aosp_15_r20/external/grpc-grpc/src/cpp/client/create_channel.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/status.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/create_channel.h>
28 #include <grpcpp/impl/grpc_library.h>
29 #include <grpcpp/security/credentials.h>
30 #include <grpcpp/support/channel_arguments.h>
31 #include <grpcpp/support/client_interceptor.h>
32 #include <grpcpp/support/config.h>
33 
34 #include "src/cpp/client/create_channel_internal.h"
35 
36 namespace grpc {
CreateChannel(const grpc::string & target,const std::shared_ptr<grpc::ChannelCredentials> & creds)37 std::shared_ptr<grpc::Channel> CreateChannel(
38     const grpc::string& target,
39     const std::shared_ptr<grpc::ChannelCredentials>& creds) {
40   return CreateCustomChannel(target, creds, grpc::ChannelArguments());
41 }
42 
CreateCustomChannel(const grpc::string & target,const std::shared_ptr<grpc::ChannelCredentials> & creds,const grpc::ChannelArguments & args)43 std::shared_ptr<grpc::Channel> CreateCustomChannel(
44     const grpc::string& target,
45     const std::shared_ptr<grpc::ChannelCredentials>& creds,
46     const grpc::ChannelArguments& args) {
47   grpc::internal::GrpcLibrary
48       init_lib;  // We need to call init in case of bad creds.
49   return creds ? creds->CreateChannelImpl(target, args)
50                : grpc::CreateChannelInternal(
51                      "",
52                      grpc_lame_client_channel_create(
53                          nullptr, GRPC_STATUS_INVALID_ARGUMENT,
54                          "Invalid credentials."),
55                      std::vector<std::unique_ptr<
56                          grpc::experimental::
57                              ClientInterceptorFactoryInterface>>());
58 }
59 
60 namespace experimental {
61 /// Create a new \em custom \a Channel pointing to \a target with \a
62 /// interceptors being invoked per call.
63 ///
64 /// \warning For advanced use and testing ONLY. Override default channel
65 /// arguments only if necessary.
66 ///
67 /// \param target The URI of the endpoint to connect to.
68 /// \param creds Credentials to use for the created channel. If it does not
69 /// hold an object or is invalid, a lame channel (one on which all operations
70 /// fail) is returned.
71 /// \param args Options for channel creation.
CreateCustomChannelWithInterceptors(const std::string & target,const std::shared_ptr<grpc::ChannelCredentials> & creds,const grpc::ChannelArguments & args,std::vector<std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> interceptor_creators)72 std::shared_ptr<grpc::Channel> CreateCustomChannelWithInterceptors(
73     const std::string& target,
74     const std::shared_ptr<grpc::ChannelCredentials>& creds,
75     const grpc::ChannelArguments& args,
76     std::vector<
77         std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
78         interceptor_creators) {
79   grpc::internal::GrpcLibrary
80       init_lib;  // We need to call init in case of bad creds.
81   return creds ? creds->CreateChannelWithInterceptors(
82                      target, args, std::move(interceptor_creators))
83                : grpc::CreateChannelInternal(
84                      "",
85                      grpc_lame_client_channel_create(
86                          nullptr, GRPC_STATUS_INVALID_ARGUMENT,
87                          "Invalid credentials."),
88                      std::move(interceptor_creators));
89 }
90 }  // namespace experimental
91 
92 }  // namespace grpc
93