Merge branch 'fix-geospatial-configuration' into 'master'

Fix geospatial runtime configuration

See merge request framasoft/mobilizon!860
This commit is contained in:
Thomas Citharel 2021-03-16 15:07:30 +00:00
commit c57269469b
8 changed files with 43 additions and 35 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 ->

View file

@ -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

View file

@ -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

View file

@ -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 ->

View file

@ -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