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

69 add endpoints to retrieve from look up data tables #74

Merged

Conversation

jcadam14
Copy link
Contributor

@jcadam14 jcadam14 commented Dec 28, 2023

Closes #69

Added institutions/address-states, institutions/regulators, and institutions/types/{type} endpoints

Went with a 'generic' InstitutionTypeDTO (since both sbl and hmda tables just have id and name). The /types/ endpoint uses a type Literal to limit the accepted types to sbl or hmda. It then queries the specific Dao type. If this approach isn't desired I can break that GET into two separate types/sbl and types/hmda endpoints that return the specific DTO type.

Updated pytests to test these endpoints and the corresponding get_* functions added to the institution repo.

…ndpoint

Adds endpoints for getting address-states and regulators
Adds institution repo get_ functions for the above
Let commented out code in for specific Dtos in case the general approach isn't desired
@jcadam14 jcadam14 self-assigned this Dec 28, 2023
@jcadam14 jcadam14 linked an issue Dec 28, 2023 that may be closed by this pull request
Copy link

github-actions bot commented Dec 28, 2023

Coverage report

The coverage rate went from 86.53% to 87.42% ⬆️
The branch rate is 87%.

100% of new lines are covered.

Diff Coverage details (click to unfold)

src/entities/models/init.py

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

src/entities/models/dao.py

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

src/entities/models/dto.py

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

src/entities/repos/institutions_repo.py

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

src/entities/repos/repo_utils.py

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

src/routers/institutions.py

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

Comment on lines 53 to 56
async with session.begin():
stmt = select(SBLInstitutionTypeDao)
res = await session.scalars(stmt)
return res.all()
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 abstract this portion out, since it's repeated across all of the lookups

Copy link
Collaborator

Choose a reason for hiding this comment

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

or better yet, just have something like get_look_up(session..., T) -> List[T] or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a repo_utiils with the following

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from typing import List, TypeVar

T = TypeVar("T")


async def query_type(session: AsyncSession, type: T) -> List[T]:
    async with session.begin():
        stmt = select(type)
        res = await session.scalars(stmt)
        return res.all()

I thought about putting that directly in the institution_repo and having the routers call it, but as it stands the routers don't "know" about DAOs, just DTOs, so I kept it separate. And the institution_repo now has:

async def get_sbl_types(session: AsyncSession) -> List[SBLInstitutionTypeDao]:
    return await query_type(session, SBLInstitutionTypeDao)


async def get_hmda_types(session: AsyncSession) -> List[HMDAInstitutionTypeDao]:
    return await query_type(session, HMDAInstitutionTypeDao)


async def get_address_states(session: AsyncSession) -> List[AddressStateDao]:
    return await query_type(session, AddressStateDao)


async def get_federal_regulators(session: AsyncSession) -> List[FederalRegulatorDao]:
    return await query_type(session, FederalRegulatorDao)

which again is somewhat repetitive, but less so than before.

@@ -45,6 +49,34 @@ async def get_institution(session: AsyncSession, lei: str) -> FinancialInstituti
return await session.scalar(stmt)


async def get_sbl_types(session: AsyncSession) -> SBLInstitutionTypeDao:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The return type should be a collection, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, updated all these funcs to return List[type]

Comment on lines 76 to 79
if type == "sbl":
return await repo.get_sbl_types(request.state.db_session)
else:
return await repo.get_hmda_types(request.state.db_session)
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 be explicit with hmda as well, and throw invalid arg exception when neither matches

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh I see, the Literal would take care of that, in that case I'd still like hmda to be explicit, so maybe use match

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to

    match type:
        case "sbl":
            return await repo.get_sbl_types(request.state.db_session)
        case "hmda":
            return await repo.get_hmda_types(request.state.db_session)

Comment on lines 84 to 89
class HMDAInstitutionTypeDto(InstitutionTypeDto):
pass


class SBLInstitutionTypeDto(SBLInstitutionTypeBase):
name: str

class Config:
from_attributes = True
class SBLInstitutionTypeDto(InstitutionTypeDto):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

now looking at it, just one dto would work for both, so we can completely remove the distinct ones, at least until the need arises when differences come up later. we can probably also update the dao to have one base dao, and the two specific ones inherit from that, and only change the __tablename__

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed commented out DTO references. Added a InstitutionTypeMixin that the HMDA and SBL Inst. Types inherit. Couldn't see a cleaner way to add inheritance here.

Removed commented out code
Added a repo_utils with a func that contains a genericized query
Added an InstitutionTypeMixin to contain duplicated fields between HMDA and SBL
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

@lchen-2101 lchen-2101 merged commit 3bef614 into main Jan 4, 2024
3 checks passed
@lchen-2101 lchen-2101 deleted the 69-add-endpoints-to-retrieve-from-look-up-data-tables branch January 4, 2024 19:01
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.

Add endpoints to retrieve from look up data tables
2 participants