From f14e4da2835baab8031d56bf364af3d11e593fb3 Mon Sep 17 00:00:00 2001 From: kiriharu Date: Mon, 4 Jan 2021 00:01:51 +0300 Subject: [PATCH] added node info in api response --- apps/api/api/checkers/http.py | 14 +++++++++++--- apps/api/api/config.py | 2 +- apps/core/core/coretypes.py | 7 +++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/api/api/checkers/http.py b/apps/api/api/checkers/http.py index 972919c..a6547e2 100644 --- a/apps/api/api/checkers/http.py +++ b/apps/api/api/checkers/http.py @@ -1,10 +1,11 @@ from core.coretypes import ( - Response, HttpCheckerResponse, - ResponseStatus, HttpErrorCodes, ErrorPayload + Response, HttpCheckerResponse, APINodeInfo, + ResponseStatus, HttpErrorCodes, ErrorPayload, ) from requests import Session from requests.exceptions import ConnectionError from .base import BaseChecker +from api.config import NODE_NAME, NODE_LOCATION import time import re @@ -29,12 +30,16 @@ class HttpChecker(BaseChecker): request = self.session.get( url ) + # TODO: requests.exceptions.InvalidURL failed to parse exception except ConnectionError: return Response( status=ResponseStatus.ERROR, payload=ErrorPayload( message="Failed to establish a new connection", - code=HttpErrorCodes.ConnectError + code=HttpErrorCodes.ConnectError, + ), + node=APINodeInfo( + name=NODE_NAME, location=NODE_LOCATION ) ) @@ -45,5 +50,8 @@ class HttpChecker(BaseChecker): payload=HttpCheckerResponse( time=end_time - start_time, status_code=request.status_code + ), + node=APINodeInfo( + name=NODE_NAME, location=NODE_LOCATION ) ) diff --git a/apps/api/api/config.py b/apps/api/api/config.py index 596099d..7687c89 100644 --- a/apps/api/api/config.py +++ b/apps/api/api/config.py @@ -7,7 +7,7 @@ APP_PORT = os.environ.get("PORT", 8080) NODE_NAME = os.environ.get("NODE_NAME", "Default node") # Node location. Will be shown in tgbot -NODE_LOCATION = os.environ.get("NODE_LOCATION", "Undefined Location") +NODE_LOCATION = os.environ.get("NODE_LOCATION", "🏳️‍🌈 Undefined, Location") # Access token. Will be used for requests ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN", "CHANGE_TOKEN_BY_ENV") diff --git a/apps/core/core/coretypes.py b/apps/core/core/coretypes.py index b53c3e4..402ff63 100644 --- a/apps/core/core/coretypes.py +++ b/apps/core/core/coretypes.py @@ -27,10 +27,17 @@ class HttpCheckerResponse(Payload): time: float +@dataclass +class APINodeInfo: + name: str + location: str + + @dataclass class Response: status: ResponseStatus payload: Payload + node: APINodeInfo @dataclass