1- local request = require (" rest-nvim.request" )
1+ local backend = require (" rest-nvim.request" )
22local config = require (" rest-nvim.config" )
33local curl = require (" rest-nvim.curl" )
44local log = require (" plenary.log" ).new ({ plugin = " rest.nvim" })
55
66local rest = {}
77local Opts = {}
8- local LastOpts = {}
8+ local defaultRequestOpts = {
9+ verbose = false ,
10+ highlight = false ,
11+ engine = ' classic'
12+ }
913
14+ local LastOpts = {}
1015rest .setup = function (user_configs )
1116 config .set (user_configs or {})
17+
1218end
1319
1420-- run will retrieve the required request information from the current buffer
1521-- and then execute curl
1622-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
1723rest .run = function (verbose )
18- local ok , result = request .get_current_request ()
24+ local ok , result = backend .get_current_request ()
1925 if not ok then
2026 log .error (" Failed to run the http request:" )
2127 log .error (result )
3137-- @param string filename to load
3238-- @param opts table
3339-- 1. keep_going boolean keep running even when last request failed
40+ -- 2. verbose boolean
3441rest .run_file = function (filename , opts )
3542 log .info (" Running file :" .. filename )
36- local new_buf = vim .api .nvim_create_buf (false , false )
43+ opts = vim .tbl_deep_extend (
44+ " force" , -- use value from rightmost map
45+ defaultRequestOpts ,
46+ opts or {}
47+ )
48+
49+ -- 0 on error or buffer handle
50+ local new_buf = vim .api .nvim_create_buf (true , false )
3751
3852 vim .api .nvim_win_set_buf (0 , new_buf )
3953 vim .cmd .edit (filename )
40- local last_line = vim .fn .line (" $" )
41-
42- -- reset cursor position
43- vim .fn .cursor (1 , 1 )
44- local curpos = vim .fn .getcurpos ()
45- while curpos [2 ] <= last_line do
46- local ok , req = request .buf_get_request (new_buf , curpos )
47- if ok then
48- -- request.print_request(req)
49- curpos [2 ] = req .end_line + 1
50- rest .run_request (req , opts )
51- else
52- return false , req
53- end
54+
55+ local requests = backend .buf_list_requests (new_buf )
56+ for _ , req in pairs (requests ) do
57+ vim .pretty_print (" Request:" )
58+ vim .pretty_print (req )
59+ rest .run_request (req , opts )
5460 end
61+
5562 return true
5663end
5764
6269-- @param opts table
6370-- 1. keep_going boolean keep running even when last request failed
6471rest .run_request = function (req , opts )
72+ -- TODO rename result to req
6573 local result = req
6674 opts = vim .tbl_deep_extend (
6775 " force" , -- use value from rightmost map
68- { verbose = false ,
69- highlight = false
70- }, -- defaults
76+ defaultRequestOpts ,
7177 opts or {}
7278 )
7379
@@ -92,7 +98,7 @@ rest.run_request = function(req, opts)
9298 end
9399
94100 if opts .highlight then
95- request .highlight (result .bufnr , result .start_line , result .end_line )
101+ backend .highlight (result .bufnr , result .start_line , result .end_line )
96102 end
97103
98104 local request_id = vim .loop .now ()
@@ -127,7 +133,7 @@ rest.last = function()
127133 end
128134
129135 if config .get (" highlight" ).enabled then
130- request .highlight (LastOpts .bufnr , LastOpts .start_line , LastOpts .end_line )
136+ backend .highlight (LastOpts .bufnr , LastOpts .start_line , LastOpts .end_line )
131137 end
132138
133139 local success_req , req_err = pcall (curl .curl_cmd , LastOpts )
@@ -140,7 +146,7 @@ rest.last = function()
140146 end
141147end
142148
143- rest .request = request
149+ rest .request = backend
144150
145151rest .select_env = function (path )
146152 if path ~= nil then
0 commit comments