1
1
defmodule Podium do
2
2
@ moduledoc """
3
- Podium is a client for Podium's public API. It allows your to create new
3
+ Podium is a client for Podium's public API. It allows you to create new
4
4
messages and send them, among other things.
5
+
6
+ All functions accept a keyword list of options as an optional second
7
+ argument. Those options will be passed through to `HTTPoison.Request`. Other
8
+ options include the following:
9
+
10
+ - `:headers` - allows you to add headers to the defaults sent by the library
5
11
"""
6
12
7
13
alias Podium . {
@@ -19,102 +25,119 @@ defmodule Podium do
19
25
@ doc """
20
26
Create a conversation item. Read about conversation items [here](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
21
27
"""
22
- @ spec create_conversation_item ( ConversationItem . t ( ) ) :: ConversationItem . t ( )
23
- def create_conversation_item ( % ConversationItem { } = item ) do
28
+ @ spec create_conversation_item ( ConversationItem . t ( ) , Keyword . t ( ) ) :: ConversationItem . t ( )
29
+ def create_conversation_item ( % ConversationItem { } = item , opts \\ [ ] ) do
30
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
31
+
24
32
conversation_item =
25
33
item
26
34
|> remove_nils ( )
27
35
|> format_actions ( )
28
36
|> inject_application_uid ( )
29
37
|> inject_source_type ( )
30
38
31
- conversation_item =
39
+ body =
32
40
Caramelize . camelize ( % {
33
41
conversation_item: conversation_item
34
42
} )
35
43
36
- API . post ( "/conversation_items" , Caramelize . camelize ( conversation_item ) )
44
+ API . post ( "/conversation_items" , body , headers , opts )
37
45
end
38
46
39
47
@ doc """
40
48
Update a [conversation item](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
41
49
"""
42
- @ spec update_conversation_item ( ConversationItem . t ( ) ) :: ConversationItem . t ( )
43
- def update_conversation_item ( % ConversationItem { } = item ) do
50
+ @ spec update_conversation_item ( ConversationItem . t ( ) , Keyword . t ( ) ) :: ConversationItem . t ( )
51
+ def update_conversation_item ( % ConversationItem { } = item , opts \\ [ ] ) do
52
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
53
+
44
54
conversation_item =
45
55
item
46
56
|> remove_nils ( )
47
57
|> inject_application_uid ( )
48
58
|> inject_source_type ( )
49
59
50
- conversation_item = % {
51
- conversation_item: conversation_item
52
- }
60
+ body =
61
+ Caramelize . camelize ( % {
62
+ conversation_item: conversation_item
63
+ } )
53
64
54
- API . put ( "/conversation_items" , Caramelize . camelize ( conversation_item ) )
65
+ API . put ( "/conversation_items" , body , headers , opts )
55
66
end
56
67
57
68
@ doc """
58
69
Delete a [conversation item](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
59
70
"""
60
- @ spec delete_conversation_item ( String . t ( ) , String . t ( ) ) :: :ok
61
- def delete_conversation_item ( uid , organization_uid ) do
71
+ @ spec delete_conversation_item ( String . t ( ) , String . t ( ) , Keyword . t ( ) ) :: :ok
72
+ def delete_conversation_item ( uid , org_uid , opts \\ [ ] ) do
73
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
62
74
application_uid = Application . get_env ( :podium_ex , :application_uid )
63
75
64
- API . delete ( "/applications/#{ application_uid } /organizations/#{ organization_uid } /conversation_items/#{ uid } " )
76
+ path = "/applications/#{ application_uid } /organizations/#{ org_uid } /conversation_items/#{ uid } "
77
+
78
+ API . delete ( path , headers , opts )
65
79
end
66
80
67
81
@ doc """
68
82
Create a message. Read about messages [here](https://hexdocs.pm/podium_ex/Podium.Message.html#content).
69
83
"""
70
- @ spec create_message ( Message . t ( ) ) :: Message . t ( )
71
- def create_message ( % Message { } = msg ) do
84
+ @ spec create_message ( Message . t ( ) , Keyword . t ( ) ) :: Message . t ( )
85
+ def create_message ( % Message { } = msg , opts \\ [ ] ) do
86
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
87
+
72
88
message =
73
89
msg
74
90
|> remove_nils ( )
75
91
|> inject_application_uid ( )
76
92
77
- message = % {
78
- conversation_item: message
79
- }
93
+ body =
94
+ Caramelize . camelize ( % {
95
+ conversation_item: message
96
+ } )
80
97
81
- API . post ( "/messages" , Caramelize . camelize ( message ) )
98
+ API . post ( "/messages" , body , headers , opts )
82
99
end
83
100
84
101
@ doc """
85
102
Create an interaction. Read about interactions [here](https://hexdocs.pm/podium_ex/Podium.Interaction.html#content).
86
103
"""
87
- @ spec create_interaction ( Interaction . t ( ) ) :: Interaction . t ( )
88
- def create_interaction ( % Interaction { } = interaction ) do
104
+ @ spec create_interaction ( Interaction . t ( ) , Keyword . t ( ) ) :: Interaction . t ( )
105
+ def create_interaction ( % Interaction { } = interaction , opts \\ [ ] ) do
106
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
107
+
89
108
interaction =
90
109
interaction
91
110
|> remove_nils ( )
92
111
|> inject_application_uid ( )
93
112
|> inject_source_type ( )
94
113
95
- interaction = % {
96
- interaction: interaction
97
- }
114
+ body =
115
+ Caramelize . camelize ( % {
116
+ interaction: interaction
117
+ } )
98
118
99
- API . post ( "/interactions" , Caramelize . camelize ( interaction ) )
119
+ API . post ( "/interactions" , body , headers , opts )
100
120
end
101
121
102
122
@ doc """
103
123
Update an [interaction](https://hexdocs.pm/podium_ex/Podium.Interaction.html#content).
104
124
"""
105
- @ spec update_interaction ( Interaction . t ( ) ) :: Interaction . t ( )
106
- def update_interaction ( % Interaction { uid: uid } = interaction ) do
125
+ @ spec update_interaction ( Interaction . t ( ) , Keyword . t ( ) ) :: Interaction . t ( )
126
+ def update_interaction ( % Interaction { uid: uid } = interaction , opts \\ [ ] ) do
127
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
128
+
107
129
interaction =
108
130
interaction
109
131
|> remove_nils ( )
110
132
|> inject_application_uid ( )
111
133
|> inject_source_type ( )
112
134
113
- interaction = % {
114
- interaction: interaction
115
- }
135
+ body =
136
+ Caramelize . camelize ( % {
137
+ interaction: interaction
138
+ } )
116
139
117
- API . put ( "/interactions/#{ uid } " , Caramelize . camelize ( interaction ) )
140
+ API . put ( "/interactions/#{ uid } " , body , headers , opts )
118
141
end
119
142
120
143
@ doc """
@@ -128,10 +151,12 @@ defmodule Podium do
128
151
iex> get_organization("non-existent UID")
129
152
nil
130
153
"""
131
- @ spec get_organization ( String . t ( ) ) :: Organization . t ( ) | nil
132
- def get_organization ( uid ) do
154
+ @ spec get_organization ( String . t ( ) , Keyword . t ( ) ) :: Organization . t ( ) | nil
155
+ def get_organization ( uid , opts \\ [ ] ) do
156
+ { headers , opts } = Keyword . pop ( opts , :headers , [ ] )
157
+
133
158
with { :ok , % HTTPoison.Response { body: body , status_code: 200 } } <-
134
- API . get ( "/organizations/#{ uid } " ) ,
159
+ API . get ( "/organizations/#{ uid } " , headers , opts ) ,
135
160
{ :ok , % { "data" => % { "organization" => org } } } <- Jason . decode ( body ) ,
136
161
% { "uid" => uid , "locations" => locations , "businessName" => name } <- org do
137
162
% Organization {
0 commit comments