Skip to content

Commit

Permalink
Restructure the validatation code. This process reminds me that I nee…
Browse files Browse the repository at this point in the history
…d to rethink how I represent the services some
  • Loading branch information
Bachmann1234 committed Oct 6, 2018
1 parent 9c5f2ad commit 4a575d2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
6 changes: 3 additions & 3 deletions thrift_explorer/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ def service_method(thrift, service, method):
thrift_file=thrift,
service_name=service,
endpoint_name=method.name,
host=request_json["host"],
port=request_json["port"],
host=request_json.get("host"),
port=request_json.get("port"),
protocol=request_json.get("protocol", app.config[DEFAULT_PROTOCOL_ENV]),
transport=request_json.get(
"transport", app.config[DEFAULT_TRANSPORT_ENV]
),
request_body=request_json["request_body"],
request_body=request_json.get("request_body"),
)
errors = thrift_manager.validate_request(thrift_request)
if errors:
Expand Down
56 changes: 45 additions & 11 deletions thrift_explorer/thrift_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ def thrift_definition(self, thrift):
with open(self.thrift_paths[thrift]) as infile:
return infile.read()

def validate_request(self, thrift_request):
def _thrift_is_loaded(self, thrift_request):
try:
thrift_spec = self.service_specs[thrift_request.thrift_file]
return None
except KeyError:
return [
Error(
Expand All @@ -198,37 +199,46 @@ def validate_request(self, thrift_request):
),
)
]

def _service_in_thrift(self, thrift_spec, service_name, thrift_file):
try:
service_spec = thrift_spec[thrift_request.service_name]
service_spec = thrift_spec[service_name]
except KeyError:
return [
Error(
code=ErrorCode.SERVICE_NOT_IN_THRIFT,
message="Service '{}' not in thrift '{}'".format(
thrift_request.service_name, thrift_request.thrift_file
service_name, thrift_file
),
)
]

def _endpoint_in_service(
self, service_spec, endpoint_name, service_name, thrift_file
):
try:
endpoint_spec = service_spec.endpoints[thrift_request.endpoint_name]
endpoint_spec = service_spec.endpoints[endpoint_name]
except KeyError:
return [
Error(
code=ErrorCode.ENDPOINT_NOT_IN_SERVICE,
message="Endpoint '{}' not in service '{}' in thrift '{}'".format(
thrift_request.endpoint_name,
thrift_request.service_name,
thrift_request.thrift_file,
endpoint_name, service_name, thrift_file
),
)
]

def _validate_request_body(
self, thrift_file, service_name, endpoint_name, request_body
):
validation_errors = []
for arg_spec in endpoint_spec.args:
for arg_spec in (
self.service_specs[thrift_file][service_name]
.endpoints[endpoint_name]
.args # this kind of crap makes me think I need to rethink my structures
):
try:
error = arg_spec.type_info.validate_arg(
thrift_request.request_body[arg_spec.name]
)
error = arg_spec.type_info.validate_arg(request_body[arg_spec.name])
if error:
validation_errors.append(
FieldError(
Expand All @@ -250,6 +260,30 @@ def validate_request(self, thrift_request):
)
return validation_errors

def validate_request(self, thrift_request):
return (
self._thrift_is_loaded(thrift_request)
or self._service_in_thrift(
self.service_specs[thrift_request.thrift_file],
thrift_request.service_name,
thrift_request.thrift_file,
)
or self._endpoint_in_service(
self.service_specs[thrift_request.thrift_file][
thrift_request.service_name
],
thrift_request.endpoint_name,
thrift_request.service_name,
thrift_request.thrift_file,
)
or self._validate_request_body(
thrift_request.thrift_file,
thrift_request.service_name,
thrift_request.endpoint_name,
thrift_request.request_body,
)
)

def make_request(self, thrift_request):
thriftpy_service = getattr(
self._thrifts[thrift_request.thrift_file], thrift_request.service_name
Expand Down

0 comments on commit 4a575d2

Please sign in to comment.