defmodule ExVideoFactory.Client do use HTTPotion.Base @moduledoc """ Documentation for ExVideoFactory.Client. """ @prod_endpoint "https://gatewayvf.webservices.francetelevisions.fr/v1/" @preprod_endpoint "https://gatewayvf.webservices.ftv-preprod.fr/v1/" def process_url(url, options) do case Application.get_env(:ex_video_factory, :mode, :prod) do :preprod -> @preprod_endpoint <> url :prod -> @prod_endpoint <> url :custom -> Application.get_env(:ex_video_factory, :endpoint) <> url _ -> raise(ArgumentError, message: "invalid argument mode, valid are :prod or :preprod") end |> prepend_protocol |> append_query_string(options) end defp prepend_protocol(url) do if url =~ ~r/\Ahttps?:\/\// do url else "http://" <> url end end defp append_query_string(url, options) do if options[:query] do url <> "?#{URI.encode_query(options[:query])}" else url end end def process_request_headers(headers) do Keyword.put(headers, :Accept, "application/json") end def process_response_body(body) do body |> Poison.decode!() end end