Packages

This package is used for learning purposes on how to use 'mix' and 'hex' for sharing elixir code. Do NOT waste your time here. The License is MIT, but the project uses a private api.

Current section

Files

Jump to
cape lib Arpas REST Esri Api.ex
Raw

lib/Arpas/REST/Esri/Api.ex

defmodule Arpas.REST.Esri.Api do
alias Arpas.REST.Esri.Env
def token do
url = Env.token_url()
id = Env.id()
secret = Env.secret()
expiration = "21600"
headers = [
{"Accept", "application/json"},
{"Content-Type", "application/x-www-form-urlencoded"},
# , no-store, must-revalidate"}
{"Cache-Control", "no-cache"}
]
payload =
"client_id=#{id}&client_secret=#{secret}&grant_type=client_credentials&expiration=#{expiration}"
case HTTPoison.post(url, payload, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, my_token} =
body
|> Poison.decode()
adesso = DateTime.now!("Etc/UTC")
my_token = Map.put(my_token, "log_now", adesso)
Map.put(my_token, "scade", DateTime.add(adesso, my_token["expires_in"], :second))
{:ok, %HTTPoison.Response{status_code: 404}} ->
IO.puts("Not found :(")
{:error, %HTTPoison.Error{reason: reason}} ->
IO.inspect(reason)
end
end
def get_access_token do
token()
|> Map.get("access_token")
end
def feature(feauture_url, numero) do
access = get_access_token()
url = feauture_url <> numero <> "/query?where=1=1&outfields=*"
headers = [
{"Accept", "application/json"},
{"Content-Type", "application/x-www-form-urlencoded"},
{"Cache-Control", "no-cache, no-store, must-revalidate"}
]
IO.puts(url)
payload = "token=#{access}&f=json"
case HTTPoison.post(url, payload, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, idro} =
body
|> Poison.decode()
idro
{:ok, %HTTPoison.Response{status_code: 404}} ->
IO.puts("Not found :(")
{:error, codice: "404"}
{:error, %HTTPoison.Error{reason: reason}} ->
IO.inspect(reason)
{:error, codice: reason}
end
end
def service_features(url) do
access = get_access_token()
IO.puts(url)
url =
if url == "" do
Env.services() <> "?token=#{access}&f=json"
else
url <> "?token=#{access}&f=json"
end
headers = [
{"Accept", "application/json"},
{"Content-Type", "application/x-www-form-urlencoded"},
{"Cache-Control", "no-cache, no-store, must-revalidate"}
]
case HTTPoison.get(url, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
body
|> Poison.decode()
{:ok, %HTTPoison.Response{status_code: 404}} ->
IO.puts("Not found :(")
{:error, codice: "404"}
{:error, %HTTPoison.Error{reason: reason}} ->
IO.inspect(reason)
{:error, codice: reason}
value ->
IO.puts(value)
end
end
def layers(url) do
case service_features(url) do
{:ok, servizio} ->
servizio
{:error, reason} ->
reason
end
end
end