Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 4d2e56c

Browse files
committed
add back the ability to pass options as a final param
1 parent 3303512 commit 4d2e56c

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The package can be installed by adding `podium_ex` to your list of dependencies
99
```elixir
1010
def deps do
1111
[
12-
{:podium_ex, "~> 0.6"}
12+
{:podium_ex, "~> 0.7"}
1313
]
1414
end
1515
```
@@ -25,7 +25,7 @@ You will need to provide the following configuration options (usually in `config
2525

2626
You can optionally also include the following:
2727

28-
`:base_url` - The root URL onto which all paths are appended. Defaults to `https://api.podium.com/api/v2`
28+
- `:base_url` - The root URL onto which all paths are appended. Defaults to `https://api.podium.com/api/v2`
2929

3030
### Example Configuration
3131

lib/podium.ex

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
defmodule Podium do
22
@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
44
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
511
"""
612

713
alias Podium.{
@@ -19,102 +25,119 @@ defmodule Podium do
1925
@doc """
2026
Create a conversation item. Read about conversation items [here](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
2127
"""
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+
2432
conversation_item =
2533
item
2634
|> remove_nils()
2735
|> format_actions()
2836
|> inject_application_uid()
2937
|> inject_source_type()
3038

31-
conversation_item =
39+
body =
3240
Caramelize.camelize(%{
3341
conversation_item: conversation_item
3442
})
3543

36-
API.post("/conversation_items", Caramelize.camelize(conversation_item))
44+
API.post("/conversation_items", body, headers, opts)
3745
end
3846

3947
@doc """
4048
Update a [conversation item](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
4149
"""
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+
4454
conversation_item =
4555
item
4656
|> remove_nils()
4757
|> inject_application_uid()
4858
|> inject_source_type()
4959

50-
conversation_item = %{
51-
conversation_item: conversation_item
52-
}
60+
body =
61+
Caramelize.camelize(%{
62+
conversation_item: conversation_item
63+
})
5364

54-
API.put("/conversation_items", Caramelize.camelize(conversation_item))
65+
API.put("/conversation_items", body, headers, opts)
5566
end
5667

5768
@doc """
5869
Delete a [conversation item](https://hexdocs.pm/podium_ex/Podium.ConversationItem.html#content).
5970
"""
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, [])
6274
application_uid = Application.get_env(:podium_ex, :application_uid)
6375

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)
6579
end
6680

6781
@doc """
6882
Create a message. Read about messages [here](https://hexdocs.pm/podium_ex/Podium.Message.html#content).
6983
"""
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+
7288
message =
7389
msg
7490
|> remove_nils()
7591
|> inject_application_uid()
7692

77-
message = %{
78-
conversation_item: message
79-
}
93+
body =
94+
Caramelize.camelize(%{
95+
conversation_item: message
96+
})
8097

81-
API.post("/messages", Caramelize.camelize(message))
98+
API.post("/messages", body, headers, opts)
8299
end
83100

84101
@doc """
85102
Create an interaction. Read about interactions [here](https://hexdocs.pm/podium_ex/Podium.Interaction.html#content).
86103
"""
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+
89108
interaction =
90109
interaction
91110
|> remove_nils()
92111
|> inject_application_uid()
93112
|> inject_source_type()
94113

95-
interaction = %{
96-
interaction: interaction
97-
}
114+
body =
115+
Caramelize.camelize(%{
116+
interaction: interaction
117+
})
98118

99-
API.post("/interactions", Caramelize.camelize(interaction))
119+
API.post("/interactions", body, headers, opts)
100120
end
101121

102122
@doc """
103123
Update an [interaction](https://hexdocs.pm/podium_ex/Podium.Interaction.html#content).
104124
"""
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+
107129
interaction =
108130
interaction
109131
|> remove_nils()
110132
|> inject_application_uid()
111133
|> inject_source_type()
112134

113-
interaction = %{
114-
interaction: interaction
115-
}
135+
body =
136+
Caramelize.camelize(%{
137+
interaction: interaction
138+
})
116139

117-
API.put("/interactions/#{uid}", Caramelize.camelize(interaction))
140+
API.put("/interactions/#{uid}", body, headers, opts)
118141
end
119142

120143
@doc """
@@ -128,10 +151,12 @@ defmodule Podium do
128151
iex> get_organization("non-existent UID")
129152
nil
130153
"""
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+
133158
with {:ok, %HTTPoison.Response{body: body, status_code: 200}} <-
134-
API.get("/organizations/#{uid}"),
159+
API.get("/organizations/#{uid}", headers, opts),
135160
{:ok, %{"data" => %{"organization" => org}}} <- Jason.decode(body),
136161
%{"uid" => uid, "locations" => locations, "businessName" => name} <- org do
137162
%Organization{

lib/podium/api.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ defmodule Podium.API do
88

99
use HTTPoison.Base
1010

11-
@spec process_url(String.t()) :: String.t()
11+
@impl HTTPoison.Base
1212
def process_url(path) do
1313
base_url() <> path
1414
end
1515

16-
@spec process_request_headers(list()) :: list()
16+
@impl HTTPoison.Base
1717
def process_request_headers(headers) do
1818
[
1919
{"Accept", "application/json"},
@@ -23,17 +23,28 @@ defmodule Podium.API do
2323
] ++ headers
2424
end
2525

26-
@spec process_request_body(map()) :: String.t()
26+
@impl HTTPoison.Base
2727
def process_request_body(body) do
2828
Jason.encode!(body)
2929
end
3030

31+
@impl HTTPoison.Base
32+
def process_request_options(opts) do
33+
opts ++ [timeout: timeout(), recv_timeout: recv_timeout()]
34+
end
35+
3136
@spec api_key() :: String.t()
3237
defp api_key, do: Application.get_env(:podium_ex, :api_key, "")
3338

3439
@spec base_url() :: String.t()
3540
defp base_url, do: Application.get_env(:podium_ex, :base_url, @default_base_url)
3641

42+
@spec timeout :: integer()
43+
defp timeout, do: Application.get_env(:podium_ex, :timeout, 15_000)
44+
45+
@spec recv_timeout :: integer()
46+
defp recv_timeout, do: Application.get_env(:podium_ex, :recv_timeout, 15_000)
47+
3748
@spec application_uid() :: String.t()
3849
defp application_uid, do: Application.get_env(:podium_ex, :application_uid, "")
3950
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule PodiumEx.MixProject do
55
[
66
app: :podium_ex,
77
description: "HTTP client for interfacing with the Podium API",
8-
version: "0.6.0",
8+
version: "0.7.0",
99
elixir: "~> 1.7",
1010
start_permanent: Mix.env() == :prod,
1111
deps: deps(),

0 commit comments

Comments
 (0)