Skip to content

Commit

Permalink
Adapters layer is just a proxy so all logic is on handlers layer (mer…
Browse files Browse the repository at this point in the history
  • Loading branch information
lonamiaec authored Feb 2, 2020
1 parent a8c180b commit 749cb5e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 50 deletions.
16 changes: 3 additions & 13 deletions lib/postoffice/adapters/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@ defmodule Postoffice.Adapters.Http do
Logger.info("Dispatching Http message to #{endpoint}")
%{payload: payload} = message

case HTTPoison.post(endpoint, Poison.encode!(payload), [
{"content-type", "application/json"}
]) do
{:ok, %HTTPoison.Response{status_code: status_code, body: _body}}
when status_code in 200..299 ->
{:ok, message}

{:ok, response} ->
{:error, response.status_code}

{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}
end
HTTPoison.post(endpoint, Poison.encode!(payload), [
{"content-type", "application/json"}
])
end
end
44 changes: 16 additions & 28 deletions lib/postoffice/adapters/pubsub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,24 @@ defmodule Postoffice.Adapters.Pubsub do
Logger.info("Publishing PubSub message to #{endpoint}")
%{payload: payload, attributes: _attributes} = message
# Authenticate
case Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform") do
{:ok, token} ->
Logger.info("successfully generated token for pubsub")
conn = GoogleApi.PubSub.V1.Connection.new(token.token)
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
Logger.info("successfully generated token for pubsub")
conn = GoogleApi.PubSub.V1.Connection.new(token.token)

request = %GoogleApi.PubSub.V1.Model.PublishRequest{
messages: [
%GoogleApi.PubSub.V1.Model.PubsubMessage{
data: Base.encode64(Poison.encode!(payload))
}
]
request = %GoogleApi.PubSub.V1.Model.PublishRequest{
messages: [
%GoogleApi.PubSub.V1.Model.PubsubMessage{
data: Base.encode64(Poison.encode!(payload))
}
]
}

# Make the API request.
case GoogleApi.PubSub.V1.Api.Projects.pubsub_projects_topics_publish(
conn,
Application.get_env(:postoffice, :pubsub_project_name),
endpoint,
body: request
) do
{:ok, _response} ->
{:ok, message}

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

{:error, error} ->
Logger.info("Could not generate token for pubsub #{error.reason}")
{:error, error.reason}
end
# Make the API request.
GoogleApi.PubSub.V1.Api.Projects.pubsub_projects_topics_publish(
conn,
Application.get_env(:postoffice, :pubsub_project_name),
endpoint,
body: request
)
end
end
17 changes: 14 additions & 3 deletions lib/postoffice/handlers/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Postoffice.Handlers.Http do

def run(publisher_endpoint, publisher_id, message) do
case impl().publish(publisher_endpoint, message) do
{:ok, message = %Message{}} ->
{:ok, %HTTPoison.Response{status_code: status_code, body: _body}} when status_code in 200..299 ->
Logger.info("Succesfully sent http message to #{publisher_endpoint}")

{:ok, _} =
Expand All @@ -18,15 +18,26 @@ defmodule Postoffice.Handlers.Http do

{:ok, :sent}

{:error, status} ->
Logger.info("Error trying to process message from HttpConsumer #{status}")
{:ok, response} ->
Logger.info("Error trying to process message from HttpConsumer #{response.status_code}")

Messaging.create_publisher_failure(%{
publisher_id: publisher_id,
message_id: message.id
})

{:error, :nosent}

{:error, %HTTPoison.Error{reason: reason}} ->
Logger.info("Error trying to process message from HttpConsumer #{reason}")

Messaging.create_publisher_failure(%{
publisher_id: publisher_id,
message_id: message.id
})

{:error, :nosent}

end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/postoffice/handlers/pubsub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Postoffice.Handlers.Pubsub do
{:ok, :sent}

{:error, error} ->
Logger.info("Error trying to process message from HttpConsumer #{error}")
Logger.info("Error trying to process message from PubsubConsumer #{error}")

Messaging.create_publisher_failure(%{
publisher_id: publisher_id,
Expand Down
10 changes: 5 additions & 5 deletions test/postoffice/handlers/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Postoffice.Handlers.HttpTest do
{:ok, message} = Messaging.create_message(topic, @valid_message_attrs)

expect(HttpMock, :publish, fn "http://fake.endpoint", ^message ->
{:error, 404}
{:ok, %HTTPoison.Response{status_code: 404}}
end)

Http.run(publisher.endpoint, publisher.id, message)
Expand All @@ -56,7 +56,7 @@ defmodule Postoffice.Handlers.HttpTest do
{:ok, message} = Messaging.create_message(topic, @valid_message_attrs)

expect(HttpMock, :publish, fn "http://fake.endpoint", ^message ->
{:ok, message}
{:ok, %HTTPoison.Response{status_code: 201}}
end)

Http.run(publisher.endpoint, publisher.id, message)
Expand All @@ -72,15 +72,15 @@ defmodule Postoffice.Handlers.HttpTest do
{:ok, message} = Messaging.create_message(topic, @valid_message_attrs)

expect(HttpMock, :publish, fn "http://fake.endpoint", ^message ->
{:error, %HTTPoison.Error{reason: "test"}}
{:error, %HTTPoison.Error{reason: "test error reason"}}
end)

Http.run(publisher.endpoint, publisher.id, message)
message_failure = List.first(Messaging.list_publisher_failures(publisher.id))
assert message_failure.message_id == message.id
end

test "message_failure is created for publisher if response is :ok but response_code != 200" do
test "message_failure is created for publisher if response is :ok but response_code out of 200 range" do
{:ok, topic} = Messaging.create_topic(@valid_topic_attrs)

{:ok, publisher} =
Expand All @@ -89,7 +89,7 @@ defmodule Postoffice.Handlers.HttpTest do
{:ok, message} = Messaging.create_message(topic, @valid_message_attrs)

expect(HttpMock, :publish, fn "http://fake.endpoint", ^message ->
{:error, %HTTPoison.Response{status_code: 201}}
{:ok, %HTTPoison.Response{status_code: 300}}
end)

Http.run(publisher.endpoint, publisher.id, message)
Expand Down

0 comments on commit 749cb5e

Please sign in to comment.