Coverage for src/paperap/utils.py: 0%

1"""

2----------------------------------------------------------------------------

3

4 METADATA:

5

6 File: utils.py

7 Project: paperap

8 Created: 2025-03-04

9 Version: 0.0.5

10 Author: Jess Mann

11 Email: jess@jmann.me

12 Copyright (c) 2025 Jess Mann

13

14----------------------------------------------------------------------------

15

16 LAST MODIFIED:

17

18 2025-03-04 By Jess Mann

19

20"""

21

22 from __future__ import annotations

23

24 import json

25 import os

26 from datetime import datetime

27 from typing import Any, BinaryIO, Optional

28

29

30 def datetime_to_str(dt: datetime) -> str:

31 """

32 Convert a datetime object to an ISO 8601 string for the API.

33

34 Args:

35 dt: The datetime object to convert.

36

37 Returns:

38 ISO 8601 formatted string.

39

40 """

41 if dt is None:

42 return None

43 return dt.isoformat().replace("+00:00", "Z")

44

45

46 def parse_filter_params(**kwargs: Any) -> dict[str, Any]:

47 """

48 Parse filter parameters for list endpoints.

49

50 Args:

51 **kwargs: Filter parameters.

52

53 Returns:

54 Dictionary of filter parameters.

55

56 """

57 filters: dict[str, Any] = {}

58 for key, value in kwargs.items():

59 if value is not None:

60 if isinstance(value, datetime):

61 filters[key] = datetime_to_str(value)

62 elif isinstance(value, list):

63 # Handle list parameters like tags__id__in

64 filters[key] = ",".join([str(v) for v in value])

65 else:

66 filters[key] = value

67 return filters