http.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ---@meta
  2. ---HTTP Requests
  3. ----------------
  4. ---@class mt.HTTPRequest
  5. local request = {}
  6. ---@type string
  7. request.url = nil
  8. -- Timeout for request to be completed in seconds.
  9. -- Default depends on engine settings.
  10. ---@type number
  11. request.timeout = nil
  12. -- The http method to use. Defaults to `"GET"`.
  13. ---@type '"GET"'|'"POST"'|'"PUT"'|'"DELETE"'
  14. request.method = nil
  15. -- Data for the POST, PUT or DELETE request.
  16. --
  17. -- Accepts both a string and a table.
  18. -- If a table is specified, encodes table as `x-www-form-urlencoded` key-value pairs.
  19. ---@type string|table
  20. request.data = nil
  21. -- Optional, if specified replaces the default minetest user agent with given string.
  22. ---@type string
  23. request.user_agent = nil
  24. -- Optional, if specified adds additional headers to the HTTP request.
  25. --
  26. -- You must make sure that the header strings follow HTTP specification ("Key: Value").
  27. ---@type string[]
  28. request.extra_headers = nil
  29. -- Optional, if true performs a multipart HTTP request.
  30. --
  31. -- Default is false.
  32. --
  33. -- POST only, data must be array.
  34. ---@type boolean
  35. request.multipart = nil
  36. ---@class mt.HTTPRequestResult
  37. local result = {}
  38. -- If true, the request has finished (either succeeded, failed or timed out).
  39. ---@type boolean
  40. result.completed = nil
  41. -- If true, the request was successful.
  42. ---@type boolean
  43. result.succeeded = nil
  44. -- If true, the request timed out.
  45. ---@type boolean
  46. result.timeout = nil
  47. -- HTTP status code.
  48. ---@type integer
  49. result.code = nil
  50. -- Responce data.
  51. ---@type string
  52. result.data = nil
  53. ---@class mt.HTTPApiTable
  54. local api = {}
  55. -- Performs given request asynchronously and calls callback upon completion.
  56. --
  57. -- Use this HTTP function if you are unsure, the others are for advanced use.
  58. ---@param req mt.HTTPRequest
  59. ---@param callback fun(res: mt.HTTPRequestResult)
  60. function api.fetch(req, callback) end
  61. -- Performs given request asynchronously
  62. -- and returns handle for `HTTPApiTable.fetch_async_get`.
  63. ---@param req mt.HTTPRequest
  64. ---@return any
  65. function api.fetch_async(req) end
  66. -- Return response data for given asynchronous HTTP request.
  67. ---@param handle any
  68. ---@return mt.HTTPRequestResult
  69. function api.fetch_async_get(handle) end
  70. -- Returns `HTTPApiTable` containing http functions if the calling mod has been
  71. -- granted access by being listed in the `secure.http_mods`
  72. -- or `secure.trusted_mods` setting, otherwise returns `nil`.
  73. --
  74. -- Only works at init time and must be called from the mod's main scope (not from a function).
  75. --
  76. -- Function only exists if minetest server was built with cURL support.
  77. --
  78. -- **DO NOT ALLOW ANY OTHER MODS TO ACCESS THE RETURNED TABLE, STORE IT IN A LOCAL VARIABLE!**
  79. ---@return mt.HTTPApiTable
  80. function minetest.request_http_api() end