77from __future__ import annotations
88
99import logging
10- from typing import TYPE_CHECKING , Literal , overload
10+ from typing import TYPE_CHECKING , Any , Literal , overload
1111
1212import httpx
1313
2121)
2222from re3data ._exceptions import RepositoryNotFoundError
2323from re3data ._response import Response , _build_response , _parse_repositories_response , _parse_repository_response
24+ from re3data ._serializer import _to_dict
2425
2526if TYPE_CHECKING :
2627 from re3data ._resources import Repository , RepositorySummary
@@ -48,16 +49,16 @@ def log_response(response: httpx.Response) -> None:
4849@overload
4950def _dispatch_return_type (
5051 response : Response , resource_type : Literal [ResourceType .REPOSITORY ], return_type : ReturnType
51- ) -> Repository | Response | str : ...
52+ ) -> Repository | Response | dict [ str , Any ] | str : ...
5253@overload
5354def _dispatch_return_type (
5455 response : Response , resource_type : Literal [ResourceType .REPOSITORY_LIST ], return_type : ReturnType
55- ) -> list [RepositorySummary ] | Response | str : ...
56+ ) -> list [RepositorySummary ] | Response | dict [ str , Any ] | str : ...
5657
5758
5859def _dispatch_return_type (
5960 response : Response , resource_type : ResourceType , return_type : ReturnType
60- ) -> Repository | list [RepositorySummary ] | Response | str :
61+ ) -> Repository | list [RepositorySummary ] | Response | dict [ str , Any ] | str :
6162 """Dispatch the response to the correct return type based on the provided return type and resource type.
6263
6364 Args:
@@ -67,16 +68,22 @@ def _dispatch_return_type(
6768
6869 Returns:
6970 Depending on the return_type and resource_type, this can be a Repository object, a list of RepositorySummary
70- objects, an HTTP response, or the original XML.
71+ objects, an HTTP response, a dictionary representation or the original XML.
7172 """
72- if return_type == ReturnType .DATACLASS :
73- if resource_type == ResourceType .REPOSITORY_LIST :
74- return _parse_repositories_response (response )
75- if resource_type == ResourceType .REPOSITORY :
76- return _parse_repository_response (response )
73+ if return_type == ReturnType .RESPONSE :
74+ return response
7775 if return_type == ReturnType .XML :
7876 return response .text
79- return response
77+
78+ parsed : Repository | list [RepositorySummary ]
79+ if resource_type == ResourceType .REPOSITORY_LIST :
80+ parsed = _parse_repositories_response (response )
81+ if resource_type == ResourceType .REPOSITORY :
82+ parsed = _parse_repository_response (response )
83+
84+ if return_type == ReturnType .DATACLASS :
85+ return parsed
86+ return _to_dict (parsed )
8087
8188
8289class RepositoryManager :
@@ -91,7 +98,7 @@ def __init__(self, client: Client) -> None:
9198
9299 def list (
93100 self , query : str | None = None , return_type : ReturnType = ReturnType .DATACLASS
94- ) -> list [RepositorySummary ] | Response | str :
101+ ) -> list [RepositorySummary ] | Response | dict [ str , Any ] | str :
95102 """List the metadata of all repositories in the re3data API.
96103
97104 Args:
@@ -101,7 +108,7 @@ def list(
101108
102109 Returns:
103110 Depending on the `return_type`, this can be a list of RepositorySummary objects, an HTTP response,
104- or the original XML.
111+ a dictionary representation or the original XML.
105112
106113 Raises:
107114 ValueError: If an invalid `return_type` is provided.
@@ -112,15 +119,18 @@ def list(
112119 response = self ._client ._request (Endpoint .REPOSITORY_LIST .value , query_params )
113120 return _dispatch_return_type (response , ResourceType .REPOSITORY_LIST , return_type )
114121
115- def get (self , repository_id : str , return_type : ReturnType = ReturnType .DATACLASS ) -> Repository | Response | str :
122+ def get (
123+ self , repository_id : str , return_type : ReturnType = ReturnType .DATACLASS
124+ ) -> Repository | Response | dict [str , Any ] | str :
116125 """Get the metadata of a specific repository.
117126
118127 Args:
119128 repository_id: The identifier of the repository to retrieve.
120129 return_type: The desired return type for the API resource. Defaults to `ReturnType.DATACLASS`.
121130
122131 Returns:
123- Depending on the `return_type`, this can be a Repository object, an HTTP response, or the original XML.
132+ Depending on the `return_type`, this can be a Repository object, an HTTP response,
133+ a dictionary representation or the original XML.
124134
125135 Raises:
126136 ValueError: If an invalid `return_type` is provided.
0 commit comments