Skip to content

Commit

Permalink
add REST API functions and metatable for get
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Feb 20, 2025
1 parent dc67d3f commit 4ed1ab3
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 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,45 @@ function M.graphql(opts)
}
end

local rest = function(method, opts)
local run_opts = opts.opts or {}
local args = { "api", "--method", method }

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 M.api.get(opts)
end,
})

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

0 comments on commit 4ed1ab3

Please sign in to comment.