defmodule Arpas.REST.Generico.Api do @moduledoc ~S''' Utilità generiche Api rest ''' def post(url, payload) do headers = [ {"Accept", "application/json"}, {"Content-Type", "application/x-www-form-urlencoded"}, {"Cache-Control", "no-cache, no-store, must-revalidate"} ] result = case HTTPoison.post(url, payload, headers) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> body {:ok, %HTTPoison.Response{status_code: 404}} -> IO.puts("Not found :(") {:error, %HTTPoison.Error{reason: reason}} -> IO.inspect(reason) end result end def get(url) do headers = [ {"Accept", "application/json"}, {"Content-Type", "application/x-www-form-urlencoded"}, {"Cache-Control", "no-cache, no-store, must-revalidate"} ] result = case HTTPoison.get(url, headers) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> Poison.decode(body) {:ok, %HTTPoison.Response{status_code: 404}} -> IO.puts("Not found :(") {:error, %HTTPoison.Error{reason: reason}} -> IO.inspect(reason) end result end end