Skip to content

Commit 8622c87

Browse files
authored
Merge pull request #81 from edanalytics/feature/skip_when_query_key_not_in_endpoint_params
warn and skip fetching endpoints if query keys are not valid parameters
2 parents 89eadb1 + df51e61 commit 8622c87

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

lightbeam/fetch.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,24 @@ async def get_records(self, do_write=True, log_status_counts=True):
2525
limit = self.lightbeam.config["fetch"]["page_size"]
2626
params = json.loads(self.lightbeam.query)
2727
for endpoint in self.lightbeam.endpoints:
28+
29+
# test the query for invalid params (otherwise a typo will
30+
# return _all_ records, which might be bad!)
31+
if self.lightbeam.query != '':
32+
self.lightbeam.api.load_swagger_docs()
33+
swagger = self.lightbeam.api.resources_swagger
34+
namespace = self.lightbeam.config["namespace"]
35+
supported_params = swagger.get("paths", {}).get(f"/{namespace}/{endpoint}", {}).get("get", {}).get("parameters", [])
36+
supported_param_names = [ x["name"] for x in supported_params if "name" in x.keys() and "in" in x.keys() and x["in"]=="query" ]
37+
if not set(params.keys()).issubset(set(supported_param_names)):
38+
self.logger.warn(f"Query contains keys that are not params for the endpoint {endpoint}... skipping! (Supported params: {(', '.join(supported_param_names))})")
39+
continue
40+
2841
# figure out how many (paginated) requests we must make
2942
tasks.append(asyncio.create_task(self.lightbeam.counter.get_record_count(endpoint, params)))
30-
await self.lightbeam.do_tasks(tasks, counter, log_status_counts=log_status_counts)
43+
44+
if len(tasks) > 0:
45+
await self.lightbeam.do_tasks(tasks, counter, log_status_counts=log_status_counts)
3146

3247
tasks = []
3348
record_counts = self.lightbeam.results

0 commit comments

Comments
 (0)