Skip to content

Commit

Permalink
Merge pull request #1570 from GSA/dev-with-httpoison-1445
Browse files Browse the repository at this point in the history
Replace HTTPoison Card #1445
  • Loading branch information
kkrug authored Feb 20, 2025
2 parents ccc6a0c + 328ae7a commit d834e02
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 152 deletions.
2 changes: 1 addition & 1 deletion assets/client/src/components/ChallengeDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const ChallengeDetails = ({challenge, challengePhases, preview, print, ta
<div className="follow-tooltip__section">
<h4>Follow challenge as guest</h4>
<p>Receive challenge updates to your email. No sign-in required</p>
<a href={preview ? "#" : `${encodeURIComponent(challenge.gov_delivery_topic_subscribe_link)}`}>
<a href={preview ? "#" : `${challenge.gov_delivery_topic_subscribe_link}`}>
<button className="follow-tooltip__button">Follow challenge</button>
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/challenge_gov/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule ChallengeGov.Application do
# Start the PubSub system
{Phoenix.PubSub, name: ChallengeGov.PubSub},
ChallengeGov.Repo,
{Finch, name: ChallengeGov.HTTPClient},
# {Finch, name: ChallengeGov.HTTPClient},
Web.Endpoint,
ChallengeGov.Scheduler,
ChallengeGov.Telemetry,
Expand Down
201 changes: 76 additions & 125 deletions lib/challenge_gov/gov_delivery/implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,219 +24,170 @@ defmodule ChallengeGov.GovDelivery.Implementation do
|> code()
|> GovDelivery.remove_topic_endpoint()

request = Finch.build(:delete, endpoint, auth_headers())
Logger.info(inspect(request))
headers = auth_headers()
Logger.info("Delete(#{endpoint}, #{inspect(headers)})")

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
case HTTPoison.delete(endpoint, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
Logger.info("Gov Delivery Removed Topic #{challenge.id}")
Challenges.clear_gov_delivery_topic(challenge)
{:ok, :removed}

{:ok, %{body: body}} ->
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error("Gov Delivery Failed to Remove Topic #{challenge.id} #{inspect(body)}")
{:error, inspect(body)}

e ->
Logger.error("Gov Delivery Failed to Remove Topic #{challenge.id} E: #{inspect(e)}")
{:error, e}
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error("Gov Delivery Failed to Remove Topic #{challenge.id} E: #{inspect(reason)}")
{:error, reason}
end
end

@impl ChallengeGov.GovDelivery
def add_topic(challenge) do
body = xml_topic_from_challenge(challenge)
headers = auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
endpoint = GovDelivery.create_topic_endpoint()

request =
Finch.build(
:post,
GovDelivery.create_topic_endpoint(),
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}],
body
)
Logger.info("Post(#{endpoint}, #{body}, #{inspect(headers)})")

Logger.info(inspect(request))

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
case HTTPoison.post(endpoint, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
Challenges.store_gov_delivery_topic(challenge, code(challenge.id))
set_category(challenge)

{:ok, %{body: body}} ->
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error("Gov Delivery Failed to Add Topic #{challenge.id} #{inspect(body)}")
{:error, inspect(body)}

e ->
Logger.error("Gov Delivery Failed to Add Topic #{challenge.id} E: #{inspect(e)}")
{:error, e}
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error("Gov Delivery Failed to Add Topic #{challenge.id} E: #{inspect(reason)}")
{:error, reason}
end
end

@impl ChallengeGov.GovDelivery
def subscribe_user_general(user) do
body = xml_subscribe_general(user)
headers = auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
endpoint = GovDelivery.subscribe_endpoint()

request =
Finch.build(
:post,
GovDelivery.subscribe_endpoint(),
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}],
body
)

Logger.info(inspect(request))
Logger.info("Post(#{endpoint}, #{body}, #{inspect(headers)})")

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
case HTTPoison.post(endpoint, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
{:ok, :subscribed}

{:ok, %{body: body}} ->
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error("Gov Delivery Failed to Subscribe User General #{user.id} #{inspect(body)}")
{:error, inspect(body)}

e ->
Logger.error("Gov Delivery Failed to Subscribe User General #{user.id} E: #{inspect(e)}")
{:error, e}
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error(
"Gov Delivery Failed to Subscribe User General #{user.id} E: #{inspect(reason)}"
)

{:error, reason}
end
end

@impl ChallengeGov.GovDelivery
def subscribe_user_challenge(user, challenge) do
body = xml_subscribe_challenge(user, challenge)
headers = auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
endpoint = GovDelivery.subscribe_endpoint()

request =
Finch.build(
:post,
GovDelivery.subscribe_endpoint(),
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}],
body
)

Logger.info(inspect(request))
Logger.info("Post(#{endpoint}, #{body}, #{inspect(headers)})")

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
case HTTPoison.post(endpoint, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
{:ok, :subscribed}

{:ok, %{body: body}} ->
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error(
"Gov Delivery Failed to Subscribe User Challenge user: #{user.id} challenge: #{challenge.id} #{inspect(body)}"
"Gov Delivery Failed to Subscribe User Challenge #{user.id} #{challenge.id} #{inspect(body)}"
)

{:error, inspect(body)}

e ->
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error(
"Gov Delivery Failed to Subscribe User Challenge user: #{user.id} challenge: #{challenge.id} E: #{inspect(e)}"
"Gov Delivery Failed to Subscribe User Challenge #{user.id} #{challenge.id} E: #{inspect(reason)}"
)

{:error, e}
{:error, reason}
end
end

@impl ChallengeGov.GovDelivery
def send_bulletin(challenge, subject, body) do
body = xml_send_bulletin(challenge, subject, body)
headers = auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
endpoint = GovDelivery.send_bulletin_endpoint()

request =
Finch.build(
:post,
GovDelivery.send_bulletin_endpoint(),
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}],
body
)

Logger.info(inspect(request))
Logger.info("Post(#{endpoint}, #{body}, #{inspect(headers)})")

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
case HTTPoison.post(endpoint, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
{:ok, :sent}

{:ok, %{body: body}} ->
Logger.error(
"Gov Delivery Failed to Send Bulletin subject: #{inspect(subject)} challenge: #{challenge.id} #{inspect(body)}"
)

{:send_error, inspect(body)}

e ->
Logger.error(
"Gov Delivery Failed to Send Bulletin subject: #{inspect(subject)} challenge: #{challenge.id} E: #{inspect(e)}"
)
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error("Gov Delivery Failed to Send Bulletin #{challenge.id} #{inspect(body)}")
{:error, inspect(body)}

{:send_error, e}
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error("Gov Delivery Failed to Send Bulletin #{challenge.id} E: #{inspect(reason)}")
{:error, reason}
end
end

@impl ChallengeGov.GovDelivery
def get_topic_subscribe_count(challenge) do
request =
Finch.build(
:get,
GovDelivery.topic_details_endpoint(code(challenge.id)),
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
)

Logger.info(inspect(request))
endpoint = challenge.id |> code() |> GovDelivery.get_topic_subscribe_count_endpoint()

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200, body: body}} ->
result =
body
|> xpath(~x"//topic/subscribers-count/text()")
|> to_string()
headers = auth_headers()
Logger.info("Get(#{endpoint}, #{inspect(headers)})")

{:ok, parse_count_result(result)}
case HTTPoison.get(endpoint, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
count = body |> parse_count_result()
{:ok, count}

{:ok, %{body: body}} ->
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error(
"Gov Delivery Failed to get topic subscribe count challenge: #{challenge.id} #{inspect(body)}"
"Gov Delivery Failed to Get Topic Subscribe Count #{challenge.id} #{inspect(body)}"
)

{:error, inspect(body)}

e ->
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error(
"Gov Delivery Failed to get topic subscribe count challenge: #{challenge.id} E: #{inspect(e)}"
"Gov Delivery Failed to Get Topic Subscribe Count #{challenge.id} E: #{inspect(reason)}"
)

{:error, e}
{:error, reason}
end
end

def set_category(challenge) do
endpoint =
challenge.id
|> code()
|> GovDelivery.set_topic_categories_endpoint()

request =
Finch.build(
:put,
endpoint,
auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}],
xml_categories_for_challenge()
)
body = xml_categories_for_challenge()
headers = auth_headers() ++ [{"content-type", "application/xml; charset: utf-8"}]
endpoint = GovDelivery.set_category_endpoint()

Logger.info(inspect(request))
Logger.info("Post(#{endpoint}, #{body}, #{inspect(headers)})")

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200}} ->
{:ok, :added}
case HTTPoison.post(endpoint, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
Logger.info("Gov Delivery Set Category #{challenge.id}")
{:ok, :set}

{:ok, %{body: body}} ->
Logger.error(
"Gov Delivery Failed to set category challenge: #{challenge.id} #{inspect(body)}"
)

{:category_error, inspect(body)}

e ->
Logger.error(
"Gov Delivery Failed to set category challenge: #{challenge.id} E: #{inspect(e)}"
)
{:ok, %HTTPoison.Response{body: body}} ->
Logger.error("Gov Delivery Failed to Set Category #{challenge.id} #{inspect(body)}")
{:error, inspect(body)}

{:category_error, e}
{:error, %HTTPoison.Error{reason: reason}} ->
Logger.error("Gov Delivery Failed to Set Category #{challenge.id} E: #{inspect(reason)}")
{:error, reason}
end
end

Expand Down
10 changes: 4 additions & 6 deletions lib/challenge_gov/recaptcha/implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ defmodule ChallengeGov.Recaptcha.Implementation do

body = Plug.Conn.Query.encode(%{secret: key, response: token})

request = Finch.build(:post, "https://www.google.com/recaptcha/api/siteverify", headers, body)

case Finch.request(request, HTTPClient) do
{:ok, %{body: body, status: 200}} ->
case HTTPoison.post("https://www.google.com/recaptcha/api/siteverify", body, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, Jason.decode!(body)}

{:error, failure} ->
{:error, "Error: " <> inspect(failure)}
{:error, %HTTPoison.Error{reason: reason}} ->
{:error, "Error: " <> inspect(reason)}

_ ->
{:error, "Unknown Recaptcha Failure"}
Expand Down
8 changes: 4 additions & 4 deletions lib/mix/tasks/import_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,18 @@ defmodule Mix.Tasks.ImportHelper do

{:ok, tmp_file} = Stein.Storage.Temp.create(extname: extension)

request = Finch.build(:get, logo_url)
headers = [{"Content-Type", "application/octet-stream"}]

case Finch.request(request, HTTPClient) do
{:ok, %{status: 200, body: body}} ->
case HTTPoison.get(logo_url, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
File.write!(tmp_file, body, [:binary])

%{
filename: filename,
path: tmp_file
}

{:ok, %{status: 404}} ->
{:ok, %HTTPoison.Response{status_code: 404}} ->
""
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/web/controllers/api/banner_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Web.Api.BannerController do
use Web, :controller
import Plug.Conn

alias ChallengeGov.SiteContent
def init(default), do: default

def call(conn, _params) do
{:ok, banner} = SiteContent.get("site_wide_banner")

case banner_is_active?(banner) do
true ->
text(conn, banner.content)

false ->
text(conn, "")
end
end

defp banner_is_active?(banner) do
now = DateTime.utc_now()

if is_nil(banner.start_date) or is_nil(banner.end_date) do
false
else
!is_nil(banner.content) and
DateTime.compare(now, banner.start_date) === :gt and
DateTime.compare(now, banner.end_date) === :lt
end
end
end
2 changes: 2 additions & 0 deletions lib/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ defmodule Web.Router do
get("/phase/:phase_id/winners", WinnerController, :phase_winners)

get("/challenges/filter", ChallengeController, :filter)

get("/banner", BannerController, :ok)
end

# Public Routes
Expand Down
Loading

0 comments on commit d834e02

Please sign in to comment.