Fix geospatial clients

add json plus to base client and rename to geospacial client
geospatial http client with json plug
This commit is contained in:
setop
2020-08-30 23:29:56 +02:00
committed by Thomas Citharel
parent 25e748a880
commit bd3087d121
15 changed files with 124 additions and 111 deletions

View File

@@ -9,7 +9,7 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.Geospatial.Provider
alias Mobilizon.Service.HTTP.BaseClient
alias Mobilizon.Service.HTTP.GeospatialClient
require Logger
@@ -23,15 +23,9 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
"""
@spec geocode(number(), number(), keyword()) :: list(Address.t())
def geocode(lon, lat, options \\ []) do
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
Logger.debug("Asking Mimirsbrunn for reverse geocoding with #{url}")
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
{:ok, %{"features" => features}} <- Jason.decode(body) do
process_data(features)
else
_ -> []
end
:geocode
|> build_url(%{lon: lon, lat: lat}, options)
|> fetch_features
end
@impl Provider
@@ -40,15 +34,9 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
"""
@spec search(String.t(), keyword()) :: list(Address.t())
def search(q, options \\ []) do
url = build_url(:search, %{q: q}, options)
Logger.debug("Asking Mimirsbrunn for addresses with #{url}")
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
{:ok, %{"features" => features}} <- Jason.decode(body) do
process_data(features)
else
_ -> []
end
:search
|> build_url(%{q: q}, options)
|> fetch_features
end
@spec build_url(atom(), map(), list()) :: String.t()
@@ -68,6 +56,20 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
end
end
@spec fetch_features(String.t()) :: list(Address.t())
defp fetch_features(url) do
Logger.debug("Asking addok with #{url}")
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
%{"features" => features} <- body do
process_data(features)
else
_ ->
Logger.error("Asking addok with #{url}")
[]
end
end
defp process_data(features) do
features
|> Enum.map(fn %{