I wrote a lambda in python that connects to an http endpoint when an object is created in an S3 bucket. It works great. However, the customer has explained to me that there can be many endpoints, not just one, and the endpoint will be determined by the path in the S3 bucket. So, for example, the mapping is essentially like so:
[
{
"vendor1/inbound/": "vendor1service"
},
{
"vendor2/inbound/": "vendor2service"
},
[...]
{
"vendorN/inbound": "vendorNservice"
}
]
What would this data structure be called in python? Is it a list of dictionaries? Can you give me an example of code which would retrieve the endpoint, given the key?
The code you provided would indeed be classified as a list of dictionaries. However, I believe the middle-step of a list or multiple dictionaries is unnecessary. I would suggest just using a dictionary instead:
endpoints = {
"vendor1/inbound/": "vendor1service",
"vendor2/inbound/": "vendor2service",
"vendorN/inbound": "vendorNservice",
}
The endpoints can then be retrieved by:
print(endpoint["vendor1/inbound/"]) # prints "vendor1service"
Furthermore, if you are unable to modify the structure, you would have to loop over each list entry and check if the desired key is in the corresponding dicionary. See below:
endpoints = [
{
"vendor1/inbound/": "vendor1service"
},
{
"vendor2/inbound/": "vendor2service"
},
[...]
{
"vendorN/inbound": "vendorNservice"
}
]
key = "vendor2/inbound/"
for entry in endpoints:
if key in entry:
print(entry[key])
break