Skip to content
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

Added endpoint to return lei data from list of leis #20

Merged
merged 15 commits into from
Sep 15, 2023

Conversation

guffee23
Copy link
Contributor

@guffee23 guffee23 commented Sep 1, 2023

Added endpoint to return lei data from request containing list of leis. Added repo to gather data from db. Added simple error handling for when none of leis from list are found.

Added endpoint to return lei data from request containing list of leis. Added repo to gather data from db. Added simple error handling for when none of leis from list are found.
@guffee23
Copy link
Contributor Author

guffee23 commented Sep 1, 2023

Closes #13

@lchen-2101
Copy link
Collaborator

also, let's start adding tests to feature development instead of just worrying about tests later.

from fastapi import Query


def parse_leis(leis: List[str] = Query(None)) -> Optional[List]:
Copy link
Collaborator

@lchen-2101 lchen-2101 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're using Query which does allow the flexibility of allowing users to do the leis=test1&leis=test2 method; let's make this dependency flexible as well, so that if len(leis) is more than 1, just return the leis, otherwise do the comma separated list parsing.

* fix: ignore issuer for jwt (#22)

* Changed parser to handle different combinations

Removed unnecessary join section in repo call. Reformatted get institution request param order. Added a flatmap function to parser and changed lei handling to account for different input combinations.

---------

Co-authored-by: lchen-2101 <[email protected]>
Copy link
Member

@hkeeler hkeeler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff. Added a few questions and suggestions.

src/oauth2/oauth2_admin.py Outdated Show resolved Hide resolved
Comment on lines 21 to 27
if leis is None or len(leis) == 0:
return []
else:
lei_list = []
for i in leis:
lei_list.append(i.split(','))
return flat_map(lambda x: x, lei_list)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could reduce (pun intended) this to:

if leis:
    return list(chain.from_iterable([x.split(',') for x in leis]))
else:
    return []

See: itertools.chain.from_iterable

...and that'd let you drop flat_map() as well.

from fastapi import Query


def parse_leis(leis: List[str] = Query(None)) -> Optional[List]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem LEI-specific. I'm guessing we'll have additional multi-value query parameters. Perhaps this could be csv_to_list? Or can we not make it generic easily since the request parameter is named leis?

Also, there's some interesting discussion on this topic: fastapi/fastapi#8225

I like the one that creates a custom CommaSeparated[T] type, but there's a lot of metaclass magic going on there, and it's too late in the day for me to absorb it. I think if we end up doing this a lot, it'll be worth reevaluating how we handle these types of params.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename this to src/dependencies.py, and use it for any shared FastAPI Dependencies.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually have a src/dependencies.py created in the denied domain PR: #21, so maybe get that merged first to avoid conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or can we not make it generic easily since the request parameter is named leis?

The naming convention was due to the request parameter naming.

I think if we end up doing this a lot, it'll be worth reevaluating how we handle these types of params.

Completely agree with this. @hkeeler would there be issue with leaving it this way for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK by me. I mean, I guess it depends on which PR gets merged first. 😉 If #21 get merged, then you should move this into that new file here. If PR goes first, then @lchen-2101 should incorporate it. Tick tock! ⏱️

from fastapi import Query


def parse_leis(leis: List[str] = Query(None)) -> Optional[List]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the return type is Optional[List], but it seems like a list is always returned, just empty if leis is not set. Seems like we should make that consistent.

async def test_get_institutions_by_lei_list(self, session: AsyncSession):
res = await repo.get_institutions(session, leis=['TESTBANK123'])
assert len(res) == 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test scenarios with multiple LEIs. Probably want scenarios where all requested values are present, and where only some are present. Looks like that'll require adding at least one more FI instance to the above fixture.

@@ -13,7 +13,7 @@


async def get_institutions(
session: AsyncSession, domain: str = "", page: int = 0, count: int = 100
session: AsyncSession, domain: str = "", page: int = 0, count: int = 100, leis: List[str] = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's order the args here to match the api endpoint, better readability on the precedence.

Copy link
Collaborator

@lchen-2101 lchen-2101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions
Copy link

github-actions bot commented Sep 14, 2023

Coverage report

The coverage rate went from 82.47% to 82.21% ⬇️
The branch rate is 83%.

91.66% of new lines are covered.

Diff Coverage details (click to unfold)

src/routers/institutions.py

100% of new lines are covered (95.45% of the complete file).

src/entities/repos/institutions_repo.py

100% of new lines are covered (94.28% of the complete file).

src/dependencies.py

85.71% of new lines are covered (85.71% of the complete file).
Missing lines: 54

@guffee23 guffee23 merged commit 29f1137 into main Sep 15, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants