Skip to content

Commit

Permalink
Merge pull request #45 from beamkenya/develop
Browse files Browse the repository at this point in the history
Promote to master
  • Loading branch information
kamalogudah authored Jun 29, 2020
2 parents 0bbfdad + 97faee8 commit 0db553b
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 182 deletions.
66 changes: 33 additions & 33 deletions lib/at_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ defmodule AtEx do
## Examples
iex> AtEx.send_airtime(%{recipients: [%{phone_number: "+254721978097", amount: "KES 50"}]})
{:ok,
%{
"errorMessage" => "None",
"numSent" => 1,
"responses" => [
%{
"amount" => "KES 50.0000",
"discount" => "KES 2.0000",
"errorMessage" => "None",
"phoneNumber" => "+254721978097",
"requestId" => "ATQid_630557e624c70f2b0d2c5e90ebc282bb",
"status" => "Sent"
}
],
iex> AtEx.send_airtime(%{recipients: [%{phone_number: "+254721978097", amount: "KES 50"}]})
{:ok,
%{
"errorMessage" => "None",
"numSent" => 1,
"responses" => [
%{
"amount" => "KES 50.0000",
"discount" => "KES 2.0000",
"errorMessage" => "None",
"phoneNumber" => "+254721978097",
"requestId" => "ATQid_630557e624c70f2b0d2c5e90ebc282bb",
"status" => "Sent"
}
],
"totalAmount" => "ZAR 7.0277",
"totalDiscount" => "ZAR 0.2811"
}}
Expand All @@ -76,28 +76,28 @@ defmodule AtEx do
sent
## Parameters
- map: a map containing a `to` and `message` key optionally it may also contain `from`, bulk_sms, enqueue, key_word
link_id and retry_hours keys, see the docs at https://build.at-labs.io/docs/sms%2Fsending for how to use these keys
- map: a map containing a `to` and `message` key optionally it may also contain `from`, bulk_sms, enqueue, key_word
link_id and retry_hours keys, see the docs at https://build.at-labs.io/docs/sms%2Fsending for how to use these keys
## Examples
iex> AtEx.send_sms(%{to: "+254721978097", message: "Howdy"})
{:ok,
iex> AtEx.send_sms(%{to: "+254721978097", message: "Howdy"})
{:ok,
%{
"SMSMessageData" => %{
"Message" => "Sent to 1/1 Total Cost: ZAR 0.1124",
"Recipients" => [
%{
"SMSMessageData" => %{
"Message" => "Sent to 1/1 Total Cost: ZAR 0.1124",
"Recipients" => [
%{
"cost" => "KES 0.8000",
"messageId" => "ATXid_96e52a761a82c1bad58e885109224aad",
"number" => "+254721978097",
"status" => "Success",
"statusCode" => 101
}
]
"cost" => "KES 0.8000",
"messageId" => "ATXid_96e52a761a82c1bad58e885109224aad",
"number" => "+254721978097",
"status" => "Success",
"statusCode" => 101
}
}}
]
}
}}
"""
defdelegate send_sms(map), to: Sms
defdelegate send_sms(map), to: Sms.Bulk

@doc """
Expand All @@ -120,5 +120,5 @@ defmodule AtEx do
"""

defdelegate fetch_sms(map), to: Sms
defdelegate fetch_sms(map), to: Sms.Bulk
end
58 changes: 58 additions & 0 deletions lib/at_ex/gateway/Sms/bulk.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule AtEx.Gateway.Sms.Bulk do
@moduledoc """
This module holds the implementation for the HTTP Gateway that runs calls against the Africas Talking API
SMS endpoint, use it to POST and GET requests to the SMS endpoint
"""
use AtEx.Gateway.Base, url: "https://api.sandbox.africastalking.com/version1"

@doc """
This function builds and runs a post request to send an SMS via the Africa's talking SMS endpoint, this
function accepts a map of parameters that should always contain the `to` address and the `message` to be
sent
## Parameters
attrs: - a map containing a `to` and `message` key optionally it may also contain `from`, bulk_sms, enqueue, key_word
link_id and retry_hours keys, see the docs at https://build.at-labs.io/docs/sms%2Fsending for how to use these keys
"""
@spec send_sms(map()) :: {:ok, term()} | {:error, term()}
def send_sms(attrs) do
username = Application.get_env(:at_ex, :username)

params =
attrs
|> Map.put(:username, username)

"/messaging"
|> post(params)
|> process_result()
end

@doc """
This function makes a get request to fetch an SMS via the Africa's talking SMS endpoint, this
function accepts a map of parameters that optionally accepts `lastReceivedId` of the message.
sent
## Parameters
attrs: - an empty map or a map containing optionally `lastReceivedId` of the message to be fetched, see the docs at https://build.at-labs.io/docs/sms%2Ffetch_messages for how to use these keys
"""

@spec fetch_sms(map()) :: {:error, any()} | {:ok, any()}
def fetch_sms(attrs) do
username = Application.get_env(:at_ex, :username)

params =
attrs
|> Map.put(:username, username)
|> Map.to_list()

with {:ok, %{status: 200} = res} <- get("/messaging", query: params) do
{:ok, Jason.decode!(res.body)}
else
{:ok, val} ->
{:error, %{status: val.status, message: val.body}}

{:error, message} ->
{:error, message}
end
end
end
108 changes: 25 additions & 83 deletions lib/at_ex/gateway/sms.ex → lib/at_ex/gateway/Sms/premium.ex
Original file line number Diff line number Diff line change
@@ -1,72 +1,10 @@
defmodule AtEx.Gateway.Sms do
@moduledoc """
This module holds the implementation for the HTTP Gateway that runs calls against the Africas Talking API
SMS endpoint, use it to POST and GET requests to the SMS endpoint
"""
defmodule AtEx.Gateway.Sms.PremiumSubscriptions do
use AtEx.Gateway.Base, url: "https://api.sandbox.africastalking.com/version1"

@doc """
This function builds and runs a post request to send an SMS via the Africa's talking SMS endpoint, this
function accepts a map of parameters that should always contain the `to` address and the `message` to be
sent
## Parameters
attrs: - a map containing a `to` and `message` key optionally it may also contain `from`, bulk_sms, enqueue, key_word
link_id and retry_hours keys, see the docs at https://build.at-labs.io/docs/sms%2Fsending for how to use these keys
"""
@spec send_sms(map()) :: {:ok, term()} | {:error, term()}
def send_sms(attrs) do
username = Application.get_env(:at_ex, :username)

params =
attrs
|> Map.put(:username, username)

with {:ok, %{status: 201} = res} <- post("/messaging", params) do
{:ok, Jason.decode!(res.body)}
else
{:ok, val} ->
{:error, %{status: val.status, message: val.body}}

{:error, message} ->
{:error, message}
end
end

@doc """
This function makes a get request to fetch an SMS via the Africa's talking SMS endpoint, this
function accepts an map of parameters that optionally accepts `lastReceivedId` of the message.
sent
## Parameters
attrs: - an empty map or a map containing optionally `lastReceivedId` of the message to be fetched, see the docs at https://build.at-labs.io/docs/sms%2Ffetch_messages for how to use these keys
"""

@spec fetch_sms(map()) :: {:error, any()} | {:ok, any()}
def fetch_sms(attrs) do
username = Application.get_env(:at_ex, :username)

params =
attrs
|> Map.put(:username, username)
|> Map.to_list()

with {:ok, %{status: 200} = res} <- get("/messaging", query: params) do
{:ok, Jason.decode!(res.body)}
else
{:ok, val} ->
{:error, %{status: val.status, message: val.body}}

{:error, message} ->
{:error, message}
end
end

# Checkout token endpoints
@live_token_url "https://api.africastalking.com/checkout/token"
@sandbox_token_url "https://api.sandbox.africastalking.com/checkout/token"

# Using this system for delivery of which URL to use (sandbox or live)
# Using this system for delivery of which URL to use (sandbox or live)
# determined by whether we ar in production or development or test
# Selection of the live URL can be forced by setting an environment
# variable FORCE_TOKEN_LIVE=YES
Expand All @@ -82,7 +20,7 @@ defmodule AtEx.Gateway.Sms do
This function fetches the checkout token from the checkout token endpoint
THIS IS DIFFERENT THAN THE SMS ENDPOINT!!
phone_number: - a string representing a valid phone number in EL64
phone_number: - a string representing a valid phone number in EL64
(+<country code><phone number> with all non digit characters removed)
[NOTE: function does not verify the phone number is in any way correct
before sending to the endpoint.]
Expand Down Expand Up @@ -136,22 +74,22 @@ defmodule AtEx.Gateway.Sms do
- `phoneNumber` - phone number to be subscribed
## Example
iex> AtEx.Gateway.Sms.create_subscription(%{
...> shortCode: "1234",
...> keyword: "keyword",
...> phoneNumber: "+2541231111"
...> })
iex> AtEx.Gateway.Sms.PremiumSubscriptions.create_subscription(%{phoneNumber: "+2541231111"})
{:ok, result}
"""
@spec create_subscription(map()) :: {:error, any()} | {:ok, any()}
def create_subscription(%{phoneNumber: phone_number} = attrs) do
username = Application.get_env(:at_ex, :username)
shortcode = Application.get_env(:at_ex, :short_code)
keyword = Application.get_env(:at_ex, :keyword)

case generate_checkout_token(phone_number) do
{:ok, token} ->
params =
attrs
|> Map.put(:username, username)
|> Map.put(:shortCode, shortcode)
|> Map.put(:keyword, keyword)
|> Map.put(:checkoutToken, token)

with {:ok, %{status: 201} = res} <- post("/subscription/create", params) do
Expand Down Expand Up @@ -181,19 +119,23 @@ defmodule AtEx.Gateway.Sms do
- `lastReceivedId` - (optional) ID of the subscription you believe to be your last. Set it to 0 to for the first time.
## Example
iex> AtEx.Gateway.Sms.create_subscription(%{
...> shortCode: "1234",
...> keyword: "keyword",
...> })
{:ok, result}
iex> AtEx.Gateway.Sms.create_subscription(%{
...> shortCode: "1234",
...> keyword: "keyword",
...> })
{:ok, result}
"""
@spec fetch_subscriptions(map()) :: {:error, any()} | {:ok, any()}
def fetch_subscriptions(attrs) do
@spec fetch_subscriptions() :: {:error, any()} | {:ok, any()}
def fetch_subscriptions() do
username = Application.get_env(:at_ex, :username)
shortcode = Application.get_env(:at_ex, :short_code)
keyword = Application.get_env(:at_ex, :keyword)

params =
attrs
%{}
|> Map.put(:username, username)
|> Map.put(:shortCode, shortcode)
|> Map.put(:keyword, keyword)

with {:ok, %{status: 200} = res} <- get("/subscription", query: params) do
{:ok, Jason.decode!(res.body)}
Expand All @@ -217,20 +159,20 @@ defmodule AtEx.Gateway.Sms do
- `phoneNumber` - phone number to be unsubscribed
## Example
iex> AtEx.Gateway.Sms.delete_subscription(%{
...> shortCode: "1234",
...> keyword: "keyword",
...> phoneNumber: "+2541231111"
...> })
iex> AtEx.Gateway.Sms.delete_subscription(%{ phoneNumber: "+2541231111"})
{:ok, %{"description" => "Succeeded", "status" => "Success"}}
"""
@spec delete_subscription(map()) :: {:error, any()} | {:ok, any()}
def delete_subscription(attrs) do
username = Application.get_env(:at_ex, :username)
shortcode = Application.get_env(:at_ex, :short_code)
keyword = Application.get_env(:at_ex, :keyword)

params =
attrs
|> Map.put(:username, username)
|> Map.put(:shortCode, shortcode)
|> Map.put(:keyword, keyword)

with {:ok, %{status: 201} = res} <- post("/subscription/delete", params) do
{:ok, Jason.decode!(res.body)}
Expand Down
22 changes: 15 additions & 7 deletions lib/at_ex/gateway/base_http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ defmodule AtEx.Gateway.Base do
@doc """
Process results from calling the gateway
"""
def process_result(result) do
with {:ok, res} <- Jason.decode(result) do
{:ok, res}
else
{:error, val} ->
{:error, val}
end

def process_result({:ok, %{status: 200} = res}) do
Jason.decode(res.body)
end

def process_result({:ok, %{status: 201} = res}) do
Jason.decode(res.body)
end

def process_result({:ok, result}) do
{:error, %{status: result.status, message: result.body}}
end

def process_result({:error, result}) do
{:error, result}
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/at_ex/gateway/voice.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
defmodule AtEx.Gateway.Voice do
end
1 change: 0 additions & 1 deletion lib/at_ex/sms/sms.ex

This file was deleted.

Loading

0 comments on commit 0db553b

Please sign in to comment.