Skip to main content

A package providing Java's Jackson-like object mapping from REST request responses.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jrequests-2025.2.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jrequests-2025.2.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file jrequests-2025.2.1.tar.gz.

File metadata

  • Download URL: jrequests-2025.2.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for jrequests-2025.2.1.tar.gz
Algorithm Hash digest
SHA256 16afe21ad722b68825375b05b2a0c0e9560788dd5e91a433710bb2023eb8258e
MD5 76b42cdd38774022696ef6a9a1e70554
BLAKE2b-256 bd6b67ac795ada62dafe94d86d88f3226234be508b6e688741a5e0add318ef5b

See more details on using hashes here.

File details

Details for the file jrequests-2025.2.1-py3-none-any.whl.

File metadata

  • Download URL: jrequests-2025.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for jrequests-2025.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b96889527c46d7390b9c2cbbf2fcdcecea910b60785eadc5a1f3cf07525077b9
MD5 c4cbc2467c55f0cd83a0a513fb5ccefc
BLAKE2b-256 9029aec6a45196199fa42b489562e2da59f7df9d6afcf9ba3ac345d413832a20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page