blob: 265c308bc41221b93ec4aa54eee2234b9b8cdaf2 [file] [log] [blame]
David Reiss3f660a42010-08-30 22:05:29 +00001%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19
20-module(thrift_client_util).
21
22-export([new/4]).
23
24%%
25%% Splits client options into client, protocol, and transport options
26%%
27%% split_options([Options...]) -> {ProtocolOptions, TransportOptions}
28%%
29split_options(Options) ->
30 split_options(Options, [], []).
31
32split_options([], ProtoIn, TransIn) ->
33 {ProtoIn, TransIn};
34
35split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn)
36 when OptKey =:= strict_read;
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090037 OptKey =:= strict_write;
38 OptKey =:= protocol ->
David Reiss3f660a42010-08-30 22:05:29 +000039 split_options(Rest, [Opt | ProtoIn], TransIn);
40
41split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn)
42 when OptKey =:= framed;
43 OptKey =:= connect_timeout;
Jens Geyer0b098872015-05-20 21:43:33 +020044 OptKey =:= recv_timeout;
David Robakowskia7d6a972013-08-07 05:51:00 +020045 OptKey =:= sockopts;
46 OptKey =:= ssltransport;
47 OptKey =:= ssloptions->
David Reiss3f660a42010-08-30 22:05:29 +000048 split_options(Rest, ProtoIn, [Opt | TransIn]).
49
50
51%% Client constructor for the common-case of socket transports
David Reiss3f660a42010-08-30 22:05:29 +000052new(Host, Port, Service, Options)
53 when is_integer(Port), is_atom(Service), is_list(Options) ->
David Robakowskia7d6a972013-08-07 05:51:00 +020054 {ProtoOpts, TransOpts0} = split_options(Options),
55
56 {TransportModule, TransOpts2} = case lists:keytake(ssltransport, 1, TransOpts0) of
57 {value, {_, true}, TransOpts1} -> {thrift_sslsocket_transport, TransOpts1};
58 false -> {thrift_socket_transport, TransOpts0}
59 end,
David Reiss3f660a42010-08-30 22:05:29 +000060
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090061 {ProtocolModule, ProtoOpts1} = case lists:keytake(protocol, 1, ProtoOpts) of
62 {value, {_, compact}, Opts} -> {thrift_compact_protocol, Opts};
63 {value, {_, json}, Opts} -> {thrift_json_protocol, Opts};
64 {value, {_, binary}, Opts} -> {thrift_binary_protocol, Opts};
65 false -> {thrift_binary_protocol, ProtoOpts}
66 end,
David Reiss3f660a42010-08-30 22:05:29 +000067 {ok, TransportFactory} =
David Robakowskia7d6a972013-08-07 05:51:00 +020068 TransportModule:new_transport_factory(Host, Port, TransOpts2),
David Reiss3f660a42010-08-30 22:05:29 +000069
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090070 {ok, ProtocolFactory} = ProtocolModule:new_protocol_factory(
71 TransportFactory, ProtoOpts1),
David Reiss3f660a42010-08-30 22:05:29 +000072
Jens Geyera6b328f2014-03-18 23:51:23 +020073 case ProtocolFactory() of
74 {ok, Protocol} ->
75 thrift_client:new(Protocol, Service);
76 {error, Error} ->
77 {error, Error}
78 end.