added node info in api response

This commit is contained in:
kiriharu 2021-01-04 00:01:51 +03:00
parent 6ab17a0109
commit f14e4da283
3 changed files with 19 additions and 4 deletions

View File

@ -1,10 +1,11 @@
from core.coretypes import ( from core.coretypes import (
Response, HttpCheckerResponse, Response, HttpCheckerResponse, APINodeInfo,
ResponseStatus, HttpErrorCodes, ErrorPayload ResponseStatus, HttpErrorCodes, ErrorPayload,
) )
from requests import Session from requests import Session
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
from .base import BaseChecker from .base import BaseChecker
from api.config import NODE_NAME, NODE_LOCATION
import time import time
import re import re
@ -29,12 +30,16 @@ class HttpChecker(BaseChecker):
request = self.session.get( request = self.session.get(
url url
) )
# TODO: requests.exceptions.InvalidURL failed to parse exception
except ConnectionError: except ConnectionError:
return Response( return Response(
status=ResponseStatus.ERROR, status=ResponseStatus.ERROR,
payload=ErrorPayload( payload=ErrorPayload(
message="Failed to establish a new connection", 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( payload=HttpCheckerResponse(
time=end_time - start_time, time=end_time - start_time,
status_code=request.status_code status_code=request.status_code
),
node=APINodeInfo(
name=NODE_NAME, location=NODE_LOCATION
) )
) )

View File

@ -7,7 +7,7 @@ APP_PORT = os.environ.get("PORT", 8080)
NODE_NAME = os.environ.get("NODE_NAME", "Default node") NODE_NAME = os.environ.get("NODE_NAME", "Default node")
# Node location. Will be shown in tgbot # 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. Will be used for requests
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN", "CHANGE_TOKEN_BY_ENV") ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN", "CHANGE_TOKEN_BY_ENV")

View File

@ -27,10 +27,17 @@ class HttpCheckerResponse(Payload):
time: float time: float
@dataclass
class APINodeInfo:
name: str
location: str
@dataclass @dataclass
class Response: class Response:
status: ResponseStatus status: ResponseStatus
payload: Payload payload: Payload
node: APINodeInfo
@dataclass @dataclass