diff --git a/lib/postoffice/adapters/http.ex b/lib/postoffice/adapters/http.ex index ad5dcb66..10f05139 100644 --- a/lib/postoffice/adapters/http.ex +++ b/lib/postoffice/adapters/http.ex @@ -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 diff --git a/lib/postoffice/adapters/pubsub.ex b/lib/postoffice/adapters/pubsub.ex index e19cf2b3..46ef2f7c 100644 --- a/lib/postoffice/adapters/pubsub.ex +++ b/lib/postoffice/adapters/pubsub.ex @@ -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 diff --git a/lib/postoffice/handlers/http.ex b/lib/postoffice/handlers/http.ex index 234b7801..0b3e0756 100644 --- a/lib/postoffice/handlers/http.ex +++ b/lib/postoffice/handlers/http.ex @@ -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, _} = @@ -18,8 +18,8 @@ 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, @@ -27,6 +27,17 @@ defmodule Postoffice.Handlers.Http do }) {: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 diff --git a/lib/postoffice/handlers/pubsub.ex b/lib/postoffice/handlers/pubsub.ex index 695b87d5..6549c610 100644 --- a/lib/postoffice/handlers/pubsub.ex +++ b/lib/postoffice/handlers/pubsub.ex @@ -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, diff --git a/test/postoffice/handlers/http_test.exs b/test/postoffice/handlers/http_test.exs index a039827f..603fe1c7 100644 --- a/test/postoffice/handlers/http_test.exs +++ b/test/postoffice/handlers/http_test.exs @@ -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) @@ -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) @@ -72,7 +72,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.Error{reason: "test"}} + {:error, %HTTPoison.Error{reason: "test error reason"}} end) Http.run(publisher.endpoint, publisher.id, message) @@ -80,7 +80,7 @@ defmodule Postoffice.Handlers.HttpTest do 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} = @@ -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)