Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Upgraded python and dependencies #460

Merged
merged 24 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion kytos/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def send(self, buffer):
try:
if self.is_alive():
self.transport.write(buffer)
except OSError as exception:
except (OSError, TypeError) as exception:
viniarck marked this conversation as resolved.
Show resolved Hide resolved
LOG.debug('Could not send packet. Exception: %s', exception)
self.close()
raise
Expand Down
3 changes: 1 addition & 2 deletions kytos/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from importlib import reload as reload_module
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from socket import error as SocketError

from pyof.foundation.exceptions import PackException

Expand Down Expand Up @@ -626,7 +625,7 @@ async def msg_out_event_handler(self):
message.header.xid,
packet.hex())
self.notify_listeners(triggered_event)
except (OSError, SocketError):
except (OSError, TypeError):
await self.publish_connection_error(triggered_event)
self.log.info("connection closed. Cannot send message")
except PackException as err:
Expand Down
41 changes: 16 additions & 25 deletions kytos/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
from pathlib import Path
from threading import Thread

from openapi_core.spec import Spec
from openapi_core.spec.shortcuts import create_spec
from openapi_core.validation.request import openapi_request_validator
from openapi_core.validation.request.datatypes import RequestValidationResult
from openapi_spec_validator import validate_spec
from openapi_spec_validator.readers import read_from_filename
from openapi_core import OpenAPI
from openapi_core.contrib.starlette import StarletteOpenAPIRequest
from openapi_core.unmarshalling.request.datatypes import RequestUnmarshalResult

from kytos.core.apm import ElasticAPM
from kytos.core.config import KytosConfig
from kytos.core.rest_api import (AStarletteOpenAPIRequest, HTTPException,
Request, StarletteOpenAPIRequest,
from kytos.core.rest_api import (HTTPException, Request,
content_type_json_or_415, get_body)

__all__ = ['listen_to', 'now', 'run_on_thread', 'get_time']
Expand Down Expand Up @@ -351,28 +347,23 @@ def get_time(data=None):
return date.replace(tzinfo=timezone.utc)


def _read_from_filename(yml_file_path: Path) -> dict:
"""Read from yml filename."""
spec_dict, _ = read_from_filename(yml_file_path)
return spec_dict


def load_spec(yml_file_path: Path):
"""Load and validate spec object given a yml file path."""
spec_dict = _read_from_filename(yml_file_path)
validate_spec(spec_dict)
return create_spec(spec_dict)
return OpenAPI.from_file_path(yml_file_path)


def _request_validation_result_or_400(result: RequestValidationResult) -> None:
def _request_validation_result_or_400(result: RequestUnmarshalResult) -> None:
"""Request validation result or raise HTTP 400."""
if not result.errors:
return
error_response = (
"The request body contains invalid API data."
)
errors = result.errors[0]
if hasattr(errors, "schema_errors"):
if not errors.__cause__:
error_response = str(errors)
elif hasattr(errors.__cause__, "schema_errors"):
errors = errors.__cause__
schema_errors = errors.schema_errors[0]
error_log = {
"error_message": schema_errors.message,
Expand All @@ -388,12 +379,12 @@ def _request_validation_result_or_400(result: RequestValidationResult) -> None:
f" {'/'.join(map(str,schema_errors.path))}."
)
else:
error_response = str(errors)
error_response = str(errors.__cause__)
raise HTTPException(400, detail=error_response)


def validate_openapi_request(
spec: Spec, request: Request, loop: AbstractEventLoop
spec: OpenAPI, request: Request, loop: AbstractEventLoop
) -> bytes:
"""Validate a Request given an OpenAPI spec.

Expand All @@ -405,13 +396,13 @@ def validate_openapi_request(
if body:
content_type_json_or_415(request)
openapi_request = StarletteOpenAPIRequest(request, body)
result = openapi_request_validator.validate(spec, openapi_request)
result = spec.unmarshal_request(openapi_request)
_request_validation_result_or_400(result)
return body


async def avalidate_openapi_request(
spec: Spec,
spec: OpenAPI,
request: Request,
) -> bytes:
"""Async validate_openapi_request.
Expand All @@ -429,8 +420,8 @@ async def avalidate_openapi_request(
body = await request.body()
if body:
content_type_json_or_415(request)
openapi_request = AStarletteOpenAPIRequest(request, body)
result = openapi_request_validator.validate(spec, openapi_request)
openapi_request = StarletteOpenAPIRequest(request, body)
result = spec.unmarshal_request(openapi_request)
_request_validation_result_or_400(result)
return body

Expand Down
58 changes: 0 additions & 58 deletions kytos/core/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
from datetime import datetime
from typing import Any, Optional

from openapi_core.contrib.starlette import \
StarletteOpenAPIRequest as _StarletteOpenAPIRequest
from openapi_core.validation.request.datatypes import RequestParameters
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse as StarletteJSONResponse
from starlette.responses import Response

Request = Request
Response = Response
HTTPException = HTTPException


def _json_serializer(obj):
Expand Down Expand Up @@ -99,53 +91,3 @@ def render(self, content) -> bytes:
separators=(",", ":"),
default=_json_serializer,
).encode("utf-8")


# pylint: disable=super-init-not-called
class AStarletteOpenAPIRequest(_StarletteOpenAPIRequest):
"""Async StarletteOpenAPIRequest."""

def __init__(self, request: Request, body: bytes) -> None:
"""Constructor of AsycnStarletteOpenAPIRequest.

This constructor doesn't call super().__init__() to keep it async
"""
self.request = request
self.parameters = RequestParameters(
query=self.request.query_params,
header=self.request.headers,
cookie=self.request.cookies,
)
self._body = body

@property
def body(self) -> Optional[str]:
body = self._body
if body is None:
return None
return body.decode("utf-8")


# pylint: disable=super-init-not-called
class StarletteOpenAPIRequest(_StarletteOpenAPIRequest):
"""Sync StarletteOpenAPIRequest."""

def __init__(self, request: Request, body: bytes) -> None:
"""Constructor of AsycnStarletteOpenAPIRequest.

This constructor doesn't call super().__init__() to keep it async
"""
self.request = request
self.parameters = RequestParameters(
query=self.request.query_params,
header=self.request.headers,
cookie=self.request.cookies,
)
self._body = body

@property
def body(self) -> Optional[str]:
body = self._body
if body is None:
return None
return body.decode("utf-8")
viniarck marked this conversation as resolved.
Show resolved Hide resolved
61 changes: 40 additions & 21 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ asttokens==2.4.1
# -r requirements/run.txt
# kytos
# stack-data
attrs==21.4.0
attrs==23.2.0
# via
# -r requirements/run.txt
# jsonschema
# kytos
# pytest
# referencing
babel==2.9.0
# via sphinx
black==23.3.0
Expand All @@ -53,8 +54,11 @@ certifi==2024.2.2
# httpx
# kytos
# requests
charset-normalizer==2.0.10
# via requests
charset-normalizer==3.3.2
# via
# -r requirements/run.txt
# kytos
# requests
click==8.1.7
# via
# -r requirements/run.txt
Expand Down Expand Up @@ -178,19 +182,25 @@ jinja2==3.1.3
# kytos
# sphinx
# starlette
jsonschema==4.17.3
jsonschema==4.21.1
# via
# -r requirements/run.txt
# jsonschema-spec
# kytos
# openapi-core
# openapi-schema-validator
# openapi-spec-validator
jsonschema-spec==0.1.4
jsonschema-path==0.3.2
# via
# -r requirements/run.txt
# kytos
# openapi-core
# openapi-spec-validator
jsonschema-specifications==2023.12.1
# via
# -r requirements/run.txt
# jsonschema
# kytos
# openapi-schema-validator
lazy-object-proxy==1.7.1
# via
# -r requirements/run.txt
Expand Down Expand Up @@ -223,17 +233,17 @@ more-itertools==8.12.0
# openapi-core
mypy-extensions==1.0.0
# via black
openapi-core==0.16.6
openapi-core==0.19.0
# via
# -r requirements/run.txt
# kytos
openapi-schema-validator==0.4.4
openapi-schema-validator==0.6.2
# via
# -r requirements/run.txt
# kytos
# openapi-core
# openapi-spec-validator
openapi-spec-validator==0.5.6
openapi-spec-validator==0.7.1
# via
# -r requirements/run.txt
# kytos
Expand All @@ -258,9 +268,8 @@ parso==0.8.3
pathable==0.4.3
# via
# -r requirements/run.txt
# jsonschema-spec
# jsonschema-path
# kytos
# openapi-core
pathspec==0.11.1
# via black
pexpect==4.9.0
Expand Down Expand Up @@ -324,11 +333,6 @@ pymongo==4.6.2
# kytos
pyproject-hooks==1.0.0
# via build
pyrsistent==0.18.0
# via
# -r requirements/run.txt
# jsonschema
# kytos
pytest==7.2.1
# via
# kytos
Expand Down Expand Up @@ -357,17 +361,34 @@ pytz==2021.3
pyyaml==6.0
# via
# -r requirements/run.txt
# jsonschema-spec
# jsonschema-path
# kytos
# starlette
# uvicorn
requests==2.27.0
# via sphinx
referencing==0.31.1
# via
# -r requirements/run.txt
# jsonschema
# jsonschema-path
# jsonschema-specifications
# kytos
requests==2.31.0
# via
# -r requirements/run.txt
# jsonschema-path
# kytos
# sphinx
rfc3339-validator==0.1.4
# via
# -r requirements/run.txt
# kytos
# openapi-schema-validator
rpds-py==0.18.0
# via
# -r requirements/run.txt
# jsonschema
# kytos
# referencing
six==1.16.0
# via
# -r requirements/run.txt
Expand Down Expand Up @@ -432,9 +453,7 @@ typing-extensions==4.5.0
# via
# -r requirements/run.txt
# janus
# jsonschema-spec
# kytos
# openapi-core
# pydantic
urllib3==1.26.18
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/run.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dnspython==2.6.1
email-validator==1.3.0
tenacity==8.2.3
elastic-apm==6.20.0
viniarck marked this conversation as resolved.
Show resolved Hide resolved
openapi-core==0.16.6
openapi-core==0.19.0
httpx==0.27.0
Alopalao marked this conversation as resolved.
Show resolved Hide resolved
starlette[full]==0.37.1
uvicorn[standard]==0.27.1
Expand Down
Loading
Loading