diff --git a/autoload/roast.vim b/autoload/roast.vim index b3a788e..b1d4ac7 100644 --- a/autoload/roast.vim +++ b/autoload/roast.vim @@ -1,18 +1,18 @@ -aug roast_response_maps - au! - au BufNewFile __roast_* nnoremap :py3 roast.next_render() - au BufNewFile __roast_* nnoremap :py3 roast.prev_render() -aug END - -if (&bg ==? 'light') - highlight default RoastCurrentSuccess guibg=#E7F4D2 gui=bold - highlight default RoastCurrentFailure guibg=#F4DFD2 gui=bold -else - highlight default RoastCurrentSuccess guibg=#005A66 gui=bold -endif - -py3 import roast - -fun! roast#run() - py3 roast.run() -endfun \ No newline at end of file +aug roast_response_maps + au! + au BufNewFile __roast_* nnoremap :py3 roast.next_render() + au BufNewFile __roast_* nnoremap :py3 roast.prev_render() +aug END + +if (&bg ==? 'light') + highlight default RoastCurrentSuccess guibg=#E7F4D2 gui=bold + highlight default RoastCurrentFailure guibg=#F4DFD2 gui=bold +else + highlight default RoastCurrentSuccess guibg=#005A66 gui=bold +endif + +py3 import roast + +fun! roast#run() + py3 roast.run() +endfun diff --git a/python3/roast_api.py b/python3/roast_api.py index 32dd383..09bb203 100644 --- a/python3/roast_api.py +++ b/python3/roast_api.py @@ -7,6 +7,8 @@ from itertools import takewhile from typing import List, Dict, Optional import json +from pathlib import Path + import requests @@ -62,7 +64,11 @@ def build_request(lines, line_num) -> requests.Request: method, loc, *tokens = tokenize(line) heredoc = pop_heredoc(tokens) - body = '\n'.join(takewhile(lambda l: l != heredoc, lines[line_num + 1:])) if heredoc else None + if heredoc: + body = '\n'.join(takewhile(lambda l: l != heredoc, lines[line_num + 1:])) + else: + file_path = pop_file_body(tokens) + body = file_path.read_text() if file_path else None if 'host' in headers: url = headers.pop('host').rstrip('/') + '/' + loc.lstrip('/').format(**variables) @@ -77,13 +83,25 @@ def build_request(lines, line_num) -> requests.Request: def pop_heredoc(tokens: List[str]) -> Optional[str]: heredoc = None if tokens and tokens[-1].startswith('<<'): - heredoc = tokens.pop().lstrip('<') + heredoc = tokens.pop()[2:] elif len(tokens) >= 2 and tokens[-2] == '<<': heredoc = tokens.pop() tokens.pop() return heredoc +def pop_file_body(tokens: List[str]) -> Optional[Path]: + loc = None + if tokens and tokens[-1].startswith('<'): + loc = tokens.pop()[1:] + elif len(tokens) >= 2 and tokens[-2] == '<': + loc = tokens.pop() + tokens.pop() + return loc and Path(loc) + + + + def build_params_dict(tokens: List[str], variables: Dict[str, str] = None) -> Dict[str, str]: if variables is None: variables = {}