From 35e641bcffe33bf8d096dd1f90ac10748e6bf776 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 16 Mar 2021 15:33:44 +0100 Subject: [PATCH] Fix geospatial runtime configuration Geospatial configuration was only evaluated at compile-time, not at runtime Signed-off-by: Thomas Citharel --- lib/service/geospatial/addok.ex | 15 ++++++++------- lib/service/geospatial/google_maps.ex | 18 +++++++++++------- lib/service/geospatial/map_quest.ex | 10 ++++++---- lib/service/geospatial/mimirsbrunn.ex | 6 ++---- lib/service/geospatial/nominatim.ex | 13 +++++++------ lib/service/geospatial/pelias.ex | 5 ++--- lib/service/geospatial/photon.ex | 6 ++---- lib/service/geospatial/provider.ex | 5 +++++ 8 files changed, 43 insertions(+), 35 deletions(-) diff --git a/lib/service/geospatial/addok.ex b/lib/service/geospatial/addok.ex index 908125e8f..e289925ae 100644 --- a/lib/service/geospatial/addok.ex +++ b/lib/service/geospatial/addok.ex @@ -6,15 +6,11 @@ defmodule Mobilizon.Service.Geospatial.Addok do alias Mobilizon.Addresses.Address alias Mobilizon.Service.Geospatial.Provider alias Mobilizon.Service.HTTP.GeospatialClient - + import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1] require Logger @behaviour Provider - @endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) - @default_country Application.get_env(:mobilizon, __MODULE__) |> get_in([:default_country]) || - "France" - @impl Provider @doc """ Addok implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`. @@ -40,7 +36,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do @spec build_url(atom(), map(), list()) :: String.t() defp build_url(method, args, options) do limit = Keyword.get(options, :limit, 10) - endpoint = Keyword.get(options, :endpoint, @endpoint) + endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__)) case method do :geocode -> @@ -71,7 +67,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do features |> Enum.map(fn %{"geometry" => geometry, "properties" => properties} -> %Address{ - country: Map.get(properties, "country", @default_country), + country: Map.get(properties, "country", default_country()), locality: Map.get(properties, "city"), region: Map.get(properties, "context"), description: Map.get(properties, "name") || street_address(properties), @@ -105,4 +101,9 @@ defmodule Mobilizon.Service.Geospatial.Addok do do: "#{url}&type=municipality" defp do_add_parameter(url, :type, _type), do: url + + defp default_country do + Application.get_env(:mobilizon, __MODULE__) |> get_in([:default_country]) || + "France" + end end diff --git a/lib/service/geospatial/google_maps.ex b/lib/service/geospatial/google_maps.ex index 63f61729f..3e145e6a7 100644 --- a/lib/service/geospatial/google_maps.ex +++ b/lib/service/geospatial/google_maps.ex @@ -13,11 +13,6 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do @behaviour Provider - @api_key Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) - - @fetch_place_details (Application.get_env(:mobilizon, __MODULE__) - |> get_in([:fetch_place_details])) in [true, "true", "True"] - @components [ "street_number", "route", @@ -77,7 +72,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do defp build_url(method, args, options) do limit = Keyword.get(options, :limit, 10) lang = Keyword.get(options, :lang, "en") - api_key = Keyword.get(options, :api_key, @api_key) + api_key = Keyword.get(options, :api_key, api_key()) if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message) url = "#{@geocode_endpoint}?limit=#{limit}&key=#{api_key}&language=#{lang}" @@ -118,7 +113,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do end) description = - if Keyword.get(options, :fetch_place_details, @fetch_place_details) == true do + if Keyword.get(options, :fetch_place_details, fetch_place_details()) == true do do_fetch_place_details(place_id, options) || description else description @@ -185,4 +180,13 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do do: "#{url}&components=administrative_area" defp do_add_parameter(url, :type, _), do: url + + defp api_key do + Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) + end + + defp fetch_place_details do + (Application.get_env(:mobilizon, __MODULE__) + |> get_in([:fetch_place_details])) in [true, "true", "True"] + end end diff --git a/lib/service/geospatial/map_quest.ex b/lib/service/geospatial/map_quest.ex index 249dae21e..77e7f6536 100644 --- a/lib/service/geospatial/map_quest.ex +++ b/lib/service/geospatial/map_quest.ex @@ -17,8 +17,6 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do @behaviour Provider - @api_key Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) - @api_key_missing_message "API Key required to use MapQuest" @impl Provider @@ -27,7 +25,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do """ @spec geocode(String.t(), keyword()) :: list(Address.t()) def geocode(lon, lat, options \\ []) do - api_key = Keyword.get(options, :api_key, @api_key) + api_key = Keyword.get(options, :api_key, api_key()) limit = Keyword.get(options, :limit, 10) open_data = Keyword.get(options, :open_data, true) @@ -56,7 +54,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do @spec search(String.t(), keyword()) :: list(Address.t()) def search(q, options \\ []) do limit = Keyword.get(options, :limit, 10) - api_key = Keyword.get(options, :api_key, @api_key) + api_key = Keyword.get(options, :api_key, api_key()) open_data = Keyword.get(options, :open_data, true) @@ -114,4 +112,8 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do street: Map.get(address, "street") } end + + defp api_key do + Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) + end end diff --git a/lib/service/geospatial/mimirsbrunn.ex b/lib/service/geospatial/mimirsbrunn.ex index c469f16f8..d718f9d9a 100644 --- a/lib/service/geospatial/mimirsbrunn.ex +++ b/lib/service/geospatial/mimirsbrunn.ex @@ -10,13 +10,11 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do alias Mobilizon.Addresses.Address alias Mobilizon.Service.Geospatial.Provider alias Mobilizon.Service.HTTP.GeospatialClient - + import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1] require Logger @behaviour Provider - @endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) - @impl Provider @doc """ Mimirsbrunn implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`. @@ -43,7 +41,7 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do defp build_url(method, args, options) do limit = Keyword.get(options, :limit, 10) lang = Keyword.get(options, :lang, "en") - endpoint = Keyword.get(options, :endpoint, @endpoint) + endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__)) case method do :search -> diff --git a/lib/service/geospatial/nominatim.ex b/lib/service/geospatial/nominatim.ex index e42e4eef2..92ba1b3fc 100644 --- a/lib/service/geospatial/nominatim.ex +++ b/lib/service/geospatial/nominatim.ex @@ -6,14 +6,11 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do alias Mobilizon.Addresses.Address alias Mobilizon.Service.Geospatial.Provider alias Mobilizon.Service.HTTP.GeospatialClient - + import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1] require Logger @behaviour Provider - @endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) - @api_key Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) - @impl Provider @doc """ Nominatim implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`. @@ -40,7 +37,7 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do defp build_url(method, args, options) do limit = Keyword.get(options, :limit, 10) lang = Keyword.get(options, :lang, "en") - endpoint = Keyword.get(options, :endpoint, @endpoint) + endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__)) url = case method do @@ -58,7 +55,7 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do url |> add_parameter(options, :country_code) - |> add_parameter(options, :api_key, @api_key) + |> add_parameter(options, :api_key, api_key()) end @spec fetch_features(String.t()) :: list(Address.t()) @@ -161,4 +158,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do @spec do_add_parameter(String.t(), atom(), any()) :: String.t() defp do_add_parameter(url, :api_key, api_key), do: "#{url}&key=#{api_key}" + + defp api_key do + Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key]) + end end diff --git a/lib/service/geospatial/pelias.ex b/lib/service/geospatial/pelias.ex index 35fd2b5bc..6622124be 100644 --- a/lib/service/geospatial/pelias.ex +++ b/lib/service/geospatial/pelias.ex @@ -8,12 +8,11 @@ defmodule Mobilizon.Service.Geospatial.Pelias do alias Mobilizon.Addresses.Address alias Mobilizon.Service.Geospatial.Provider alias Mobilizon.Service.HTTP.GeospatialClient + import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1] require Logger @behaviour Provider - @endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) - @impl Provider @doc """ Pelias implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`. @@ -40,7 +39,7 @@ defmodule Mobilizon.Service.Geospatial.Pelias do defp build_url(method, args, options) do limit = Keyword.get(options, :limit, 10) lang = Keyword.get(options, :lang, "en") - endpoint = Keyword.get(options, :endpoint, @endpoint) + endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__)) url = case method do diff --git a/lib/service/geospatial/photon.ex b/lib/service/geospatial/photon.ex index ef5f33b43..ef47bb7f5 100644 --- a/lib/service/geospatial/photon.ex +++ b/lib/service/geospatial/photon.ex @@ -6,13 +6,11 @@ defmodule Mobilizon.Service.Geospatial.Photon do alias Mobilizon.Addresses.Address alias Mobilizon.Service.Geospatial.Provider alias Mobilizon.Service.HTTP.GeospatialClient - + import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1] require Logger @behaviour Provider - @endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint]) - @impl Provider @doc """ Photon implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`. @@ -42,7 +40,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do limit = Keyword.get(options, :limit, 10) lang = Keyword.get(options, :lang, "en") coords = Keyword.get(options, :coords, nil) - endpoint = Keyword.get(options, :endpoint, @endpoint) + endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__)) case method do :search -> diff --git a/lib/service/geospatial/provider.ex b/lib/service/geospatial/provider.ex index be3193e5c..2bc7b7f4c 100644 --- a/lib/service/geospatial/provider.ex +++ b/lib/service/geospatial/provider.ex @@ -80,4 +80,9 @@ defmodule Mobilizon.Service.Geospatial.Provider do @spec coordinates(any) :: nil def coordinates(_, _), do: nil + + @spec endpoint(atom()) :: String.t() + def endpoint(provider) do + Application.get_env(:mobilizon, provider) |> get_in([:endpoint]) + end end