@@ -2,19 +2,20 @@ local backend = require("rest-nvim.request")
2
2
local config = require (" rest-nvim.config" )
3
3
local curl = require (" rest-nvim.curl" )
4
4
local log = require (" plenary.log" ).new ({ plugin = " rest.nvim" })
5
+ local utils = require (" rest-nvim.utils" )
6
+ local path = require (" plenary.path" )
5
7
6
8
local rest = {}
7
9
local Opts = {}
8
10
local defaultRequestOpts = {
9
- verbose = false ,
10
- highlight = false ,
11
- engine = ' classic'
12
- }
11
+ verbose = false ,
12
+ highlight = false ,
13
+ }
13
14
14
15
local LastOpts = {}
16
+
15
17
rest .setup = function (user_configs )
16
18
config .set (user_configs or {})
17
-
18
19
end
19
20
20
21
-- run will retrieve the required request information from the current buffer
37
38
-- @param string filename to load
38
39
-- @param opts table
39
40
-- 1. keep_going boolean keep running even when last request failed
40
- -- 2. verbose boolean
41
+ -- 2. verbose boolean
41
42
rest .run_file = function (filename , opts )
42
43
log .info (" Running file :" .. filename )
43
44
opts = vim .tbl_deep_extend (
@@ -54,38 +55,113 @@ rest.run_file = function(filename, opts)
54
55
55
56
local requests = backend .buf_list_requests (new_buf )
56
57
for _ , req in pairs (requests ) do
57
- vim .pretty_print (" Request:" )
58
- vim .pretty_print (req )
59
58
rest .run_request (req , opts )
60
59
end
61
60
62
61
return true
63
62
end
64
63
64
+ -- replace variables in header values
65
+ local function splice_headers (headers )
66
+ for name , value in pairs (headers ) do
67
+ headers [name ] = utils .replace_vars (value )
68
+ end
69
+ return headers
70
+ end
71
+
72
+ -- return the spliced/resolved filename
73
+ -- @param string the filename w/o variables
74
+ local function load_external_payload (fileimport_string )
75
+ local fileimport_spliced = utils .replace_vars (fileimport_string )
76
+ if path :new (fileimport_spliced ):is_absolute () then
77
+ return fileimport_spliced
78
+ else
79
+ local file_dirname = vim .fn .expand (" %:p:h" )
80
+ local file_name = path :new (path :new (file_dirname ), fileimport_spliced )
81
+ return file_name :absolute ()
82
+ end
83
+ end
84
+
85
+
86
+ -- @param headers table HTTP headers
87
+ -- @param payload table of the form { external = bool, filename_tpl= path, body_tpl = string }
88
+ -- with body_tpl an array of lines
89
+ local function splice_body (headers , payload )
90
+ local external_payload = payload .external
91
+ local lines -- array of strings
92
+ if external_payload then
93
+ local importfile = load_external_payload (payload .filename_tpl )
94
+ if not utils .file_exists (importfile ) then
95
+ error (" import file " .. importfile .. " not found" )
96
+ end
97
+ -- TODO we dont necessarily want to load the file, it can be slow
98
+ -- https://github.com/rest-nvim/rest.nvim/issues/203
99
+ lines = utils .read_file (importfile )
100
+ else
101
+ lines = payload .body_tpl
102
+ end
103
+ local content_type = " "
104
+ for key , val in pairs (headers ) do
105
+ if string.lower (key ) == " content-type" then
106
+ content_type = val
107
+ break
108
+ end
109
+ end
110
+ local has_json = content_type :find (" application/[^ ]*json" )
111
+
112
+ local body = " "
113
+ local vars = utils .read_variables ()
114
+ -- nvim_buf_get_lines is zero based and end-exclusive
115
+ -- but start_line and stop_line are one-based and inclusive
116
+ -- magically, this fits :-) start_line is the CRLF between header and body
117
+ -- which should not be included in the body, stop_line is the last line of the body
118
+ for _ , line in ipairs (lines ) do
119
+ body = body .. utils .replace_vars (line , vars )
120
+ end
121
+
122
+ local is_json , json_body = pcall (vim .json .decode , body )
123
+
124
+ if is_json and json_body then
125
+ if has_json then
126
+ -- convert entire json body to string.
127
+ return vim .fn .json_encode (json_body )
128
+ else
129
+ -- convert nested tables to string.
130
+ for key , val in pairs (json_body ) do
131
+ if type (val ) == " table" then
132
+ json_body [key ] = vim .fn .json_encode (val )
133
+ end
134
+ end
135
+ return vim .fn .json_encode (json_body )
136
+ end
137
+ end
138
+ end
65
139
66
140
-- run will retrieve the required request information from the current buffer
67
141
-- and then execute curl
68
142
-- @param req table see validate_request to check the expected format
69
143
-- @param opts table
70
144
-- 1. keep_going boolean keep running even when last request failed
71
145
rest .run_request = function (req , opts )
72
- -- TODO rename result to req
146
+ -- TODO rename result to request
73
147
local result = req
74
148
opts = vim .tbl_deep_extend (
75
149
" force" , -- use value from rightmost map
76
150
defaultRequestOpts ,
77
151
opts or {}
78
152
)
79
153
154
+ -- body =
155
+
80
156
Opts = {
81
157
method = result .method :lower (),
82
158
url = result .url ,
83
159
-- plenary.curl can't set http protocol version
84
160
-- http_version = result.http_version,
85
- headers = result .headers ,
161
+ headers = splice_headers ( result .headers ) ,
86
162
raw = config .get (" skip_ssl_verification" ) and vim .list_extend (result .raw , { " -k" })
87
163
or result .raw ,
88
- body = result .body ,
164
+ body = splice_body ( result .headers , result . body ) ,
89
165
dry_run = opts .verbose ,
90
166
bufnr = result .bufnr ,
91
167
start_line = result .start_line ,
@@ -103,18 +179,21 @@ rest.run_request = function(req, opts)
103
179
104
180
local request_id = vim .loop .now ()
105
181
local data = {
106
- requestId = request_id ,
107
- request = req
108
- }
182
+ requestId = request_id ,
183
+ request = req ,
184
+ }
109
185
110
186
vim .api .nvim_exec_autocmds (" User" , {
111
- pattern = " RestStartRequest" ,
112
- modeline = false ,
113
- data = data
114
- })
187
+ pattern = " RestStartRequest" ,
188
+ modeline = false ,
189
+ data = data ,
190
+ })
115
191
local success_req , req_err = pcall (curl .curl_cmd , Opts )
116
- vim .api .nvim_exec_autocmds (" User" , { pattern = " RestStopRequest" , modeline = false ,
117
- data = vim .tbl_extend (" keep" , { status = success_req , message = req_err }, data ) })
192
+ vim .api .nvim_exec_autocmds (" User" , {
193
+ pattern = " RestStopRequest" ,
194
+ modeline = false ,
195
+ data = vim .tbl_extend (" keep" , { status = success_req , message = req_err }, data ),
196
+ })
118
197
119
198
if not success_req then
120
199
vim .api .nvim_err_writeln (
@@ -148,10 +227,10 @@ end
148
227
149
228
rest .request = backend
150
229
151
- rest .select_env = function (path )
230
+ rest .select_env = function (env_file )
152
231
if path ~= nil then
153
- vim .validate ({ path = { path , " string" } })
154
- config .set ({ env_file = path })
232
+ vim .validate ({ env_file = { env_file , " string" } })
233
+ config .set ({ env_file = env_file })
155
234
else
156
235
print (" No path given" )
157
236
end
0 commit comments