From f939feef11965b82237f2402cf2765f3d2c3c71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Wed, 20 Sep 2023 18:16:05 -0600 Subject: [PATCH] Abstract away request logic --- src/citric/rest/client.py | 40 ++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/citric/rest/client.py b/src/citric/rest/client.py index d8b020e8..8712fec1 100644 --- a/src/citric/rest/client.py +++ b/src/citric/rest/client.py @@ -95,6 +95,34 @@ def _auth(self, request: requests.PreparedRequest) -> requests.PreparedRequest: request.headers["Authorization"] = f"Bearer {self.__session_id}" return request + def make_request( + self, + method: str, + path: str, + *, + params: t.Mapping[str, t.Any] | None = None, + json: t.Any | None = None, # noqa: ANN401 + ) -> requests.Response: + """Make a request to the REST API. + + Args: + method: HTTP method. + path: URL path. + params: Query parameters. + json: JSON data. + + Returns: + Response. + """ + response = self._session.request( + method=method, + url=f"{self.url}{path}", + params=params, + json=json, + ) + response.raise_for_status() + return response + def __enter__(self: Self) -> Self: """Context manager for REST session. @@ -124,8 +152,7 @@ def get_surveys(self) -> list[dict[str, t.Any]]: Returns: List of surveys. """ - response = self._session.get(url=f"{self.url}/rest/v1/survey") - response.raise_for_status() + response = self.make_request("GET", "/rest/v1/survey") return response.json()["surveys"] def get_survey_details(self, survey_id: int) -> dict[str, t.Any]: @@ -137,8 +164,7 @@ def get_survey_details(self, survey_id: int) -> dict[str, t.Any]: Returns: Survey details. """ - response = self._session.get(f"{self.url}/rest/v1/survey-detail/{survey_id}") - response.raise_for_status() + response = self.make_request("GET", f"/rest/v1/survey-detail/{survey_id}") return response.json()["survey"] def update_survey_details( @@ -155,8 +181,9 @@ def update_survey_details( Returns: Updated survey details. """ - response = self._session.patch( - f"{self.url}/rest/v1/survey-detail/{survey_id}", + response = self.make_request( + "PATCH", + f"/rest/v1/survey-detail/{survey_id}", json={ "patch": [ { @@ -168,5 +195,4 @@ def update_survey_details( ], }, ) - response.raise_for_status() return response.json()