Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add REST API functions and metatable for get #872

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion lua/octo/gh/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ end
---Insert the options into the args table
---@param args table the arguments table
---@param options table the options to insert
---@param replace table key value pairs to replace in the key of the options
---@param replace table|nil key value pairs to replace in the key of the options
---@return table the updated args table
M.insert_args = function(args, options, replace)
replace = replace or {}
Expand Down Expand Up @@ -283,10 +283,49 @@ function M.graphql(opts)
}
end

local rest = function(method, opts)
local run_opts = opts.opts or {}
local args = { "api" }
if method ~= nil then
table.insert(args, "--method")
table.insert(args, method)
end

opts.opts = nil
args = M.insert_args(args, opts)

M.run {
args = args,
mode = run_opts.mode,
cb = run_opts.cb,
stream_cb = run_opts.stream_cb,
headers = run_opts.headers,
hostname = run_opts.hostname,
}
end

M.api = {
graphql = M.graphql,
get = function(opts)
return rest("GET", opts)
end,
post = function(opts)
return rest("POST", opts)
end,
patch = function(opts)
return rest("PATCH", opts)
end,
delete = function(opts)
return rest("DELETE", opts)
end,
}

setmetatable(M.api, {
__call = function(_, opts)
return rest(nil, opts)
end,
})

local create_subcommand = function(command)
local subcommand = {}
subcommand.command = command
Expand Down
Loading