Skip to content

Commit

Permalink
Variable position
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed Dec 30, 2024
1 parent 225c85c commit 4e56122
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
19 changes: 19 additions & 0 deletions dotextensions/server/handlers/gohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Union

from dothttp.parse import BaseModelProcessor, Http, MultidefHttp
from dothttp.parse.dsl_jsonparser import jsonmodel_to_json
from dothttp.parse.request_base import dothttp_model
from ..models import BaseHandler, Command, DothttpTypes, Result

Expand Down Expand Up @@ -70,6 +71,24 @@ def figure_n_get(self, model: MultidefHttp, position: int) -> dict:
"type": DothttpTypes.IMPORT.value,
"filename": import_file.value,
}
elif model.variables:
for index, variable in enumerate(model.variables):
if self.is_in_between(variable, position):
value = None
try:
if variable.value:
value = jsonmodel_to_json(variable.value)
elif variable.inter:
value = variable.inter
elif variable.func:
value = variable.func
except:
pass
return {
"type": DothttpTypes.VARIABLE.value,
"name": variable.name,
"value": value,
}
for index, pick_http in enumerate(model.allhttps):
if self.is_in_between(pick_http, position):
if dot_type := self.pick_in_http(pick_http, position):
Expand Down
1 change: 1 addition & 0 deletions dotextensions/server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def run(self, command: Command) -> Result:

class DothttpTypes(Enum):
NAME = "name"
VARIABLE = "variable"
EXTRA_ARGS = "extra_args"
URL = "url"
BASIC_AUTH = "basic_auth"
Expand Down
13 changes: 9 additions & 4 deletions examples/example.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
var firstName = "John";
var lastName = "Doe";
var age = 25;
var fullName = $"{firstName} {lastName}";

@name("fetch 100 users, skip first 50")
POST https://httpbin.org/post
"Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
Expand All @@ -9,10 +14,10 @@ POST https://httpbin.org/post
? projection, location

{
"firstName": {{firstName='John'}},
"lastName": {{lastName='Doe'}},
"age": {{age=25}},
"fullName": "{{firstName}} {{lastName}}",
"firstName": {{firstName}},
"lastName": {{lastName}},
"age": {{age}},
"fullName": "{{fullName}}",
ageInDays: (25*365),
// sample without no quotes
description: sampleOneWithNoQuotesInValue
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.44a1"
version = "0.0.44a2"
description = "Dothttp is Simple http client for testing and development"
authors = ["Prasanth <[email protected]>"]
license = "MIT"
Expand Down
35 changes: 35 additions & 0 deletions test/extensions/test_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ def test_working(self):
{"base_start": None, "target": "1", "target_base": None, "type": "url"},
result,
)

def test_content_var(self):
params = {
"content": """var a = 10;
var b = 100;
var b = $"{a} + {a} = {b}";
var d = { "a": a, "b": b };
POST "https://httpbin.org/post"
? a = "{{a}}"
? b = "{{b}}"
json({
"a": {{d}}
})
""",
"position":9,
"source":"hover"
}
result = self.execute_n_get(params)
self.assertEqual(
{'name': 'a', 'type': 'variable', 'value': 10},
result,
)
params["position"] = 20
result = self.execute_n_get(params)
self.assertEqual(
{'name': 'b', 'type': 'variable', 'value': 100},
result,
)
params["position"] = 30
result = self.execute_n_get(params)
self.assertEqual(
{'name': 'b', 'type': 'variable', 'value': '$"{a} + {a} = {b}"'},
result,
)


def test_more1(self):
for params, result in [
Expand Down

0 comments on commit 4e56122

Please sign in to comment.