diff --git a/dotextensions/server/handlers/gohandler.py b/dotextensions/server/handlers/gohandler.py index aa8208b..c295558 100644 --- a/dotextensions/server/handlers/gohandler.py +++ b/dotextensions/server/handlers/gohandler.py @@ -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 @@ -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): diff --git a/dotextensions/server/models/__init__.py b/dotextensions/server/models/__init__.py index 8f0ba8f..623ba2b 100644 --- a/dotextensions/server/models/__init__.py +++ b/dotextensions/server/models/__init__.py @@ -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" diff --git a/examples/example.http b/examples/example.http index 9998db5..ed07f53 100644 --- a/examples/example.http +++ b/examples/example.http @@ -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=" @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 8754bfd..58b9165 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT" diff --git a/test/extensions/test_position.py b/test/extensions/test_position.py index bff577f..34c948a 100644 --- a/test/extensions/test_position.py +++ b/test/extensions/test_position.py @@ -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 [