@@ -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
0 commit comments