From 29c2519ccc53e8d170118c10d0266d8091e79e80 Mon Sep 17 00:00:00 2001 From: jefer94 Date: Wed, 20 Mar 2024 23:54:43 -0500 Subject: [PATCH] support debug logger --- src/linked_services/__about__.py | 2 +- src/linked_services/core/service.py | 150 +++++++++++++++++++++++++- tests/django/actions/test_get_user.py | 6 +- 3 files changed, 153 insertions(+), 5 deletions(-) diff --git a/src/linked_services/__about__.py b/src/linked_services/__about__.py index 292457c..120a059 100644 --- a/src/linked_services/__about__.py +++ b/src/linked_services/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2024-present jefer94 # # SPDX-License-Identifier: LGPL-3.0-or-later -__version__ = "1.1.6" +__version__ = "1.2.0" diff --git a/src/linked_services/core/service.py b/src/linked_services/core/service.py index 28ae325..54a8aca 100644 --- a/src/linked_services/core/service.py +++ b/src/linked_services/core/service.py @@ -1,5 +1,7 @@ from __future__ import annotations +import logging +import os from types import TracebackType from typing import Any, Callable, Coroutine, Optional, Type @@ -12,6 +14,9 @@ __all__ = ["Service"] +logger = logging.getLogger(__name__) +DEBUG = os.getenv("LOG_LEVEL") == "DEBUG" + LIBRARIES = { "requests": False, "aiohttp": False, # no implemented yet @@ -94,6 +99,12 @@ def _sync_proxy(self, request: Callable[[], requests.Response], stream: bool) -> for header in header_keys: resource[header] = response.headers[header] + if DEBUG: + logger.debug("Response") + logger.debug(" Content: no visible due to it's a stream") + logger.debug(" Headers: " + str(response.headers)) + logger.debug(" Status code: " + str(response.status_code)) + return resource headers = {} @@ -101,6 +112,13 @@ def _sync_proxy(self, request: Callable[[], requests.Response], stream: bool) -> for header in header_keys: headers[header] = response.headers[header] + if DEBUG: + logger.debug("Response") + logger.debug(" Type: Proxy") + logger.debug(" Content: " + response.content.decode()) + logger.debug(" Headers: " + str(headers)) + logger.debug(" Status code: " + str(response.status_code)) + return HttpResponse(response.content, status=response.status_code, headers=headers) def _sync_get(self, url, params=None, **kwargs): @@ -112,6 +130,12 @@ def _sync_get(self, url, params=None, **kwargs): headers = self._authenticate("get", params=params, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: GET") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.get(url, params=params, **kwargs, headers=headers) if self.proxy: @@ -126,6 +150,12 @@ def _sync_options(self, url, **kwargs): headers = self._authenticate("options", **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: OPTIONS") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.options(url, **kwargs, headers=headers) if self.proxy: @@ -140,6 +170,12 @@ def _sync_head(self, url, **kwargs): headers = self._authenticate("head", **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: HEAD") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.head(url, **kwargs, headers=headers) if self.proxy: @@ -154,6 +190,12 @@ def _sync_post(self, url, data=None, json=None, **kwargs): headers = self._authenticate("post", data=data, json=json, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: POST") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.post(url, data=data, json=json, **kwargs, headers=headers) if self.proxy: @@ -168,6 +210,12 @@ def _sync_webhook(self, url, data=None, json=None, **kwargs): headers = self._authenticate("post", data=data, json=json, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Type: Webhook") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.post(url, data=data, json=json, **kwargs, headers=headers) if self.proxy: @@ -182,6 +230,12 @@ def _sync_put(self, url, data=None, **kwargs): headers = self._authenticate("put", data=data, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: PUT") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.put(url, data=data, **kwargs, headers=headers) if self.proxy: @@ -196,6 +250,12 @@ def _sync_patch(self, url, data=None, **kwargs): headers = self._authenticate("patch", data=data, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: PATCH") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.patch(url, data=data, **kwargs, headers=headers) if self.proxy: @@ -210,6 +270,12 @@ def _sync_delete(self, url, **kwargs): headers = self._authenticate("delete", **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: DELETE") + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.delete(url, **kwargs, headers=headers) if self.proxy: @@ -224,6 +290,12 @@ def _sync_request(self, method, url, **kwargs): headers = self._authenticate(method, **kwargs) def request() -> requests.Response: + if DEBUG: + logger.debug("Request") + logger.debug(" Method: " + str(method).upper()) + logger.debug(" Url: " + str(url)) + logger.debug("") + return requests.request(method, url, **kwargs, headers=headers) if self.proxy: @@ -289,7 +361,16 @@ async def _async_proxy(self, response: Coroutine[Any, Any, ClientResponse]) -> H for header in header_keys: headers[str(header)] = r.headers[header] - return HttpResponse(await r.content.read(), status=r.status, headers=headers) + content = await r.content.read() + + if DEBUG: + logger.debug("Response") + logger.debug(" Type: Proxy") + logger.debug(" Content: " + content.decode()) + logger.debug(" Headers: " + str(headers)) + logger.debug(" Status code: " + str(response.status_code)) + + return HttpResponse(content, status=r.status, headers=headers) def _async_get(self, url, params=None, **kwargs): url = self.app.app_url + self._fix_url(url) @@ -300,6 +381,12 @@ def _async_get(self, url, params=None, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: GET") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -315,6 +402,12 @@ def _async_options(self, url, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: OPTIONS") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -330,6 +423,12 @@ def _async_head(self, url, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: HEAD") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -345,6 +444,12 @@ def _async_post(self, url, data=None, json=None, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: POST") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -360,6 +465,12 @@ def _async_webhook(self, url, data=None, json=None, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Type: Webhook") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -375,6 +486,12 @@ def _async_put(self, url, data=None, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: PUT") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -390,6 +507,12 @@ def _async_patch(self, url, data=None, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: PATCH") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -405,6 +528,12 @@ def _async_delete(self, url, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: DELETE") + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -420,6 +549,12 @@ def _async_request(self, method, url, **kwargs): res = obj.__aenter__() + if DEBUG: + logger.debug("Request") + logger.debug(" Method: " + str(method).upper()) + logger.debug(" Url: " + str(url)) + logger.debug("") + # wraps client response to be used within django views if self.proxy: return self._async_proxy(res) @@ -458,6 +593,12 @@ def _async_proxy(self, response: requests.Response, stream: bool) -> StreamingHt for header in header_keys: resource[header] = response.headers[header] + if DEBUG: + logger.debug("Response") + logger.debug(" Content: no visible due to it's a stream") + logger.debug(" Headers: " + str(response.headers)) + logger.debug(" Status code: " + str(response.status_code)) + return resource headers = {} @@ -465,6 +606,13 @@ def _async_proxy(self, response: requests.Response, stream: bool) -> StreamingHt for header in header_keys: headers[header] = response.headers[header] + if DEBUG: + logger.debug("Response") + logger.debug(" Type: Proxy") + logger.debug(" Content: " + response.content.decode()) + logger.debug(" Headers: " + str(headers)) + logger.debug(" Status code: " + str(response.status_code)) + return HttpResponse(response.content, status=response.status_code, headers=headers) async def _async_get(self, url, params=None, **kwargs): diff --git a/tests/django/actions/test_get_user.py b/tests/django/actions/test_get_user.py index 7a4e5b2..c12d5ff 100644 --- a/tests/django/actions/test_get_user.py +++ b/tests/django/actions/test_get_user.py @@ -23,7 +23,7 @@ def patch(db, monkeypatch): @pytest.mark.parametrize("user", [0, 1]) -async def test_no_first_party_credentials(patch, fake, database, user): +def test_no_first_party_credentials(patch, fake, database, user): database.create(user=user) v = fake.slug() create_use_mock = patch @@ -36,9 +36,9 @@ async def test_no_first_party_credentials(patch, fake, database, user): assert database.list_of("linked_services.FirstPartyCredentials") == [] -async def test_with_first_party_credentials(patch, fake, database, get_json_obj): +def test_with_first_party_credentials(patch, fake, database, get_json_obj): app_slug = fake.slug() - model = database.acreate( + model = database.create( user=1, first_party_credentials={ "app": {app_slug: 1},