1313
1414from re3data import __version__
1515from re3data ._response import Response , _count_repositories , _parse_repositories_response , _parse_repository_response
16- from re3data ._serializer import _to_dict , _to_json
16+ from re3data ._serializer import _to_csv , _to_dataframe , _to_dict , _to_json
1717
1818if TYPE_CHECKING :
19+ from pandas import DataFrame
20+
1921 from re3data ._resources import Repository , RepositorySummary
2022
2123BASE_URL : str = "https://www.re3data.org/api/beta/"
@@ -37,7 +39,9 @@ class ResourceType(str, Enum):
3739
3840
3941class ReturnType (str , Enum ):
42+ CSV = "csv"
4043 DATACLASS = "dataclass"
44+ DATAFRAME = "dataframe"
4145 DICT = "dict"
4246 JSON = "json"
4347 RESPONSE = "response"
@@ -80,19 +84,19 @@ def _build_query_params(query: str | None = None) -> dict[str, str]:
8084@overload
8185def _dispatch_return_type (
8286 response : Response , resource_type : Literal [ResourceType .REPOSITORY ], return_type : ReturnType , count : bool = False
83- ) -> Repository | Response | dict [str , Any ] | str : ...
87+ ) -> Repository | Response | dict [str , Any ] | DataFrame | str : ...
8488@overload
8589def _dispatch_return_type (
8690 response : Response ,
8791 resource_type : Literal [ResourceType .REPOSITORY_LIST ],
8892 return_type : ReturnType ,
8993 count : bool = False ,
90- ) -> list [RepositorySummary ] | Response | dict [str , Any ] | str | int : ...
94+ ) -> list [RepositorySummary ] | Response | dict [str , Any ] | DataFrame | str | int : ...
9195
9296
93- def _dispatch_return_type (
97+ def _dispatch_return_type ( # noqa: PLR0911
9498 response : Response , resource_type : ResourceType , return_type : ReturnType , count : bool = False
95- ) -> Repository | list [RepositorySummary ] | Response | dict [str , Any ] | str | int :
99+ ) -> Repository | list [RepositorySummary ] | Response | dict [str , Any ] | DataFrame | str | int :
96100 """Dispatch the response to the correct return type based on the provided return type and resource type.
97101
98102 Args:
@@ -105,14 +109,15 @@ def _dispatch_return_type(
105109 Depending on the return_type and resource_type, this can be a Repository object, a list of RepositorySummary
106110 objects, an HTTP response, a dictionary representation or the original XML.
107111 """
112+ # return the count of repositories, the response or the original xml before parsing the response
108113 if resource_type == ResourceType .REPOSITORY_LIST and count :
109114 return _count_repositories (response .text )
110-
111115 if return_type == ReturnType .RESPONSE :
112116 return response
113117 if return_type == ReturnType .XML :
114118 return response .text
115119
120+ # all subsequent return types rely on parsing the response first
116121 parsed : Repository | list [RepositorySummary ]
117122 if resource_type == ResourceType .REPOSITORY_LIST :
118123 parsed = _parse_repositories_response (response )
@@ -121,9 +126,16 @@ def _dispatch_return_type(
121126 if return_type == ReturnType .DATACLASS :
122127 return parsed
123128
129+ # JSON and dictionary
124130 if return_type == ReturnType .JSON :
125131 return _to_json (parsed )
126- return _to_dict (parsed )
132+ if return_type == ReturnType .DICT :
133+ return _to_dict (parsed )
134+
135+ # tabular representations: DataFrame and CSV
136+ if return_type == ReturnType .DATAFRAME :
137+ return _to_dataframe (parsed )
138+ return _to_csv (parsed )
127139
128140
129141class BaseClient :
0 commit comments