1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2018 Erlang Solutions Ltd. |
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 |
|
%% @doc |
17 |
|
%% This module provides an API for dealing with AMQP operations. |
18 |
|
%% @end |
19 |
|
%%============================================================================== |
20 |
|
|
21 |
|
-module(mongoose_amqp). |
22 |
|
-author('kacper.mentel@erlang-solutions.com'). |
23 |
|
|
24 |
|
-include_lib("amqp_client/include/amqp_client.hrl"). |
25 |
|
|
26 |
|
-export([network_params/0, network_params/1, exchange_declare/2, |
27 |
|
exchange_declare_ok/0, exchange_delete/1, basic_publish/2, |
28 |
|
confirm_select/0, confirm_select_ok/0, message/1]). |
29 |
|
|
30 |
|
-ignore_xref([exchange_delete/1, network_params/0]). |
31 |
|
|
32 |
|
-export_type([network_params/0, method/0, message/0]). |
33 |
|
|
34 |
|
|
35 |
|
%%%=================================================================== |
36 |
|
%%% Types and definitions |
37 |
|
%%%=================================================================== |
38 |
|
|
39 |
|
-type network_params() :: #amqp_params_network{}. |
40 |
|
|
41 |
|
-type method() :: #'exchange.declare'{} |
42 |
|
| #'exchange.declare_ok'{} |
43 |
|
| #'exchange.delete'{} |
44 |
|
| #'basic.publish'{} |
45 |
|
| #'confirm.select'{} |
46 |
|
| #'confirm.select_ok'{}. |
47 |
|
|
48 |
|
-type message() :: #amqp_msg{}. |
49 |
|
|
50 |
|
-define(DEFAULT_PORT, 5672). |
51 |
|
|
52 |
|
%%%=================================================================== |
53 |
|
%%% API |
54 |
|
%%%=================================================================== |
55 |
|
|
56 |
|
-spec network_params() -> network_params(). |
57 |
|
network_params() -> |
58 |
:-( |
network_params([]). |
59 |
|
|
60 |
|
-spec network_params(proplists:proplist()) -> #amqp_params_network{}. |
61 |
|
network_params(Opts) -> |
62 |
1 |
network_params(Opts, #amqp_params_network{}). |
63 |
|
|
64 |
|
-spec exchange_declare(Exchange :: binary(), Type :: binary()) -> method(). |
65 |
|
exchange_declare(Exchange, Type) -> |
66 |
:-( |
#'exchange.declare'{exchange = Exchange, type = Type}. |
67 |
|
|
68 |
|
-spec exchange_declare_ok() -> method(). |
69 |
|
exchange_declare_ok() -> |
70 |
:-( |
#'exchange.declare_ok'{}. |
71 |
|
|
72 |
|
-spec exchange_delete(Exchange :: binary()) -> method(). |
73 |
|
exchange_delete(Exchange) -> |
74 |
:-( |
#'exchange.delete'{exchange = Exchange}. |
75 |
|
|
76 |
|
-spec basic_publish(Exchange :: binary(), RoutingKey :: binary()) -> method(). |
77 |
|
basic_publish(Exchange, RoutingKey) -> |
78 |
:-( |
#'basic.publish'{exchange = Exchange, routing_key = RoutingKey}. |
79 |
|
|
80 |
|
-spec confirm_select() -> method(). |
81 |
|
confirm_select() -> |
82 |
:-( |
#'confirm.select'{}. |
83 |
|
|
84 |
|
-spec confirm_select_ok() -> method(). |
85 |
|
confirm_select_ok() -> |
86 |
:-( |
#'confirm.select_ok'{}. |
87 |
|
|
88 |
|
-spec message(Payload :: binary()) -> message(). |
89 |
|
message(Payload) -> |
90 |
:-( |
#amqp_msg{payload = Payload}. |
91 |
|
|
92 |
|
%%%=================================================================== |
93 |
|
%%% Helpers |
94 |
|
%%%=================================================================== |
95 |
|
|
96 |
|
network_params(Opts, #amqp_params_network{host = Host, username = UserName, |
97 |
|
password = Password}) -> |
98 |
1 |
#amqp_params_network{ |
99 |
|
host = proplists:get_value(host, Opts, Host), |
100 |
|
port = proplists:get_value(port, Opts, ?DEFAULT_PORT), |
101 |
|
username = proplists:get_value(username, Opts, UserName), |
102 |
|
password = proplists:get_value(password, Opts, Password)}. |