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/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(map()) -> #amqp_params_network{}. |
57 |
|
network_params(#{host := Host, port := Port, username := UserName, password := Password}) -> |
58 |
2 |
#amqp_params_network{host = Host, port = Port, username = UserName, password = Password}. |
59 |
|
|
60 |
|
-spec exchange_declare(Exchange :: binary(), Type :: binary()) -> method(). |
61 |
|
exchange_declare(Exchange, Type) -> |
62 |
12 |
#'exchange.declare'{exchange = Exchange, type = Type}. |
63 |
|
|
64 |
|
-spec exchange_declare_ok() -> method(). |
65 |
|
exchange_declare_ok() -> |
66 |
12 |
#'exchange.declare_ok'{}. |
67 |
|
|
68 |
|
-spec exchange_delete(Exchange :: binary()) -> method(). |
69 |
|
exchange_delete(Exchange) -> |
70 |
:-( |
#'exchange.delete'{exchange = Exchange}. |
71 |
|
|
72 |
|
-spec basic_publish(Exchange :: binary(), RoutingKey :: binary()) -> method(). |
73 |
|
basic_publish(Exchange, RoutingKey) -> |
74 |
64 |
#'basic.publish'{exchange = Exchange, routing_key = RoutingKey}. |
75 |
|
|
76 |
|
-spec confirm_select() -> method(). |
77 |
|
confirm_select() -> |
78 |
:-( |
#'confirm.select'{}. |
79 |
|
|
80 |
|
-spec confirm_select_ok() -> method(). |
81 |
|
confirm_select_ok() -> |
82 |
:-( |
#'confirm.select_ok'{}. |
83 |
|
|
84 |
|
-spec message(Payload :: binary()) -> message(). |
85 |
|
message(Payload) -> |
86 |
64 |
#amqp_msg{payload = Payload}. |