pmb.helpers.http: Simplify retrieve_json() (MR 2490)

The parameters of this function were needlessly complicated. While I
understand the intent of trying to replicate the API of retrieve()
without duplicating the parameter list, in practice this feels like
reducing duplication to a fault to me. This is due to that the third
parameter, <allow_404>, doesn't make sense in the context of
retrieve_json() as it would raise an exception[1] if retrieve() returns
None (which is the effect of <allow_404> when a 404 occurs), thus merely
changing the exception that gets raised to a less descriptive one
instead of actually behaving like retrieve() would with that parameter
set to True.

With <allow_404> eliminated, there's just two parameters left, and I
don't think duplicating those is a big deal. So make the function easier
to read and annotate by getting rid of the args/kwargs logic.

 [1]: json.loads() raises an exception if its first argument is None
This commit is contained in:
Newbyte 2024-11-15 11:38:46 +01:00
parent 9f433b2b1d
commit c8bd5abde1
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB

View file

@ -7,7 +7,7 @@ import os
from pathlib import Path from pathlib import Path
import shutil import shutil
import urllib.request import urllib.request
from typing import Literal, overload from typing import Any, Literal, overload
import pmb.helpers.cli import pmb.helpers.cli
from pmb.core.context import get_context from pmb.core.context import get_context
@ -143,9 +143,9 @@ def retrieve(
raise raise
def retrieve_json(*args, **kwargs): def retrieve_json(url: str, headers: dict[str, str] | None = None) -> Any:
"""Fetch the contents of a URL, parse it as JSON and return it. """Fetch the contents of a URL, parse it as JSON and return it.
See retrieve() for the list of all parameters. See retrieve() for the meaning of the parameters.
""" """
return json.loads(retrieve(*args, **kwargs)) return json.loads(retrieve(url, headers, False))