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

Plugins HTTP api type mismatch #5493

Closed
4 tasks done
2547techno opened this issue Jul 5, 2024 · 1 comment · Fixed by #5494
Closed
4 tasks done

Plugins HTTP api type mismatch #5493

2547techno opened this issue Jul 5, 2024 · 1 comment · Fixed by #5494
Labels
bug Something isn't working as intended, or works in a confusing/unintuitive way for the user Feature: EXPERIMENTAL Plugins Stuff related to EXPERIMENTAL Lua plugin support

Comments

@2547techno
Copy link
Contributor

2547techno commented Jul 5, 2024

Checklist

  • I'm reporting a problem with Chatterino
  • I've verified that I'm running the most recent nightly build or stable release
  • I've looked for my problem on the wiki
  • I've searched the issues and pull requests for similar looking reports

Describe your issue

HTTP api type definition in the generated plugin-meta.lua file defines HTTPResponse.status as an integer. However, when used in a plugin it is a function that (almost) follows the docs:

local url = "https://httpbin.org/get"

local function test(ctx)
    local req = c2.HTTPRequest.create(c2.HTTPMethod.Get, url)
    req:on_success(function(res)
        print(res.status)
        -- [9960] chatterino.lua: [test:test] function: 00007FF7EE7DF120
    end)
    req:execute()
end

c2.register_command("/test", test)

"Almost", because it says the data() function take no parameters, but after using it I realized it needs an HTTPResponse type table is required as the parameter. The following code is what works for me currently:

local url = "https://httpbin.org/get"

local function test(ctx)
    local req = c2.HTTPRequest.create(c2.HTTPMethod.Get, url)
    req:on_success(function(res)
        print(res.status(res))
        -- [9960] chatterino.lua: [test:test] 200
    end)
    req:execute()
end

c2.register_command("/test", test)

When calling the status function with an incorrect/without a parameter, the following errors are returned:

local url = "https://httpbin.org/get"

local function test(ctx)
    local req = c2.HTTPRequest.create(c2.HTTPMethod.Get, url)
    req:on_success(function(res)

        local _, error1 = pcall(function() return res.status() end)
        print(error1)
        -- [9960] chatterino.lua: [test:test] .../AppData/Roaming/Chatterino2/Plugins/test/init.lua:7: Called c2.HTTPResponse method without a request object
        local _, error2 = pcall(function() return res.status(req) end)
        print(error2)
        -- [9960] chatterino.lua: [test:test] .../AppData/Roaming/Chatterino2/Plugins/test/init.lua:7: bad argument #1 to 'status' (c2.HTTPResponse expected, got c2.HTTPRequest)


    end)
    req:execute()
end

c2.register_command("/test", test)

I figured there was a mistake somewhere, but I'm not sure which definition would be correct. Could the docs be fixed to reflect this?

Screenshots

No response

OS and Chatterino Version

Chatterino Nightly 2.5.1 (commit 7bfb5ac) built on 2024-06-23 with Qt 6.7.1, MSVC 194033811 Running on Windows 10 Version 22H2, kernel: 10.0.19045

@2547techno 2547techno added the issue-report An issue reported by a user. label Jul 5, 2024
@Mm2PL
Copy link
Collaborator

Mm2PL commented Jul 5, 2024

This is a mistake with the meta comments in src/controllers/plugins/api/HTTPRequest.hpp.

This comment should be deleted:

/**
 * @lua@class HTTPResponse
 * @lua@field data string Data received from the server
 * @lua@field status integer? HTTP Status code returned by the server
 * @lua@field error string A somewhat human readable description of an error if such happened
 */

The order of includes in src/controllers/plugins/LuaAPI.hpp should probably be switched from:

/**
 * @includefile common/Channel.hpp
 * @includefile controllers/plugins/api/ChannelRef.hpp
 * @includefile controllers/plugins/api/HTTPRequest.hpp
 * @includefile controllers/plugins/api/HTTPResponse.hpp
 * @includefile common/network/NetworkCommon.hpp
 */

to:

/**
 * @includefile common/Channel.hpp
 * @includefile controllers/plugins/api/ChannelRef.hpp
 * @includefile controllers/plugins/api/HTTPResponse.hpp
 * @includefile controllers/plugins/api/HTTPRequest.hpp
 * @includefile common/network/NetworkCommon.hpp
 */

Swapping HTTPRequest and HTTPResponse. Then the luals meta file should be regenerated.

BTW instead of res.status(res) you can use res:status().

@Mm2PL Mm2PL added bug Something isn't working as intended, or works in a confusing/unintuitive way for the user and removed issue-report An issue reported by a user. labels Jul 5, 2024
@Felanbird Felanbird added the Feature: EXPERIMENTAL Plugins Stuff related to EXPERIMENTAL Lua plugin support label Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as intended, or works in a confusing/unintuitive way for the user Feature: EXPERIMENTAL Plugins Stuff related to EXPERIMENTAL Lua plugin support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants