mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Added error checking to Lua libs,
added http_failure event to http.requestAsync
This commit is contained in:
parent
719b66fdb0
commit
5e0183cc1c
4 changed files with 43 additions and 6 deletions
|
@ -1,6 +1,11 @@
|
||||||
local timer = require("timer")
|
local timer = require("timer")
|
||||||
|
local expect = require("expect").expect
|
||||||
|
local range = require("expect").range
|
||||||
|
|
||||||
function timer.sleep(n)
|
function timer.sleep(n)
|
||||||
|
expect(1, n, "number")
|
||||||
|
range(1, 1)
|
||||||
|
|
||||||
local timerId = timer.start(n)
|
local timerId = timer.start(n)
|
||||||
repeat
|
repeat
|
||||||
local _, par = coroutine.yield("timer")
|
local _, par = coroutine.yield("timer")
|
||||||
|
|
|
@ -1,19 +1,43 @@
|
||||||
local http = require("http")
|
local http = require("http")
|
||||||
local event = require("event")
|
local event = require("event")
|
||||||
|
local expect = require("expect").expect
|
||||||
|
|
||||||
function http.request(url, body, headers, options)
|
function http.request(url, body, headers, options)
|
||||||
|
expect(1, url, "string")
|
||||||
|
expect(2, body, "string", "nil")
|
||||||
|
expect(3, headers, "table", "nil")
|
||||||
|
expect(4, options, "table", "nil")
|
||||||
|
|
||||||
|
if not http.checkURL(url) then
|
||||||
|
return nil, "Invalid URL"
|
||||||
|
end
|
||||||
|
|
||||||
local requestId = http.requestAsync(url, body, headers, options)
|
local requestId = http.requestAsync(url, body, headers, options)
|
||||||
local _, response
|
local ev, id, par
|
||||||
repeat
|
repeat
|
||||||
_, id, response = event.pull("http_response")
|
ev, id, par = event.pull("http_response", "http_failure")
|
||||||
until id == requestId
|
until id == requestId
|
||||||
return response
|
|
||||||
|
if ev == "http_failure" then
|
||||||
|
return nil, par
|
||||||
|
end
|
||||||
|
|
||||||
|
return par
|
||||||
end
|
end
|
||||||
|
|
||||||
function http.get(url, headers, options)
|
function http.get(url, headers, options)
|
||||||
|
expect(1, url, "string")
|
||||||
|
expect(2, headers, "table", "nil")
|
||||||
|
expect(3, options, "table", "nil")
|
||||||
|
|
||||||
return http.request(url, nil, headers, options)
|
return http.request(url, nil, headers, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
function http.post(url, body, headers, options)
|
function http.post(url, body, headers, options)
|
||||||
|
expect(1, url, "string")
|
||||||
|
expect(2, body, "string", "nil")
|
||||||
|
expect(3, headers, "table", "nil")
|
||||||
|
expect(4, options, "table", "nil")
|
||||||
|
|
||||||
return http.request(url, body, headers, options)
|
return http.request(url, body, headers, options)
|
||||||
end
|
end
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
|
|
||||||
"HTTP": {
|
"HTTP": {
|
||||||
|
"Enable": true,
|
||||||
"Blacklist": [],
|
"Blacklist": [],
|
||||||
"WebSockets": {
|
"WebSockets": {
|
||||||
"Enable": true,
|
"Enable": true,
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using Capy64.LuaRuntime.Extensions;
|
using Capy64.LuaRuntime.Extensions;
|
||||||
using Capy64.LuaRuntime.Handlers;
|
using Capy64.LuaRuntime.Handlers;
|
||||||
using KeraLua;
|
using KeraLua;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
@ -15,7 +16,6 @@ public class HTTP : IPlugin
|
||||||
private static IGame _game;
|
private static IGame _game;
|
||||||
private static HttpClient _client;
|
private static HttpClient _client;
|
||||||
private static long RequestId;
|
private static long RequestId;
|
||||||
private static List<ReadHandle> ReadHandles = new();
|
|
||||||
|
|
||||||
private readonly LuaRegister[] HttpLib = new LuaRegister[]
|
private readonly LuaRegister[] HttpLib = new LuaRegister[]
|
||||||
{
|
{
|
||||||
|
@ -31,7 +31,7 @@ public class HTTP : IPlugin
|
||||||
},
|
},
|
||||||
new(),
|
new(),
|
||||||
};
|
};
|
||||||
public HTTP(IGame game)
|
public HTTP(IGame game, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_game = game;
|
_game = game;
|
||||||
RequestId = 0;
|
RequestId = 0;
|
||||||
|
@ -60,6 +60,7 @@ public class HTTP : IPlugin
|
||||||
private static int L_Request(IntPtr state)
|
private static int L_Request(IntPtr state)
|
||||||
{
|
{
|
||||||
var L = Lua.FromIntPtr(state);
|
var L = Lua.FromIntPtr(state);
|
||||||
|
|
||||||
var request = new HttpRequestMessage();
|
var request = new HttpRequestMessage();
|
||||||
|
|
||||||
var url = L.CheckString(1);
|
var url = L.CheckString(1);
|
||||||
|
@ -153,6 +154,13 @@ public class HTTP : IPlugin
|
||||||
var reqTask = _client.SendAsync(request);
|
var reqTask = _client.SendAsync(request);
|
||||||
reqTask.ContinueWith(async (task) =>
|
reqTask.ContinueWith(async (task) =>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if(task.IsFaulted || task.IsCanceled)
|
||||||
|
{
|
||||||
|
_game.LuaRuntime.PushEvent("http_failure", requestId, task.Exception?.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var response = await task;
|
var response = await task;
|
||||||
/*object content;
|
/*object content;
|
||||||
if ((bool)options["binary"])
|
if ((bool)options["binary"])
|
||||||
|
|
Loading…
Reference in a new issue