Add custom user-agent to geospatial calls
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -30,6 +30,10 @@ defmodule Mobilizon.Config do
|
||||
@spec instance_email_reply_to :: String.t()
|
||||
def instance_email_reply_to, do: instance_config()[:email_reply_to]
|
||||
|
||||
@spec instance_user_agent :: String.t()
|
||||
def instance_user_agent,
|
||||
do: "#{instance_name()} #{instance_hostname()} - Mobilizon #{Mix.Project.config()[:version]}"
|
||||
|
||||
@spec get(module | atom) :: any
|
||||
def get(key), do: get(key, nil)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Config
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -18,12 +19,14 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
|
||||
Logger.debug("Asking addok for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
process_data(features)
|
||||
end
|
||||
@@ -35,11 +38,13 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking addok for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
process_data(features)
|
||||
end
|
||||
|
||||
@@ -11,6 +11,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Config
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -29,6 +30,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
api_key = Keyword.get(options, :api_key, @api_key)
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
open_data = Keyword.get(options, :open_data, true)
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
|
||||
prefix = if open_data, do: "open", else: "www"
|
||||
|
||||
@@ -38,7 +41,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
HTTPoison.get(
|
||||
"https://#{prefix}.mapquestapi.com/geocoding/v1/reverse?key=#{api_key}&location=#{
|
||||
lat
|
||||
},#{lon}&maxResults=#{limit}"
|
||||
},#{lon}&maxResults=#{limit}",
|
||||
headers
|
||||
),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
results |> Enum.map(&process_data/1)
|
||||
@@ -54,6 +58,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
api_key = Keyword.get(options, :api_key, @api_key)
|
||||
|
||||
@@ -71,7 +77,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
Logger.debug("Asking MapQuest for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
results |> Enum.map(&process_data/1)
|
||||
else
|
||||
|
||||
@@ -5,6 +5,7 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Config
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -19,11 +20,13 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking Nominatim for geocode with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, body} <- Poison.decode(body) do
|
||||
[process_data(body)]
|
||||
end
|
||||
@@ -35,11 +38,13 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking Nominatim for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, body} <- Poison.decode(body) do
|
||||
body |> Enum.map(fn entry -> process_data(entry) end) |> Enum.filter(& &1)
|
||||
end
|
||||
|
||||
@@ -5,6 +5,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.Provider
|
||||
alias Mobilizon.Config
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -20,11 +21,13 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
"""
|
||||
@spec geocode(number(), number(), keyword()) :: list(Address.t())
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
Logger.debug("Asking photon for reverse geocoding with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
process_data(features)
|
||||
end
|
||||
@@ -36,11 +39,13 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
def search(q, options \\ []) do
|
||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
||||
headers = [{"User-Agent", user_agent}]
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
Logger.debug("Asking photon for addresses with #{url}")
|
||||
|
||||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
HTTPoison.get(url, headers),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
process_data(features)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user