Skip to content

Commit

Permalink
dothttp agent (#138)
Browse files Browse the repository at this point in the history
* dothttp agent
* fix(postman): import postman to http is creating weird urls
  • Loading branch information
cedric05 authored Jun 2, 2022
1 parent af23819 commit f1c1f03
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 13 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM python:3.10
LABEL io.whalebrew.config.networks '["host"]'
RUN pip install dothttp-req
ENTRYPOINT ["dothttp"]
CMD ["dothttp"]
RUN pip install dothttp-req --pre
RUN pip install flask flask-cors
ENTRYPOINT ["python"]
CMD ["-m dotextensions.server http"]
15 changes: 12 additions & 3 deletions dotextensions/server/handlers/postman2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict
from pathlib import Path
from typing import Iterator, Optional, Union, Dict, List
from urllib.parse import unquote
from urllib.parse import unquote, urljoin

import requests

Expand Down Expand Up @@ -87,9 +87,18 @@ def import_leaf_item(item: Items, base_http_name) -> Union[Http, None]:

auth_wrap, lines = ImportPostmanCollection.get_auth_wrap(request_auth)
if isinstance(req.url, (URLClass, URLClass_2_1)):
host = ".".join(req.url.host) if req.url.host else "{{host}}"
if host := req.url.host:
if isinstance(host, list):
host = ".".join(host)
else:
host = "{{host}}"
if path := req.url.path:
if isinstance(path, list):
path = "/".join(path)
else:
path = ""
proto = req.url.protocol or "https"
path = "/".join(req.url.path) if req.url.path else ""
# url = urljoin(f"{proto}://{host}", path)
url = f"{proto}://{host}/{path}"
urlwrap.url = slashed_path_to_normal_path(url)
if req.url.query:
Expand Down
3 changes: 2 additions & 1 deletion dotextensions/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def get_command(self, **kwargs):
class HttpServer(Base):
def __init__(self, port=5000):
from flask import Flask
app = Flask("dothttp-server")
from flask_cors import CORS
app = CORS(Flask("dothttp-server"))
self.app = app
self.port = port

Expand Down
2 changes: 1 addition & 1 deletion dothttp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.41a2'
__version__ = '0.0.41a3'
5 changes: 5 additions & 0 deletions test/extensions/fixtures/dottedhost.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files": {
"test.http\\imported_from_collection.http": "#!/usr/bin/env dothttp\n\n# imported from dottedhost.postman_collection.json\n\n// base\n\n@name(\"base\")\nGET \"https://httpbin.org//get\"\n? \"q\"= \"tetris language:assembly\"\n? \"order\"= \"asc\"\n? \"sort\"= \"stars\"\n\n\n\n"
}
}
64 changes: 64 additions & 0 deletions test/extensions/postman/dottedhost.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"auth": null,
"event": null,
"info": {
"_postman_id": null,
"description": null,
"name": "test.http",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"version": null
},
"item": [
{
"description": null,
"event": null,
"id": null,
"name": "base",
"protocolProfileBehavior": null,
"request": {
"auth": null,
"body": null,
"certificate": null,
"description": "base",
"header": null,
"method": "GET",
"proxy": null,
"url": {
"hash": null,
"host": "httpbin.org",
"path": "/get",
"port": null,
"protocol": "https",
"query": [
{
"description": null,
"disabled": false,
"key": "q",
"value": "tetris language:assembly"
},
{
"description": null,
"disabled": false,
"key": "order",
"value": "asc"
},
{
"description": null,
"disabled": false,
"key": "sort",
"value": "stars"
}
],
"raw": "https://httpbin.org/get",
"variable": null
}
},
"response": null,
"variable": null,
"auth": null,
"item": null
}
],
"protocolProfileBehavior": null,
"variable": null
}
11 changes: 6 additions & 5 deletions test/extensions/test_postman2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def compare(self,
id=1)
)
# -------------------------------------------------------------------------------------
## For Linux
# # For Linux
# with open(os.path.join(fixtures_dir, file_to_compare), 'w') as f:
# result_normalize = {}
# if 'files' in response.result:
Expand All @@ -40,11 +40,11 @@ def compare(self,
# json.dump({"files": result_normalize}, f)
# else:
# json.dump(response.result, f)
# -------------------------------------------------------------------------------------
# For Windows
# # -------------------------------------------------------------------------------------
## For Windows
# with open(os.path.join(fixtures_dir, file_to_compare), 'w') as f:
# json.dump(response.result, f)
# -------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------
with open(os.path.join(fixtures_dir, file_to_compare), 'r') as f:
if sys.platform.startswith("windows"):
self.assertEqual(json.load(f), response.result)
Expand Down Expand Up @@ -101,7 +101,8 @@ def test_more(self):
f"{postman_dir}/variables.postman_collection.json",
f"{postman_dir}/descriptionobject.postman_collection.json",
f"{postman_dir}/ntlm.postman_collection.json",
f"{postman_dir}/multipart.postmancollection.json"
f"{postman_dir}/multipart.postmancollection.json",
f"{postman_dir}/dottedhost.postman_collection.json"
]
for link in links:
self.compare(link, os.path.join(fixtures_dir, os.path.basename(link)))
Expand Down

0 comments on commit f1c1f03

Please sign in to comment.