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

Helper to construct REST endpoints #821

Closed
wd60622 opened this issue Jan 25, 2025 · 3 comments · Fixed by #888
Closed

Helper to construct REST endpoints #821

wd60622 opened this issue Jan 25, 2025 · 3 comments · Fixed by #888
Labels

Comments

@wd60622
Copy link
Collaborator

wd60622 commented Jan 25, 2025

Some ideas for the API:

local endpoint = construct_endpoint {
    path = "/repos/{owner}/{repo}/notifications/",
    params = {
        owner = "octocat",
        repo = "Hello-World"
    }
}
--- Or if reusability is needed
local endpoint = Endpoint("/repos/{owner}/{repo}/notifications/")

endpoint:construct {
    owner = "octocat",
    repo = "Hello-World"
}   

Related to #751

@wd60622
Copy link
Collaborator Author

wd60622 commented Feb 23, 2025

Another idea is to have this part of the rest function

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)
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.get = function(opts)
return rest("GET", opts)
end
M.api.post = function(opts)
return rest("POST", opts)
end
M.api.patch = function(opts)
return rest("PATCH", opts)
end
M.api.delete = function(opts)
return rest("DELETE", opts)
end
M.api.put = function(opts)
return rest("PUT", opts)
end

For example, another parameter in opts, say format

Then the API becomes:

local gh = require "octo.gh"

local endpoint = "/repos/{owner}/{repo}/notifications"

gh.api.get {
    endpoint, 
    format = {
        owner = "pwntester",
        repo = "octo.nvim"
    },
}

And the user doesn't think about string interpolation at all.

Thoughts on this? @ldelossa

@ldelossa
Copy link
Contributor

I like it! It could be handy to have "prebaked" "get", "post", etc... handlers with a simple syntax.

Im assuming that a callback can be provided to the "get" function as well?

@wd60622
Copy link
Collaborator Author

wd60622 commented Feb 23, 2025

No formatting would likely be default behavior. The way it is set up now allows for all gh.run parameters in the opts parameters. Others are dynamically generated

See here for reference: #876

The only change is "params" or "format" would be a designated parameter to format any endpoint placeholders

@wd60622 wd60622 added the request discussion Feedback is welcomed! label Feb 23, 2025
@wd60622 wd60622 linked a pull request Feb 23, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants