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

@@ -5,7 +5,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.Geospatial.Provider
alias Mobilizon.Service.HTTP.BaseClient
alias Mobilizon.Service.HTTP.GeospatialClient
require Logger
@@ -21,15 +21,9 @@ defmodule Mobilizon.Service.Geospatial.Photon 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 photon for reverse geocoding with #{url}")
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
%{"features" => features} <- body do
process_data(features)
else
_ -> []
end
:geocode
|> build_url(%{lon: lon, lat: lat}, options)
|> fetch_features
end
@impl Provider
@@ -38,15 +32,9 @@ defmodule Mobilizon.Service.Geospatial.Photon do
"""
@spec search(String.t(), keyword()) :: list(Address.t())
def search(q, options \\ []) do
url = build_url(:search, %{q: q}, options)
Logger.debug("Asking photon for addresses with #{url}")
with {:ok, %{status: 200, body: body}} <- BaseClient.get(url),
%{"features" => features} <- body do
process_data(features)
else
_ -> []
end
:search
|> build_url(%{q: q}, options)
|> fetch_features
end
@spec build_url(atom(), map(), list()) :: String.t()
@@ -66,6 +54,20 @@ defmodule Mobilizon.Service.Geospatial.Photon do
end
end
@spec fetch_features(String.t()) :: list(Address.t())
defp fetch_features(url) do
Logger.debug("Asking photon with #{url}")
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
%{"features" => features} <- body do
process_data(features)
else
_ ->
Logger.error("Asking photon with #{url}")
[]
end
end
defp process_data(features) do
features
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->