Skip to content

Commit 82fe5fa

Browse files
committed
add support for filtering feed ids in the gbfs batch function
1 parent 0e6d2f3 commit 82fe5fa

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

functions-python/gbfs_validator/src/main.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ def gbfs_validator_pubsub(cloud_event: CloudEvent):
101101

102102
@with_db_session
103103
@functions_framework.http
104-
def gbfs_validator_batch(_, db_session: Session):
104+
def gbfs_validator_batch(request, db_session: Session):
105105
"""
106-
HTTP Cloud Function to trigger the GBFS Validator function for multiple datasets.
106+
HTTP Cloud Function to trigger the GBFS Validator function for multiple datasets.
107+
When the request is a POST request with a JSON body containing `feed_ids`,
108+
it processes only those feeds. Otherwise, it processes all feeds in the database.
107109
@param _: The request object.
108110
@return: The response of the function.
109111
"""
@@ -113,9 +115,23 @@ def gbfs_validator_batch(_, db_session: Session):
113115
logging.error("PUBSUB_TOPIC_NAME environment variable not set.")
114116
return "PUBSUB_TOPIC_NAME environment variable not set.", 500
115117

116-
# Get all GBFS feeds from the database
117118
try:
118-
gbfs_feeds = fetch_all_gbfs_feeds(db_session)
119+
feed_ids = None
120+
if request and request.method == "POST" and request.is_json:
121+
request_json = request.get_json()
122+
feed_ids = request_json.get("feed_ids") if request_json else None
123+
else:
124+
logging.info("Request body not provided or not a valid JSON.")
125+
except Exception as e:
126+
logging.error("Error parsing request body: %s", e)
127+
return "Invalid request body.", 400
128+
129+
try:
130+
if feed_ids:
131+
gbfs_feeds = fetch_gbfs_feeds_by_ids(db_session, feed_ids)
132+
else:
133+
# Get all GBFS feeds from the database
134+
gbfs_feeds = fetch_all_gbfs_feeds(db_session)
119135
except Exception:
120136
return "Error getting all GBFS feeds.", 500
121137

@@ -150,3 +166,13 @@ def gbfs_validator_batch(_, db_session: Session):
150166
f"GBFS Validator batch function triggered successfully for {len(feeds_data)} feeds.",
151167
200,
152168
)
169+
170+
171+
def fetch_gbfs_feeds_by_ids(db_session, feed_ids):
172+
"""Fetch GBFS feeds by their IDs from the database."""
173+
gbfs_feeds = (
174+
db_session.query(Gbfsfeed)
175+
.filter(Gbfsfeed.id.in_(feed_ids), Gbfsfeed.status != "deprecated")
176+
.all()
177+
)
178+
return gbfs_feeds

functions-python/gbfs_validator/tests/test_gbfs_validator.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,40 @@ def test_gbfs_validator_batch_publish_exception(
179179
# Call the function
180180
result = gbfs_validator_batch(None)
181181
self.assertEqual(result[1], 500)
182+
183+
@patch.dict(
184+
os.environ,
185+
{
186+
"PUBSUB_TOPIC_NAME": "mock-topic",
187+
},
188+
)
189+
@patch("main.pubsub_v1.PublisherClient")
190+
@patch("main.fetch_gbfs_feeds_by_ids")
191+
def test_gbfs_validator_batch_by_feed_ids(
192+
self, mock_fetch_gbfs_feeds_by_ids, mock_publisher_client
193+
):
194+
# Prepare mocks
195+
mock_session = MagicMock()
196+
# mock_database.return_value.start_db_session.return_value = mock_session
197+
198+
mock_publisher = MagicMock()
199+
mock_publisher_client.return_value = mock_publisher
200+
201+
mock_feed = MagicMock()
202+
mock_feed.stable_id = "mock-stable-id"
203+
mock_feed.id = str(uuid.uuid4())
204+
mock_feed.auto_discovery_url = "http://mock-url.com"
205+
mock_feed.gbfsversions = [MagicMock(version="1.0")]
206+
mock_feed_2 = copy.deepcopy(mock_feed)
207+
mock_feed_2.gbfsversions = []
208+
mock_fetch_gbfs_feeds_by_ids.return_value = [mock_feed, mock_feed_2]
209+
request = MagicMock()
210+
request.method = "POST"
211+
request.is_json = True
212+
request.get_json.return_value = {"feed_ids": [mock_feed.id, mock_feed_2.id]}
213+
# Call the function
214+
result = gbfs_validator_batch(request, db_session=mock_session)
215+
self.assertEqual(result[1], 200)
216+
217+
mock_fetch_gbfs_feeds_by_ids.assert_called_once()
218+
self.assertEqual(mock_publisher.publish.call_count, 2)

0 commit comments

Comments
 (0)