yakshaveinc / tasks

distributed roadmap
The Unlicense
1 stars 0 forks source link

LSP2LSIF #73

Open abitrolly opened 3 years ago

abitrolly commented 3 years ago
abitrolly commented 3 years ago

Debugging with httpie fails, because it sends HTTP headers and expects HTTP headers. It should be patched (or extended with TransportPlugin to just send Content-Length and expect Content-Length in return.

The plugin in turn is requests Transport Adapter.

(expand) http -v 127.0.0.1:9084 method=initialize id=1 --debug --traceback --stream ```python $ http -v 127.0.0.1:9084 method=initialize id=1 --debug --traceback --stream HTTPie 2.3.0 Requests 2.25.1 Pygments 2.7.4 Python 3.9.4 (default, Apr 6 2021, 00:00:00) [GCC 11.0.1 20210324 (Red Hat 11.0.1-0)] /usr/bin/python3 Linux 5.11.15-300.fc34.x86_64 , 'is_windows': False, 'log_error': , 'program_name': 'http', 'stderr': <_io.TextIOWrapper name='' mode='w' encoding='utf-8'>, 'stderr_isatty': True, 'stdin': <_io.TextIOWrapper name='' mode='r' encoding='utf-8'>, 'stdin_encoding': 'utf-8', 'stdin_isatty': True, 'stdout': <_io.TextIOWrapper name='' mode='w' encoding='utf-8'>, 'stdout_encoding': 'utf-8', 'stdout_isatty': True}> >>> requests.request(**{'auth': None, 'data': '{"method": "initialize", "id": "1"}', 'headers': {'User-Agent': b'HTTPie/2.3.0', 'Accept': b'application/json, */*;q=0.5', 'Content-Type': b'application/json'}, 'method': 'post', 'params': , 'url': 'http://127.0.0.1:9084'}) POST / HTTP/1.1 Accept: application/json, */*;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 35 Content-Type: application/json Host: 127.0.0.1:9084 User-Agent: HTTPie/2.3.0 { "id": "1", "method": "initialize" } http: error: ConnectionError: ('Connection aborted.', BadStatusLine('Content-Length: 528\r\n')) while doing a POST request to URL: http://127.0.0.1:9084/ ```

/spent 10h

abitrolly commented 3 years ago

requests only needs to implement one method - send() - https://docs.python-requests.org/en/latest/_modules/requests/adapters/

The requests API is tied to Session, but it is not possible to add own methods to it without monkeypatching. So users will need to use standard .get() (or .post()) calls with JSON-RPC method names added to passed JSON.

import requests
from requests_lsp import LSPAdapter

session = requests.Session()
session.mount("lsp://", LSPAdapter())

response = session.get("lsp://127.0.0.1:9084", json={"id":"1", "method": "initialize"})
print(response.json())

Adapter should save connection between calls, provide Content-Length header and JSON-RPC ids if they are not set.

abitrolly commented 3 years ago

Working proof of concept for requests adapter is done - https://github.com/abitrolly/requests-lsp

/spent 20h