Chatterino / chatterino2

Chat client for https://twitch.tv
MIT License
2.04k stars 449 forks source link

Plugins HTTP api type mismatch #5493

Closed 2547techno closed 3 months ago

2547techno commented 3 months ago

Checklist

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 7bfb5ac0) built on 2024-06-23 with Qt 6.7.1, MSVC 194033811 Running on Windows 10 Version 22H2, kernel: 10.0.19045

Mm2PL commented 3 months ago

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().