David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 1 | %% |
| 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 | %% |
| 29 | split_options(Options) -> |
| 30 | split_options(Options, [], []). |
| 31 | |
| 32 | split_options([], ProtoIn, TransIn) -> |
| 33 | {ProtoIn, TransIn}; |
| 34 | |
| 35 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 36 | when OptKey =:= strict_read; |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 37 | OptKey =:= strict_write; |
| 38 | OptKey =:= protocol -> |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 39 | split_options(Rest, [Opt | ProtoIn], TransIn); |
| 40 | |
| 41 | split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn) |
| 42 | when OptKey =:= framed; |
| 43 | OptKey =:= connect_timeout; |
Jens Geyer | 0b09887 | 2015-05-20 21:43:33 +0200 | [diff] [blame] | 44 | OptKey =:= recv_timeout; |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 45 | OptKey =:= sockopts; |
| 46 | OptKey =:= ssltransport; |
| 47 | OptKey =:= ssloptions-> |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 48 | split_options(Rest, ProtoIn, [Opt | TransIn]). |
| 49 | |
| 50 | |
| 51 | %% Client constructor for the common-case of socket transports |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 52 | new(Host, Port, Service, Options) |
| 53 | when is_integer(Port), is_atom(Service), is_list(Options) -> |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 54 | {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 Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 60 | |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 61 | {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 Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 67 | {ok, TransportFactory} = |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 68 | TransportModule:new_transport_factory(Host, Port, TransOpts2), |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 70 | {ok, ProtocolFactory} = ProtocolModule:new_protocol_factory( |
| 71 | TransportFactory, ProtoOpts1), |
David Reiss | 3f660a4 | 2010-08-30 22:05:29 +0000 | [diff] [blame] | 72 | |
Jens Geyer | a6b328f | 2014-03-18 23:51:23 +0200 | [diff] [blame] | 73 | case ProtocolFactory() of |
| 74 | {ok, Protocol} -> |
| 75 | thrift_client:new(Protocol, Service); |
| 76 | {error, Error} -> |
| 77 | {error, Error} |
| 78 | end. |