-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a /entities
endpoint to the API
#714
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍 !
We're finally one step closer from documenting the entities in the legislation explorer 🎉
CHANGELOG.md
Outdated
- Introduce the `/entities` endpoint for the Web API | ||
```json | ||
{ | ||
"description": "[...]", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make up a simple description? Examples will real values are always easier to grasp 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, in my previous comment I thought description was the label, now I understand better why you didn't want to write the full Household
doc.
But I still think a one or two-sentence max doc would hep :)
openfisca_web_api/loader/entities.py
Outdated
|
||
entity_formated = { | ||
'plural': entity.plural, | ||
'description': to_unicode(entity.doc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#561 mentioned a documentation
key containing the docstring.
If we are to have a description
key, it should probably contain entity.label
openfisca_web_api/loader/entities.py
Outdated
'description': to_unicode(entity.doc) | ||
} | ||
if hasattr(entity, 'roles'): | ||
entity_formated['roles'] = \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid \
for line continuation: you can rely on implicit continuation, as you've done everywhere else 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, pycharm outsmarted me. ;)
openfisca_web_api/loader/entities.py
Outdated
'plural': entity.plural, | ||
'description': to_unicode(entity.doc) | ||
} | ||
if hasattr(entity, 'roles'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use:
if entity.is_person:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't a group entity not have roles ?
it seems to me that if entity.is_person:
would give me all group entities whether they have roles
or not 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point.
We have never used a group entity without roles, and right now it would trigger an ugly error, but you're right, there could be use cases.
However, even if we had a group entity without roles, it should still have an attribute roles
that would be []
, so the code further down would still work. In general, using hasattr
is pretty rare unless you are doing relatively tricky stuff.
openfisca_web_api/loader/entities.py
Outdated
if role.max: | ||
role_formated['max'] = role.max | ||
if role.subroles: | ||
role_formated['max'] = len(role.subroles) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
openfisca_web_api/loader/entities.py
Outdated
if role.subroles: | ||
role_formated['max'] = len(role.subroles) | ||
|
||
role_formated['mandatory'] = True if role_formated.get('max') else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there is no good reason to assume than a role with a max is a mandatory role.
Right now, we don't handle mandatory role in OpenFisca Core. I see two solutions to deal with that:
- Always set
role_formated['mandatory']
toFalse
(the Web API would be "anticipating" the feature) - Open another PR in Core introducing this
mandatory
attribute in Python
1 allows for a quick delivery, but leads to a relatively inconsistent state of the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented 1 and opened #721 to follow 2
openfisca_web_api/openAPI.yml
Outdated
@@ -16,13 +16,18 @@ tags: | |||
- name: "Parameters" | |||
description: "A parameter is a numeric property of the legislation that can evolve over time." | |||
externalDocs: | |||
description: "Parameter official documentation" | |||
description: "Official Parameter documentation" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not we sure what we mean by "official". Never seen an fan-based documentation so far, nor an off-brand one ;)
openfisca_web_api/loader/entities.py
Outdated
|
||
def build_entity(entity): | ||
|
||
entity_formated = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatted_entity
?
tests/web_api/test_entities.py
Outdated
|
||
def test_response_data(): | ||
entities = json.loads(entities_response.data.decode('utf-8')) | ||
test_description = ''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy-pasting the description makes the test really fragile. A minor edition of the doc would break it.
I'd recommend importing Household
from the country template and getting the doc from Household.doc
tests/web_api/test_entities.py
Outdated
'roles': { | ||
'parent': { | ||
'description': 'The one or two adults in charge of the household.', | ||
'mandatory': True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably stick to False
unless we actually handle mandatory roles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, there is a couple of things I missed during my first review, but we are almost there :) !
openfisca_web_api/openAPI.yml
Outdated
additionalProperties: | ||
type: "object" | ||
properties: | ||
definition: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't think there is a definition
property for our entities object. We should also document the following properties:
description
mandatory
max
plural
documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and roles
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, forgot that one 😓
openfisca_web_api/loader/entities.py
Outdated
if role.subroles: | ||
formatted_role['max'] = len(role.subroles) | ||
|
||
formatted_role['mandatory'] = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about it, the least confortable I feel about this mandatory
property.
For instance, are we sure the following
max: 2
mandatory: true
is clearer than
max:2
min:1
?
I'd be in favor of removing that property and postponing this discussion to #721
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally agree
openfisca_web_api/loader/entities.py
Outdated
formatted_entity = { | ||
'plural': entity.plural, | ||
'description': to_unicode(entity.label), | ||
'documentation': to_unicode(entity.doc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
documentation
seems to often start and end with \n
, which is not really relevant. We should probably clean these :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a way but it is not very elegant.... I'm looking forward to your feedback on it :)
openfisca_web_api/loader/entities.py
Outdated
split_doc = to_unicode(entity.doc).split("\n") | ||
formatted_start_doc = split_doc[1:] if split_doc[0] == "" else split_doc | ||
formatted_end_doc = formatted_start_doc[:-1] if formatted_start_doc[-1] == "" else formatted_start_doc | ||
formatted_doc = "\n".join(formatted_end_doc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just use formatted_doc = to_unicode(entity.doc.strip())
strip
removes all trailing white spaces, and \n
is considered as a particular white space 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... 🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build_entity
should be simplified, but GTM otherwise 🙂
Fixes #561
New features
/entities
endpoint for the Web API