Is there any kind of restriction while using an exs script with Mix.install and structs defined by the modules in an installed dependency. I am trying an exs script with Mix.install and a struct in the installed mix package cannot be found by the script.
** (CompileError) struct.exs:6: HTTPoison.Response.__struct__/0 is undefined, cannot expand struct HTTPoison.Response. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
That’s a good question which I’m not sure why it doesn’t work right in the root level of the script.
But for sure it works if the struct is nested within a module. So the code below works,
Mix.install([{:httpoison, "~> 1.8"}])
url = "https://httparrot.herokuapp.com/ip"
defmodule Response do
def successful_response(resp) do
case resp do
{:ok, %HTTPoison.Response{status_code: 200} = response} ->
{:ok, response}
other ->
{:error, "Unexpected response #{inspect(other)}"}
end
end
end
url
|> HTTPoison.get()
|> Response.successful_response()
|> IO.inspect()