Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion polyapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import copy
import truststore
import logging
import builtins
from typing import Any, Dict, Optional, overload, Literal
from typing_extensions import TypedDict
truststore.inject_into_ssl()
Expand Down Expand Up @@ -98,4 +100,19 @@ def copy(self) -> '_PolyCustom':
return new


polyCustom: PolyCustomDict = _PolyCustom()
polyCustom: PolyCustomDict = _PolyCustom()

original_print = print

logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')

def log_prints(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
message = sep.join(map(str, objects)) + end
if file is sys.stdout:
logging.info(message)
elif file is sys.stderr:
logging.error(message)
else:
original_print(*objects, sep=sep, end=end, file=file, flush=flush)

builtins.print = log_prints
7 changes: 3 additions & 4 deletions polyapi/execute.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Dict, Optional
import requests
import os
import logging
import sys
from requests import Response
from polyapi.config import get_api_key_and_url, get_mtls_config
from polyapi.exceptions import PolyApiException

logger = logging.getLogger("poly")

def direct_execute(function_type, function_id, data) -> Response:
""" execute a specific function id/type
Expand Down Expand Up @@ -49,7 +48,7 @@ def direct_execute(function_type, function_id, data) -> Response:
if (resp.status_code < 200 or resp.status_code >= 300):
error_content = resp.content.decode("utf-8", errors="ignore")
if function_type == 'api' and os.getenv("LOGS_ENABLED"):
logger.error(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}")
print(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}", file=sys.stderr)
elif function_type != 'api':
raise PolyApiException(f"{resp.status_code}: {error_content}")

Expand All @@ -73,7 +72,7 @@ def execute(function_type, function_id, data) -> Response:
if (resp.status_code < 200 or resp.status_code >= 300) and os.getenv("LOGS_ENABLED"):
error_content = resp.content.decode("utf-8", errors="ignore")
if function_type == 'api' and os.getenv("LOGS_ENABLED"):
logger.error(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}")
print(f"Error executing api function with id: {function_id}. Status code: {resp.status_code}. Request data: {data}, Response: {error_content}", file=sys.stderr)
elif function_type != 'api':
raise PolyApiException(f"{resp.status_code}: {error_content}")

Expand Down
7 changes: 6 additions & 1 deletion polyapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import contextlib
import re
import polyapi
import builtins
from typing import Dict
from jsonschema_gentypes.cli import process_config
from jsonschema_gentypes import configuration
Expand Down Expand Up @@ -89,9 +91,12 @@ def generate_schema_types(input_data: Dict, root=None):
}

# jsonschema_gentypes prints source to stdout
# no option to surpress so we do this
# no option to suppress so we do this
# Not reverting the print monkeypatch causes print to bypass redirect
builtins.print = polyapi.original_print
with contextlib.redirect_stdout(None):
process_config(config, [tmp_input])
builtins.print = polyapi.log_prints

with open(tmp_output, encoding='utf-8') as f:
output = f.read()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]

[project]
name = "polyapi-python"
version = "0.3.9.dev6"
version = "0.3.9.dev7"
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
dependencies = [
Expand Down