Skip to content

Commit

Permalink
Post request body can now be read from file.
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Mar 20, 2019
1 parent 36878a8 commit c86089a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
36 changes: 18 additions & 18 deletions autoload/roast.vim
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
aug roast_response_maps
au!
au BufNewFile __roast_* nnoremap <buffer> <silent> <C-j> :py3 roast.next_render()<CR>
au BufNewFile __roast_* nnoremap <buffer> <silent> <C-k> :py3 roast.prev_render()<CR>
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
aug roast_response_maps
au!
au BufNewFile __roast_* nnoremap <buffer> <silent> <C-j> :py3 roast.next_render()<CR>
au BufNewFile __roast_* nnoremap <buffer> <silent> <C-k> :py3 roast.prev_render()<CR>
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
22 changes: 20 additions & 2 deletions python3/roast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from itertools import takewhile
from typing import List, Dict, Optional
import json
from pathlib import Path

import requests


Expand Down Expand Up @@ -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)
Expand All @@ -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 = {}
Expand Down

0 comments on commit c86089a

Please sign in to comment.