Skip to content

Commit

Permalink
final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
InnocentBug committed Feb 23, 2024
1 parent 93dce58 commit 807d72d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
The fixtures are all functional fixtures that stay consistent between all tests.
"""
import logging
import os

import pytest
Expand Down Expand Up @@ -57,6 +58,8 @@ def cript_api():
api._BUCKET_NAME = "cript-stage-user-data"
# using the tests folder name within our cloud storage
api._BUCKET_DIRECTORY_NAME = "tests"
api.extra_api_log_debug_info = True
api.logger.setLevel(logging.DEBUG)

yield api

Expand Down
20 changes: 10 additions & 10 deletions src/cript/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import os
import traceback
import uuid
from pathlib import Path
from typing import Any, Dict, Optional, Union
Expand Down Expand Up @@ -72,6 +73,8 @@ class API:
_internal_s3_client: Any = None # type: ignore
# trunk-ignore-end(cspell)

extra_api_log_debug_info: bool = False

@beartype
def __init__(self, host: Union[str, None] = None, api_token: Union[str, None] = None, storage_token: Union[str, None] = None, config_file_path: Union[str, Path] = "", default_log_level=logging.INFO):
"""
Expand Down Expand Up @@ -212,9 +215,6 @@ def __init__(self, host: Union[str, None] = None, api_token: Union[str, None] =
# add Bearer to token for HTTP requests
self._http_headers = {"Authorization": f"Bearer {self._api_token}", "Content-Type": "application/json"}

# check that api can connect to CRIPT with host and token
self._check_initial_host_connection()

# set a logger instance to use for the class logs
self._init_logger(default_log_level)
self._db_schema = DataSchema(self)
Expand Down Expand Up @@ -452,10 +452,12 @@ def _internal_save(self, node, save_values: Optional[_InternalSaveValues] = None
break

method = "POST"
url_path = f"/{node.node_type_snake_case}/"
if patch_request:
method = "PATCH"
url_path += f"{str(node.uuid)}/"

response: Dict = self._capsule_request(url=f"/{node.node_type_snake_case}/{str(node.uuid)}/", method=method, data=json_data).json() # type: ignore
response: Dict = self._capsule_request(url_path=url_path, method=method, data=json_data).json() # type: ignore

# if node.node_type != "Project":
# test_success: Dict = requests.get(url=f"{self._host}/{node.node_type_snake_case}/{str(node.uuid)}/", headers=self._http_headers, timeout=_API_TIMEOUT).json()
Expand Down Expand Up @@ -971,8 +973,6 @@ def _capsule_request(self, url_path: str, method: str, api_request: bool = True,
additional keyword arguments that are passed to `request.request`
"""

extra_debug_info: bool = True

if headers is None:
headers = self._http_headers

Expand All @@ -982,15 +982,15 @@ def _capsule_request(self, url_path: str, method: str, api_request: bool = True,
url += url_path

pre_log_message: str = f"Requesting {method} from {url}"
if extra_debug_info:
pre_log_message += f"headers {headers} kwargs {kwargs}"
if self.extra_api_log_debug_info:
pre_log_message += f" from {traceback.format_stack(limit=4)} kwargs {kwargs}"
pre_log_message += "..."
self.logger.debug(pre_log_message)

response: requests.Response = requests.request(url=url, method=method, headers=headers, timeout=timeout, **kwargs)
post_log_message: str = f"Request return with {response.status_code}"
if extra_debug_info:
post_log_message += f" {response}"
if self.extra_api_log_debug_info:
post_log_message += f" {response.json()}"
self.logger.debug(post_log_message)

return response
5 changes: 2 additions & 3 deletions src/cript/api/data_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ def __init__(self, api):
>>> with cript.API(host="https://api.criptapp.org/") as api:
... data_schema = cript.api.DataSchema(api)
"""

self._api = api
self._db_schema = self._get_db_schema()
self._vocabulary = self._get_vocab()
self._api = api

def _get_db_schema(self) -> dict:
"""
Expand Down Expand Up @@ -75,7 +74,7 @@ def _get_db_schema(self) -> dict:

return db_schema

def _get_vocab(self, host: str) -> dict:
def _get_vocab(self) -> dict:
"""
gets the entire CRIPT controlled vocabulary and stores it in _vocabulary
Expand Down

0 comments on commit 807d72d

Please sign in to comment.