Skip to content

Commit

Permalink
Add query and headers to resolve. Update errors to include http/body (#…
Browse files Browse the repository at this point in the history
…354)

* Add query and headers to resolve. Update errors to include http/body

* Update pyproject.toml

* fix tests
  • Loading branch information
cedric05 authored Jan 14, 2025
1 parent ca44c46 commit bb8ad62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
91 changes: 47 additions & 44 deletions dotextensions/server/handlers/basic_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ def run(self, command: Command) -> Result:
except DotHttpException as exc:
logger.error(f"dothttp exception happened {exc}", exc_info=True)
result = Result(
id=command.id, result={
"error_message": exc.message, "error": True}
id=command.id, result={"error_message": exc.message, "error": True}
)
except RequestException as exc:
logger.error(f"exception from requests {exc}", exc_info=True)
result = Result(
id=command.id, result={
"error_message": str(exc), "error": True}
id=command.id, result={"error_message": str(exc), "error": True}
)
except Exception as exc:
logger.error(f"unknown error happened {exc}", exc_info=True)
result = Result(
id=command.id, result={
"error_message": str(exc), "error": True}
id=command.id, result={"error_message": str(exc), "error": True}
)
return result

Expand Down Expand Up @@ -97,8 +94,7 @@ def get_config(self, command):
target = params.get("target", "1")
nocookie = params.get("nocookie", False)
curl = params.get("curl", False)
properties = [f"{i}={j}" for i,
j in params.get("properties", {}).items()]
properties = [f"{i}={j}" for i, j in params.get("properties", {}).items()]
content = params.get("content", None)
contexts = params.get("contexts")
property_file = params.get("property-file", None)
Expand Down Expand Up @@ -131,11 +127,21 @@ def get_request_result(self, command, comp: RequestCompiler):
return Result(
id=command.id,
result={
"errors": [{"var": list(error.kwargs['var']), "message": str(error)} for error in comp.property_util.errors],
"errors": [
{"var": list(error.kwargs["var"]), "message": str(error)}
for error in comp.property_util.errors
],
"body": "\n".join(
[str(error) for error in comp.property_util.errors]
),
"http": "ERROR",
"response": {
"body": "",
"body": "\n".join(
[str(error) for error in comp.property_util.errors]
),
"output_file": "",
"status": 0,
"http": "ERROR",
"method": "ERROR",
"url": "ERROR",
"headers": {
Expand All @@ -155,8 +161,7 @@ def get_request_result(self, command, comp: RequestCompiler):
except Exception as e:
output = f"Not!. unhandled error happened : {e}"
logger.warning("unable to write because", exc_info=True)
script_result = comp.script_execution.execute_test_script(
resp).as_json()
script_result = comp.script_execution.execute_test_script(resp).as_json()
body = resp.text
response_data = {
"response": {
Expand All @@ -179,11 +184,11 @@ def get_request_result(self, command, comp: RequestCompiler):
# redirects can add cookies
comp.httpdef.headers["cookie"] = resp.request.headers["cookie"]
try:
data.update({"http": self.get_http_from_req(
comp.httpdef, comp.property_util)})
data.update(
{"http": self.get_http_from_req(comp.httpdef, comp.property_util)}
)
except Exception as e:
logger.error(
"ran into error regenerating http def from parsed object")
logger.error("ran into error regenerating http def from parsed object")
data.update(
{"http": f"ran into error \n Exception: `{e}` message:{e.args}"}
)
Expand All @@ -203,8 +208,7 @@ def get_request_comp(self, config):

@staticmethod
def get_http_from_req(request: HttpDef, property_util: "PropertyProvider"):
http_def = MultidefHttp(import_list=[], allhttps=[
request.get_http_from_req()])
http_def = MultidefHttp(import_list=[], allhttps=[request.get_http_from_req()])
return HttpFileFormatter.format(http_def, property_util=property_util)


Expand All @@ -229,8 +233,7 @@ def load_model(self):
##
# context has varibles defined
# for resolving purpose, including them into content
self.content = self.content + CONTEXT_SEP + \
CONTEXT_SEP.join(self.args.contexts)
self.content = self.content + CONTEXT_SEP + CONTEXT_SEP.join(self.args.contexts)

def select_target(self):
for context in self.args.contexts:
Expand All @@ -242,9 +245,7 @@ def select_target(self):
self.model.allhttps = self.model.allhttps + model.allhttps
if model.import_list and model.import_list.filename:
if self.model.import_list and self.model.import_list.filename:
self.model.import_list.filename += (
model.import_list.filename
)
self.model.import_list.filename += model.import_list.filename
else:
self.model.import_list = model.import_list
self.load_imports()
Expand Down Expand Up @@ -333,12 +334,12 @@ def run(self, command: Command) -> Result:
return result


class ResolveBase():
class ResolveBase:

def get_resolved(self, command: Command) -> Result:
filename = command.params.get('file')
content = command.params.get('content')
pos = command.params.get('position')
filename = command.params.get("file")
content = command.params.get("content")
pos = command.params.get("position")
if content:
model: MultidefHttp = dothttp_model.model_from_str(content)
else:
Expand All @@ -361,39 +362,43 @@ def get_resolved(self, command: Command) -> Result:
config = self.get_config(command)
comp: RequestCompiler = self.get_request_comp(config)
comp.load_def()
type_type = type_dict['type']
type_type = type_dict["type"]

resolved = None
if property_hovered:
type_dict["property_at_pos"] = {
"name": property_hovered,
"value": comp.property_util.resolve_property_string(property_hovered),
}
if type_type == DothttpTypes.URL.value:
type_dict["resolved"] = comp.httpdef.url
resolved = comp.httpdef.url
elif type_type == DothttpTypes.NAME.value:
type_dict["resolved"] = comp.httpdef.name
resolved = comp.httpdef.name
# header
# query is pending
elif type_type == DothttpTypes.VARIABLE.value:
variable_name = type_dict["name"]
value = comp.property_util.resolve_property_string(variable_name)
type_dict["resolved"] = value
# elif type_type == DothttpTypes.HEADER:
# return Result(id=command.id, result={"resolved": comp.httpdef.headerwrap.header})
resolved = comp.property_util.resolve_property_string(variable_name)
elif type_type == DothttpTypes.HEADER.value:
resolved = {"headers": comp.httpdef.headers}
elif type_type == DothttpTypes.URL_PARAMS.value:
resolved = {"query": comp.httpdef.query}
elif type_type == DothttpTypes.PAYLOAD_DATA.value:
type_dict["resolved"] = comp.httpdef.payload.data
resolved = comp.httpdef.payload.data
elif type_type == DothttpTypes.PAYLOAD_ENCODED.value:
type_dict["resolved"] = comp.httpdef.payload.data
resolved = comp.httpdef.payload.data
elif type_type == DothttpTypes.PAYLOAD_JSON.value:
type_dict["resolved"] = comp.httpdef.payload.json
resolved = comp.httpdef.payload.json
elif type_type == DothttpTypes.PAYLOAD_MULTIPART.value:
type_dict["resolved"] = comp.httpdef.payload.files
resolved = comp.httpdef.payload.files
elif type_type == DothttpTypes.PAYLOAD_FILE.value:
type_dict["resolved"] = comp.httpdef.payload.filename
resolved = comp.httpdef.payload.filename
else:
type_dict["resolved"] = ""
resolved = ""
type_dict["resolved"] = resolved
return Result(id=command.id, result=type_dict)


# return resolved string instead of model object


Expand Down Expand Up @@ -429,8 +434,7 @@ def run(self, command: Command) -> Result:
result = self.execute(command, filename)
except DotHttpException as ex:
result = Result(
id=command.id, result={
"error_message": ex.message, "error": True}
id=command.id, result={"error_message": ex.message, "error": True}
)
except Exception as e:
result = Result(
Expand Down Expand Up @@ -464,8 +468,7 @@ def parse_n_get(self, http_data, filename: str):
for new_model, _content in BaseModelProcessor._get_models_from_import(
model, filename
):
self.get_for_http(new_model.allhttps,
imported_names, imported_urls)
self.get_for_http(new_model.allhttps, imported_names, imported_urls)
return all_names, all_urls, imported_names, imported_urls

def get_for_http(self, allhttps, all_names, all_urls):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dothttp-req"
version = "0.0.44a11"
version = "0.0.44a12"
description = "Dothttp is Simple http client for testing and development"
authors = ["Prasanth <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit bb8ad62

Please sign in to comment.