Skip to content

Commit

Permalink
somewhat overengineer API mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcactus committed Dec 12, 2023
1 parent 124984c commit 6f79f41
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 669 deletions.
52 changes: 52 additions & 0 deletions lib/mix/tasks/mock_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule Mix.Tasks.MockApi do
require Logger
use Mix.Task
@shortdoc "Refresh mock API responses"
@requirements ["app.start"]

@urls %{
stops_boyls:
{"/stops/place-boyls",
"include=parent_station%2Cfacilities%2Cchild_stops&fields%5Bfacility%5D=long_name%2Ctype%2Cproperties%2Clatitude%2Clongitude%2Cid&fields%5Bstop%5D=address%2Cname%2Clatitude%2Clongitude%2Caddress%2Cmunicipality%2Cwheelchair_boarding%2Clocation_type%2Cplatform_name%2Cplatform_code%2Cdescription"},
routes_boyls: {"/routes/", "stop=place-boyls&include=route_patterns"}
}

def run(_) do
test_data_dir = Application.app_dir(:mobile_app_backend, ["priv", "test-data"])
File.mkdir_p!(test_data_dir)

url_paths =
for {name, {path, query}} <- @urls do
data = get!(path, query)

filename = "#{name}.json"

Path.join(test_data_dir, filename)
|> File.write!(data)

%{path => %{query => filename}}
end
|> Enum.reduce(fn m1, m2 -> Map.merge(m1, m2, fn _k, v1, v2 -> Map.merge(v1, v2) end) end)

Path.join(test_data_dir, "meta.json")
|> File.write!(Jason.encode_to_iodata!(url_paths, pretty: true))
end

defp get!(path, query) do
base_url = Application.fetch_env!(:mobile_app_backend, :base_url)
api_key = Application.fetch_env!(:mobile_app_backend, :api_key)

url =
URI.new!(base_url) |> URI.append_path(path) |> URI.append_query(query) |> URI.to_string()

Logger.info("Downloading #{url}")

%HTTPoison.Response{body: body, status_code: 200} =
HTTPoison.get!(url, [
{"accept", "application/vnd.api+json"}
| MBTAV3API.Headers.build(api_key, use_cache?: false)
])

body
end
end
8 changes: 8 additions & 0 deletions priv/test-data/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"/routes/": {
"stop=place-boyls&include=route_patterns": "routes_boyls.json"
},
"/stops/place-boyls": {
"include=parent_station%2Cfacilities%2Cchild_stops&fields%5Bfacility%5D=long_name%2Ctype%2Cproperties%2Clatitude%2Clongitude%2Cid&fields%5Bstop%5D=address%2Cname%2Clatitude%2Clongitude%2Caddress%2Cmunicipality%2Cwheelchair_boarding%2Clocation_type%2Cplatform_name%2Cplatform_code%2Cdescription": "stops_boyls.json"
}
}
1 change: 1 addition & 0 deletions priv/test-data/routes_boyls.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions priv/test-data/stops_boyls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"attributes":{"address":"Boylston St and Tremont St, Boston, MA","description":null,"latitude":42.35302,"location_type":1,"longitude":-71.06459,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":2},"id":"place-boyls","links":{"self":"/stops/place-boyls"},"relationships":{"child_stops":{"data":[{"id":"70158","type":"stop"},{"id":"70159","type":"stop"},{"id":"door-boyls-inbound","type":"stop"},{"id":"door-boyls-outbound","type":"stop"},{"id":"node-boyls-in-farepaid","type":"stop"},{"id":"node-boyls-in-fareunpaid","type":"stop"},{"id":"node-boyls-instair-platform","type":"stop"},{"id":"node-boyls-out-farepaid","type":"stop"},{"id":"node-boyls-out-fareunpaid","type":"stop"},{"id":"node-boyls-outstair-platform","type":"stop"}]},"facilities":{"data":[{"id":"fvm-201221","type":"facility"},{"id":"fvm-201222","type":"facility"},{"id":"fvm-201223","type":"facility"},{"id":"fvm-201224","type":"facility"},{"id":"fvm-202157","type":"facility"}],"links":{"related":"/facilities/?filter[stop]=place-boyls"}},"parent_station":{"data":null},"zone":{"data":null}},"type":"stop"},"included":[{"attributes":{"address":null,"description":"Boylston - Green Line - Park Street & North","latitude":42.352531,"location_type":0,"longitude":-71.064682,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":"Park Street & North","wheelchair_boarding":2},"id":"70158","links":{"self":"/stops/70158"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=70158"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":{"id":"RapidTransit","type":"zone"}}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Green Line - Copley & West","latitude":42.353214,"location_type":0,"longitude":-71.064545,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":"Copley & West","wheelchair_boarding":2},"id":"70159","links":{"self":"/stops/70159"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=70159"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":{"id":"RapidTransit","type":"zone"}}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Boston Common, Street","latitude":42.352531,"location_type":2,"longitude":-71.064685,"municipality":"Boston","name":"Boylston - Boston Common, Street","platform_code":null,"platform_name":null,"wheelchair_boarding":2},"id":"door-boyls-inbound","links":{"self":"/stops/door-boyls-inbound"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=door-boyls-inbound"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Boston Common, Street","latitude":42.353214,"location_type":2,"longitude":-71.064546,"municipality":"Boston","name":"Boylston - Boston Common, Street","platform_code":null,"platform_name":null,"wheelchair_boarding":2},"id":"door-boyls-outbound","links":{"self":"/stops/door-boyls-outbound"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=door-boyls-outbound"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"latitude":null,"long_name":"Boylston fare vending machine 201221","longitude":null,"properties":[{"name":"enclosed","value":1},{"name":"excludes-stop","value":"door-boyls-outbound"},{"name":"payment-form-accepted","value":"cash"},{"name":"payment-form-accepted","value":"credit-debit-card"}],"type":"FARE_VENDING_MACHINE"},"id":"fvm-201221","links":{"self":"/facilities/fvm-201221"},"relationships":{"stop":{"data":{"id":"place-boyls","type":"stop"}}},"type":"facility"},{"attributes":{"latitude":null,"long_name":"Boylston fare vending machine 201222","longitude":null,"properties":[{"name":"enclosed","value":1},{"name":"excludes-stop","value":"door-boyls-outbound"},{"name":"payment-form-accepted","value":"cash"},{"name":"payment-form-accepted","value":"credit-debit-card"}],"type":"FARE_VENDING_MACHINE"},"id":"fvm-201222","links":{"self":"/facilities/fvm-201222"},"relationships":{"stop":{"data":{"id":"place-boyls","type":"stop"}}},"type":"facility"},{"attributes":{"latitude":null,"long_name":"Boylston fare vending machine 201223","longitude":null,"properties":[{"name":"enclosed","value":1},{"name":"excludes-stop","value":"door-boyls-inbound"},{"name":"payment-form-accepted","value":"cash"},{"name":"payment-form-accepted","value":"credit-debit-card"}],"type":"FARE_VENDING_MACHINE"},"id":"fvm-201223","links":{"self":"/facilities/fvm-201223"},"relationships":{"stop":{"data":{"id":"place-boyls","type":"stop"}}},"type":"facility"},{"attributes":{"latitude":null,"long_name":"Boylston fare vending machine 201224","longitude":null,"properties":[{"name":"enclosed","value":1},{"name":"excludes-stop","value":"door-boyls-inbound"},{"name":"payment-form-accepted","value":"cash"},{"name":"payment-form-accepted","value":"credit-debit-card"}],"type":"FARE_VENDING_MACHINE"},"id":"fvm-201224","links":{"self":"/facilities/fvm-201224"},"relationships":{"stop":{"data":{"id":"place-boyls","type":"stop"}}},"type":"facility"},{"attributes":{"latitude":null,"long_name":"Boylston fare vending machine 202157","longitude":null,"properties":[{"name":"enclosed","value":1},{"name":"excludes-stop","value":"door-boyls-inbound"},{"name":"payment-form-accepted","value":"credit-debit-card"}],"type":"FARE_VENDING_MACHINE"},"id":"fvm-202157","links":{"self":"/facilities/fvm-202157"},"relationships":{"stop":{"data":{"id":"place-boyls","type":"stop"}}},"type":"facility"},{"attributes":{"address":null,"description":"Boylston - Paid side of fare gates","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-in-farepaid","links":{"self":"/stops/node-boyls-in-farepaid"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-in-farepaid"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Unpaid side of fare gates","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-in-fareunpaid","links":{"self":"/stops/node-boyls-in-fareunpaid"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-in-fareunpaid"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Bottom of Park Street & East stairs","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-instair-platform","links":{"self":"/stops/node-boyls-instair-platform"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-instair-platform"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Paid side of fare gates","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-out-farepaid","links":{"self":"/stops/node-boyls-out-farepaid"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-out-farepaid"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Unpaid side of fare gates","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-out-fareunpaid","links":{"self":"/stops/node-boyls-out-fareunpaid"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-out-fareunpaid"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"},{"attributes":{"address":null,"description":"Boylston - Bottom of Copley & West stairs","latitude":null,"location_type":3,"longitude":null,"municipality":"Boston","name":"Boylston","platform_code":null,"platform_name":null,"wheelchair_boarding":1},"id":"node-boyls-outstair-platform","links":{"self":"/stops/node-boyls-outstair-platform"},"relationships":{"facilities":{"links":{"related":"/facilities/?filter[stop]=node-boyls-outstair-platform"}},"parent_station":{"data":{"id":"place-boyls","type":"stop"}},"zone":{"data":null}},"type":"stop"}],"jsonapi":{"version":"1.0"}}
Loading

0 comments on commit 6f79f41

Please sign in to comment.