From dd4d542d7e1348f578d1c2184336252ab8cbefbf Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 3 Dec 2018 13:54:54 -0800 Subject: [PATCH] Clean up and start work on Epiphany plugin --- plugin/src/Http.lua | 43 ++++++++++++++++++++++--------------- plugin/src/HttpResponse.lua | 24 +++++++++++++++++++++ plugin/src/Logging.lua | 21 ++++++++++++++++++ 3 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 plugin/src/Logging.lua diff --git a/plugin/src/Http.lua b/plugin/src/Http.lua index 2087cee7..e8753c0e 100644 --- a/plugin/src/Http.lua +++ b/plugin/src/Http.lua @@ -18,38 +18,47 @@ local Http = {} function Http.get(url) dprint("\nGET", url) + return Promise.new(function(resolve, reject) - spawn(function() - local ok, result = pcall(function() - return HttpService:GetAsync(url, true) + coroutine.wrap(function() + local success, response = pcall(function() + return HttpService:RequestAsync({ + Url = url, + Method = "GET", + }) end) - if ok then - dprint("\t", result) - resolve(HttpResponse.new(result)) + if success then + dprint("\t", response) + resolve(HttpResponse.fromRobloxResponse(response)) else - reject(HttpError.fromErrorString(result)) + reject(HttpError.fromErrorString(response)) end - end) + end)() end) end function Http.post(url, body) dprint("\nPOST", url) - dprint(body) + dprint(body); + return Promise.new(function(resolve, reject) - spawn(function() - local ok, result = pcall(function() - return HttpService:PostAsync(url, body) + coroutine.wrap(function() + local success, response = pcall(function() + return HttpService:RequestAsync({ + Url = url, + Method = "POST", + Body = body, + }) end) - if ok then - dprint("\t", result) - resolve(HttpResponse.new(result)) + if success then + dprint("\t", response) + resolve(HttpResponse.fromRobloxResponse(response)) else - reject(HttpError.fromErrorString(result)) + reject(HttpError.fromErrorString(response)) end - end) + end)() end) end diff --git a/plugin/src/HttpResponse.lua b/plugin/src/HttpResponse.lua index 0b85db94..c9a210ca 100644 --- a/plugin/src/HttpResponse.lua +++ b/plugin/src/HttpResponse.lua @@ -1,8 +1,18 @@ local HttpService = game:GetService("HttpService") +local stringTemplate = [[ +HttpResponse { + code: %d + body: %s +}]] + local HttpResponse = {} HttpResponse.__index = HttpResponse +function HttpResponse:__tostring() + return stringTemplate:format(self.code, self.body) +end + function HttpResponse.new(body) local response = { body = body, @@ -13,6 +23,20 @@ function HttpResponse.new(body) return response end +function HttpResponse.fromRobloxResponse(response) + local self = { + body = response.Body, + code = response.StatusCode, + headers = response.Headers, + } + + return setmetatable(self, HttpResponse) +end + +function HttpResponse:isSuccess() + return self.code >= 200 and self.code < 300 +end + function HttpResponse:json() return HttpService:JSONDecode(self.body) end diff --git a/plugin/src/Logging.lua b/plugin/src/Logging.lua new file mode 100644 index 00000000..0cedb620 --- /dev/null +++ b/plugin/src/Logging.lua @@ -0,0 +1,21 @@ +local enabledByTest = false + +local function isEnabled() + return _G.ROJO_LOG or enabledByTest +end + +local Logging = {} + +function Logging.info(template, ...) + if isEnabled() then + print("[Rojo] " .. template:format(...)) + end +end + +function Logging.warn(template, ...) + if isEnabled() then + warn("[Rojo] " .. template:format(...)) + end +end + +return Logging \ No newline at end of file