Reputation: 1093
I am trying to create a function in an Python API that should query an endpoint and get all items that contain a filter wildcard.
for example if I run
url = 'http://127.0.0.1:8000/api/data_product/'
response = requests.get(url, headers=headers)
I get all the items, i.e.
{
"count": 5,
"next": null,
"previous": null,
"results": [
{
"url": "http://127.0.0.1:8000/api/data_product/5/",
"internal_format": false,
"prov_report": "http://127.0.0.1:8000/api/prov-report/5/",
"last_updated": "2022-01-28T16:59:18.173266Z",
"name": "find/csv",
"version": "0.0.1",
"updated_by": "http://127.0.0.1:8000/api/users/1/",
"object": "http://127.0.0.1:8000/api/object/311/",
"namespace": "http://127.0.0.1:8000/api/namespace/3/",
"external_object": null
},
{
"url": "http://127.0.0.1:8000/api/data_product/4/",
"internal_format": false,
"prov_report": "http://127.0.0.1:8000/api/prov-report/4/",
"last_updated": "2022-01-24T11:21:49.541879Z",
"name": "test/csv",
"version": "0.0.1",
"updated_by": "http://127.0.0.1:8000/api/users/1/",
"object": "http://127.0.0.1:8000/api/object/34/",
"namespace": "http://127.0.0.1:8000/api/namespace/3/",
"external_object": null
},
...
I would like to find the item/s that contain a string. For example I would like to find that item whose name contains "find"
I tried
'http://127.0.0.1:8000/api/data_product/?name=find'
but returns 0 items found
Upvotes: 0
Views: 949
Reputation: 1107
If I understood well, you get all items as a string, which you can filter using python:
import json
name = "find"
results = json.dumps([obj for obj in json.loads(response)["results"] if obj["name"].startswith(name)], indent=4)
print(results)
Output:
[
{
"url": "http://127.0.0.1:8000/api/data_product/5/",
"internal_format": false,
"prov_report": "http://127.0.0.1:8000/api/prov-report/5/",
"last_updated": "2022-01-28T16:59:18.173266Z",
"name": "find/csv",
"version": "0.0.1",
"updated_by": "http://127.0.0.1:8000/api/users/1/",
"object": "http://127.0.0.1:8000/api/object/311/",
"namespace": "http://127.0.0.1:8000/api/namespace/3/",
"external_object": null
}
]
Upvotes: 1