|
| 1 | +defmodule OpenTripPlannerClient do |
| 2 | + @moduledoc """ |
| 3 | + Fetches data from the OpenTripPlanner API. |
| 4 | +
|
| 5 | + ## Configuration |
| 6 | +
|
| 7 | + ```elixir |
| 8 | + config :mobile_app_backend, |
| 9 | + otp_url: "http://localhost:8080", |
| 10 | + timezone: "America/New_York" |
| 11 | + ``` |
| 12 | + """ |
| 13 | + |
| 14 | + require Logger |
| 15 | + |
| 16 | + alias OpenTripPlannerClient.{Itinerary, ItineraryTag, NamedPosition, ParamsBuilder, Parser} |
| 17 | + |
| 18 | + @behaviour OpenTripPlannerClient.Behaviour |
| 19 | + |
| 20 | + @type error :: OpenTripPlannerClient.Behaviour.error() |
| 21 | + @type plan_opt :: OpenTripPlannerClient.Behaviour.plan_opt() |
| 22 | + |
| 23 | + @impl true |
| 24 | + @doc """ |
| 25 | + Generate a trip plan with the given endpoints and options. |
| 26 | + """ |
| 27 | + @spec plan(NamedPosition.t(), NamedPosition.t(), [plan_opt()]) :: |
| 28 | + {:ok, Itinerary.t()} | {:error, error()} |
| 29 | + def plan(from, to, opts) do |
| 30 | + accessible? = Keyword.get(opts, :wheelchair_accessible?, false) |
| 31 | + |
| 32 | + {postprocess_opts, opts} = Keyword.split(opts, [:tags]) |
| 33 | + |
| 34 | + with {:ok, params} <- ParamsBuilder.build_params(from, to, opts) do |
| 35 | + param_string = Enum.map_join(params, "\n", fn {key, val} -> ~s{#{key}: #{val}} end) |
| 36 | + |
| 37 | + graphql_query = """ |
| 38 | + { |
| 39 | + plan( |
| 40 | + #{param_string} |
| 41 | + ) |
| 42 | + #{itinerary_shape()} |
| 43 | + } |
| 44 | + """ |
| 45 | + |
| 46 | + root_url = |
| 47 | + Keyword.get(opts, :root_url, Application.fetch_env!(:mobile_app_backend, :otp_url)) |
| 48 | + |
| 49 | + graphql_url = "#{root_url}/otp/routers/default/index/" |
| 50 | + |
| 51 | + with {:ok, body} <- send_request(graphql_url, graphql_query), |
| 52 | + {:ok, itineraries} <- Parser.parse_ql(body, accessible?) do |
| 53 | + tags = Keyword.get(postprocess_opts, :tags, []) |
| 54 | + |
| 55 | + result = |
| 56 | + Enum.reduce(tags, itineraries, fn tag, itineraries -> |
| 57 | + ItineraryTag.apply_tag(tag, itineraries) |
| 58 | + end) |
| 59 | + |
| 60 | + {:ok, result} |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + defp send_request(url, query) do |
| 66 | + with {:ok, response} <- log_response(url, query), |
| 67 | + %{status: 200, body: body} <- response do |
| 68 | + {:ok, body} |
| 69 | + else |
| 70 | + %{status: _} = response -> |
| 71 | + {:error, response} |
| 72 | + |
| 73 | + error -> |
| 74 | + error |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + defp log_response(url, query) do |
| 79 | + graphql_req = |
| 80 | + Req.new(base_url: url) |
| 81 | + |> AbsintheClient.attach() |
| 82 | + |
| 83 | + {duration, response} = |
| 84 | + :timer.tc( |
| 85 | + Req, |
| 86 | + :post, |
| 87 | + [graphql_req, [graphql: query]] |
| 88 | + ) |
| 89 | + |
| 90 | + _ = |
| 91 | + Logger.info(fn -> |
| 92 | + "#{__MODULE__}.plan_response url=#{url} query=#{inspect(query)} #{status_text(response)} duration=#{duration / :timer.seconds(1)}" |
| 93 | + end) |
| 94 | + |
| 95 | + response |
| 96 | + end |
| 97 | + |
| 98 | + defp status_text({:ok, %{status: code}}) do |
| 99 | + "status=#{code}" |
| 100 | + end |
| 101 | + |
| 102 | + defp status_text({:error, error}) do |
| 103 | + "status=error error=#{inspect(error)}" |
| 104 | + end |
| 105 | + |
| 106 | + defp itinerary_shape do |
| 107 | + """ |
| 108 | + { |
| 109 | + routingErrors { |
| 110 | + code |
| 111 | + description |
| 112 | + } |
| 113 | + itineraries { |
| 114 | + accessibilityScore |
| 115 | + startTime |
| 116 | + endTime |
| 117 | + duration |
| 118 | + legs { |
| 119 | + mode |
| 120 | + startTime |
| 121 | + endTime |
| 122 | + distance |
| 123 | + duration |
| 124 | + intermediateStops { |
| 125 | + id |
| 126 | + gtfsId |
| 127 | + name |
| 128 | + desc |
| 129 | + lat |
| 130 | + lon |
| 131 | + code |
| 132 | + locationType |
| 133 | + } |
| 134 | + transitLeg |
| 135 | + headsign |
| 136 | + realTime |
| 137 | + realtimeState |
| 138 | + agency { |
| 139 | + id |
| 140 | + gtfsId |
| 141 | + name |
| 142 | + } |
| 143 | + alerts { |
| 144 | + id |
| 145 | + alertHeaderText |
| 146 | + alertDescriptionText |
| 147 | + } |
| 148 | + fareProducts { |
| 149 | + id |
| 150 | + product { |
| 151 | + id |
| 152 | + name |
| 153 | + riderCategory { |
| 154 | + id |
| 155 | + name |
| 156 | +
|
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + from { |
| 161 | + name |
| 162 | + lat |
| 163 | + lon |
| 164 | + departureTime |
| 165 | + arrivalTime |
| 166 | + stop { |
| 167 | + gtfsId |
| 168 | + } |
| 169 | + } |
| 170 | + to { |
| 171 | + name |
| 172 | + lat |
| 173 | + lon |
| 174 | + departureTime |
| 175 | + arrivalTime |
| 176 | + stop { |
| 177 | + gtfsId |
| 178 | + } |
| 179 | + } |
| 180 | + route { |
| 181 | + gtfsId |
| 182 | + longName |
| 183 | + shortName |
| 184 | + desc |
| 185 | + color |
| 186 | + textColor |
| 187 | + } |
| 188 | + trip { |
| 189 | + gtfsId |
| 190 | + } |
| 191 | + steps { |
| 192 | + distance |
| 193 | + streetName |
| 194 | + lat |
| 195 | + lon |
| 196 | + relativeDirection |
| 197 | + stayOn |
| 198 | + } |
| 199 | + legGeometry { |
| 200 | + points |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + """ |
| 206 | + end |
| 207 | +end |
0 commit comments