Adding county information to API#114
Conversation
There was a problem hiding this comment.
Damn, very well done! However, not sure I agree on the way the service is injected. I believe we'd be better off using a middleware to dynamically retrieve the data-source service.
E.g:
@api.before_request
def datasource():
"""
Attaches the datasource to the request.
"""
# Retrieve the datasource.
source = request.args.get('source', type=str, default='jhu')
# Attach source to request and return it.
request.source = services.locations.get_datasource(source)
passI am currently writing this right now.
|
Yeah, having a middleware would definitely be better. Feel free to let me know if you have any questions or other comments! |
|
I started some ground-work here: #115. |
|
Hello, @JKSenthil! It should now be done, if you still want to add your source, a couple of things: You're only allowed to return instances of from . import Location
class CbsLocation(Location):
"""
A CBS location.
"""
def __init__(self, id, country, state, county, coordinates, last_updated, confirmed, deaths):
super().__init__(
# General info.
id, 'US', state, coordinates, last_updated,
# Statistics.
confirmed=confirmed,
deaths=deaths,
recovered=0, # not provided
)
# State and county.
self.county = county
self.state = state
def serialize(self):
"""
Serializes the location into a dict.
:returns: The serialized location.
:rtype: dict
"""
serialized = super().serialize()
# Update with new fields.
serialized.update({
'state' : self.state,
'county': self.county,
})
# Return the serialized location.
return serializedThen simply create your service for retrieving the locations, and register it in I can work on implementing a more universal form for sending query params through to the services so we don't mess up the routes later. I just did it the easy way with Happy coding! I might write a guide on adding data-sources later, but this ^ is a quick summary of it. |
|
Great, thanks for the details! I'll get to work. |
ExpDev07
left a comment
There was a problem hiding this comment.
Well done :). I left a comment regarding the Last Update. To not confuse people, we should follow one format, which is UTC ISO (with an added 'Z' at the end for UTC).
| confirmed = int(item['Confirmed'] or 0) | ||
| death = int(item['Death'] or 0) | ||
| coordinates = Coordinates(float(item['Latitude']), float(item['Longitude'])) | ||
| last_update = item['Last Update'] |
There was a problem hiding this comment.
Should be parsed as ISO with an added 'Z' at the end. Use import datetime from datetime to achieve this.
|
I have finished up the csbs service and integrated it via the middleware, so everything is working. I think the only "issue" is that is |
Yes, that's fine. I'll fix it later. It's really just about checking if the |
|
Hey @all-contributors please add @JKSenthil for code, documentation, and tests. |
|
I've put up a pull request to add @JKSenthil! 🎉 |
County information is retrieved from CSBS (https://www.csbs.org/information-covid-19-coronavirus). Endpoint is
/v2/locations?source=csbs. Added unit tests and added documentation in readme for the county addition.