Skip to content

Get rid of get_list_unique and replace it with get_list(limit=1) #78

Description

@pederhan

This is mostly a brain dump to document the ...unique... nature of get_list_unique and how introduces certain anti-patterns. It's extremely low priority to "fix", but it's nice to be aware of the fact that only 3 methods use it (CommunityManager removal TBD)


Uncommonly used - irregular consumer patterns

I'm not sure if it makes sense to be so strict about how we go about trying to fetch single resources via listing resources with specific filters, like we do when we call get_list_unique. Wherever we use this method, we introduce idiosyncrasies like manually calling self.model.model_validate(obj) which to me is a code smell, because it replaces the regular means by which we construct the manager's model type.

Locations it is used:

CommunityManager.get_by_name:

mreg-api/mreg_api/managers.py

Lines 2166 to 2171 in 2fae06d

if isinstance(network, str):
resp = self._client.get_list_unique(
Endpoint.NetworkCommunities.with_params(network), params={"name": name}
)
if resp is not None:
return Community.model_validate(resp)

MXManager.get_unique:

mreg-api/mreg_api/managers.py

Lines 3148 to 3154 in 2fae06d

obj = self._client.get_list_unique(
self.endpoint,
params={"host": str(host_id), "mx": mx, "priority": str(priority)},
ok404=True,
)
if obj:
return MX.model_validate(obj)

NAPTRManager.get_unique:

mreg-api/mreg_api/managers.py

Lines 3332 to 3346 in 2fae06d

obj = self._client.get_list_unique(
self.endpoint,
params={
"host": str(host_id),
"preference": preference,
"order": order,
"flag": flag,
"service": service,
"regex": regex,
"replacement": replacement,
},
ok404=True,
)
if obj:
return NAPTR.model_validate(obj)

SRVManager.get_unique:

mreg-api/mreg_api/managers.py

Lines 3413 to 3424 in 2fae06d

obj = self._client.get_list_unique(
self.endpoint,
params={
"name": name,
"priority": priority,
"weight": weight,
"port": port,
"host": host_id,
},
)
if obj:
return Srv.model_validate(obj)

Arguments for keeping it

If we genuinely want to raise on multiple hits from get_unique calls, we should keep this method. Realistically, I don't really think it should be possible to get multiple hits unless the server introduces additional fields for these models that we don't filter on in get_unique.

Replace with get_list(limit=1)

We should consider getting rid of get_list_unique internally and replace it with get_list(limit=1), which will automatically emit a truncation event the CLI can catch.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions