Skip to content

Commit

Permalink
support debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Mar 21, 2024
1 parent 3439d31 commit 29c2519
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/linked_services/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present jefer94 <[email protected]>
#
# SPDX-License-Identifier: LGPL-3.0-or-later
__version__ = "1.1.6"
__version__ = "1.2.0"
150 changes: 149 additions & 1 deletion src/linked_services/core/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import logging
import os
from types import TracebackType
from typing import Any, Callable, Coroutine, Optional, Type

Expand All @@ -12,6 +14,9 @@

__all__ = ["Service"]

logger = logging.getLogger(__name__)
DEBUG = os.getenv("LOG_LEVEL") == "DEBUG"

LIBRARIES = {
"requests": False,
"aiohttp": False, # no implemented yet
Expand Down Expand Up @@ -94,13 +99,26 @@ 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 = {}

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):
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -458,13 +593,26 @@ 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 = {}

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):
Expand Down
6 changes: 3 additions & 3 deletions tests/django/actions/test_get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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},
Expand Down

0 comments on commit 29c2519

Please sign in to comment.