Skip to main content

jrequests

jrequests is a fluent Python library aiming to replicate the following:

  • Java's Jackson-like REST response to objects deserialization

The library is implemented with type safety in mind.

Installation

Use the package manager pip to install jrequests.

pip install jrequests

Usage

jrequests maps API call responses to objects or list of objects of the same type. It is based on the requests library and supports all parameters supported by the requests library. Currently, jrequests only supports the first level of objects, so stacked objects will instead be treated as dicts.

jrequests supports 3 types of data injection:

  • direct attribute injection (works only with public attributes)
  • dataclass fields injection (works with dataclasses with the restriction that all dataclass needs to have a no argument constructor)
  • setter injection (works with classes that use methods such as 'setValue' or 'set_value' for the named fields parameters)

Getting a single object from an API call

from dataclasses import field, dataclass
from typing import Any, Optional
from jrequests import RequestBuilder, RequestMethod


@dataclass
class DeviceData:
    # Important to note that in order for the libary to instantiate, then set the values properly,
    # there needs to be a no argument constructor
    id: Optional[str] = field(init=False)
    name: Optional[str] = field(init=False)
    data: Optional[dict[Any, Any]] = field(init=False)


def main() -> None:
    response = (
        RequestBuilder(RequestMethod.GET, "https://api.restful-api.dev/objects/1")
        .withRetries(3)
        .withHeaders({"Content-Type": "application/json"})
        .execute()
        .getObject(DeviceData)
    )
    print(f"Device ID: {response.id}")
    print(f"Device Name: {response.name}")
    print(f"Device Data: {response.data}")


if __name__ == "__main__":
    main()

Getting a list of objects from an API call

from typing import Any, Optional
from jrequests import RequestBuilder, RequestMethod


class DeviceData:
    # Important to note that in order for the libary to instantiate, then set the values properly,
    # there needs to be a no argument constructor
    def __init__(self) -> None:
        self.__id: Optional[str] = None
        self.__name: Optional[str] = None
        self.__data: Optional[dict[Any, Any]] = None

    def setId(self, id: str) -> None:
        self.__id = id

    def getId(self) -> Optional[str]:
        return self.__id

    def setName(self, name: str) -> None:
        self.__name = name

    def getName(self) -> Optional[str]:
        return self.__name

    def setData(self, data: dict[Any, Any]) -> None:
        self.__data = data

    def getData(self) -> Optional[dict[Any, Any]]:
        return self.__data


def main() -> None:
    response = (
        RequestBuilder(RequestMethod.GET, "https://api.restful-api.dev/objects")
        .withRetries(3)
        .withHeaders({"Content-Type": "application/json"})
        .execute()
        .getList(DeviceData)
    )

    for elem in response:
        print(f"Device ID: {elem.getId()}")
        print(f"Device Name: {elem.getName()}")
        print(f"Device Data: {elem.getData()}")
        print("========")


if __name__ == "__main__":
    main()

License

MIT

Metadata

Release files for jrequests 2025.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jrequests 2025.2.1
File Size Uploaded
jrequests-2025.2.1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jrequests 2025.2.1
File Interpreter ABI Platform
jrequests-2025.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / jrequests-2025.2.1.tar.gz

Download URL jrequests-2025.2.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
16afe21ad722b68825375b05b2a0c0e9560788dd5e91a433710bb2023eb8258e
BLAKE2b-256 checksum
How to use checksums
bd6b67ac795ada62dafe94d86d88f3226234be508b6e688741a5e0add318ef5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / jrequests-2025.2.1-py3-none-any.whl

Download URL jrequests-2025.2.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b96889527c46d7390b9c2cbbf2fcdcecea910b60785eadc5a1f3cf07525077b9
BLAKE2b-256 checksum
How to use checksums
9029aec6a45196199fa42b489562e2da59f7df9d6afcf9ba3ac345d413832a20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2025.2.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page