Skip to content

Commit

Permalink
feat: dont display binary output in answer
Browse files Browse the repository at this point in the history
I was testing an API that returned a zip file and it was getting out of
hand so I added an is_binary_content_type to control whether to display
the content type.
Ideally I would have liked the display to be a default postprocess step so
that it gets more generic and let the user more control but this needs
deeper refactoring.
  • Loading branch information
teto committed Jun 6, 2023
1 parent e60aece commit 5ebe35f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
12 changes: 9 additions & 3 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local function create_callback(method, url, script_str)
-- get content type
for _, header in ipairs(res.headers) do
if string.lower(header):find("^content%-type") then
content_type = header:match("application/(%l+)") or header:match("text/(%l+)")
content_type = header:match("application/([-a-z]+)") or header:match("text/(%l+)")
break
end
end
Expand Down Expand Up @@ -150,9 +150,15 @@ local function create_callback(method, url, script_str)
end

-- append response container
res.body = "#+RESPONSE\n" .. res.body .. "\n#+END"
local buf_content = "#+RESPONSE\n"
if utils.is_binary_content_type(content_type) then
buf_content = buf_content .. "Binary answer"
else
buf_content = buf_content .. res.body
end
buf_content = buf_content .. "\n#+END"

local lines = utils.split(res.body, "\n")
local lines = utils.split(buf_content, "\n")
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)

Expand Down
3 changes: 1 addition & 2 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local function get_importfile_name(bufnr, start_line, stop_line)
local fileimport_spliced
fileimport_line = vim.api.nvim_buf_get_lines(bufnr, import_line - 1, import_line, false)
fileimport_string =
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
string.gsub(fileimport_line[1], "<", "", 1):gsub("^%s+", ""):gsub("%s+$", "")
fileimport_spliced = utils.replace_vars(fileimport_string)
if path:new(fileimport_spliced):is_absolute() then
return fileimport_spliced
Expand Down Expand Up @@ -89,7 +89,6 @@ local function get_body(bufnr, start_line, stop_line, has_json)
end
end


return body
end

Expand Down
8 changes: 8 additions & 0 deletions lua/rest-nvim/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ math.randomseed(os.time())

local M = {}

M.binary_content_types = {
"octet-stream",
}

M.is_binary_content_type = function(content_type)
return vim.tbl_contains(M.binary_content_types, content_type)
end

-- move_cursor moves the cursor to the desired position in the provided buffer
-- @param bufnr Buffer number, a.k.a id
-- @param line the desired line
Expand Down

0 comments on commit 5ebe35f

Please sign in to comment.