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
21 #include <grpcpp/channel.h>
22 #include <grpcpp/create_channel.h>
23 #include <grpcpp/impl/grpc_library.h>
24 #include <grpcpp/security/credentials.h>
25 #include <grpcpp/support/channel_arguments.h>
26
27 #include "src/cpp/client/create_channel_internal.h"
28
29 namespace grpc {
CreateChannel(const grpc::string & target,const std::shared_ptr<grpc::ChannelCredentials> & creds)30 std::shared_ptr<grpc::Channel> CreateChannel(
31 const grpc::string& target,
32 const std::shared_ptr<grpc::ChannelCredentials>& creds) {
33 return CreateCustomChannel(target, creds, grpc::ChannelArguments());
34 }
35
CreateCustomChannel(const grpc::string & target,const std::shared_ptr<grpc::ChannelCredentials> & creds,const grpc::ChannelArguments & args)36 std::shared_ptr<grpc::Channel> CreateCustomChannel(
37 const grpc::string& target,
38 const std::shared_ptr<grpc::ChannelCredentials>& creds,
39 const grpc::ChannelArguments& args) {
40 grpc::GrpcLibraryCodegen
41 init_lib; // We need to call init in case of bad creds.
42 return creds ? creds->CreateChannelImpl(target, args)
43 : grpc::CreateChannelInternal(
44 "",
45 grpc_lame_client_channel_create(
46 nullptr, GRPC_STATUS_INVALID_ARGUMENT,
47 "Invalid credentials."),
48 std::vector<std::unique_ptr<
49 grpc::experimental::
50 ClientInterceptorFactoryInterface>>());
51 }
52
53 namespace experimental {
54 /// Create a new \em custom \a Channel pointing to \a target with \a
55 /// interceptors being invoked per call.
56 ///
57 /// \warning For advanced use and testing ONLY. Override default channel
58 /// arguments only if necessary.
59 ///
60 /// \param target The URI of the endpoint to connect to.
61 /// \param creds Credentials to use for the created channel. If it does not
62 /// hold an object or is invalid, a lame channel (one on which all operations
63 /// fail) is returned.
64 /// \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)65 std::shared_ptr<grpc::Channel> CreateCustomChannelWithInterceptors(
66 const std::string& target,
67 const std::shared_ptr<grpc::ChannelCredentials>& creds,
68 const grpc::ChannelArguments& args,
69 std::vector<
70 std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
71 interceptor_creators) {
72 grpc::GrpcLibraryCodegen
73 init_lib; // We need to call init in case of bad creds.
74 return creds ? creds->CreateChannelWithInterceptors(
75 target, args, std::move(interceptor_creators))
76 : grpc::CreateChannelInternal(
77 "",
78 grpc_lame_client_channel_create(
79 nullptr, GRPC_STATUS_INVALID_ARGUMENT,
80 "Invalid credentials."),
81 std::move(interceptor_creators));
82 }
83 } // namespace experimental
84
85 } // namespace grpc
86