Created
June 7, 2023 08:00
-
-
Save RuizuKun-Dev/0a5e98d2b200826bc65267d8417efadc to your computer and use it in GitHub Desktop.
A Module for Interacting with GitHub's Gist API from within Roblox!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local HttpService = game:GetService("HttpService") | |
local API = "https://api.github.com/gists" | |
export type JSONString = string | |
-- Function to encode data into JSON format | |
-- @param data The data to be encoded | |
-- @return The encoded data | |
local function encode(data: table?): JSONString? | |
return typeof(data) == "string" and data or HttpService:JSONEncode(data) | |
end | |
-- Function to decode data from JSON format | |
-- @param data The data to be decoded | |
-- @return The decoded data | |
local function decode(data: JSONString?): table | |
data = data and data ~= "" and data or "[]" | |
return typeof(data) == "table" and data or HttpService:JSONDecode(data) | |
end | |
export type gistUtil = { | |
new: (id: string, token: string) -> Gist, | |
create: (token: string, fileName: string?, content: JSONString?) -> table, | |
delete: (gist: Gist) -> table, | |
read: (gist: Gist) -> table, | |
readFile: (gist: Gist, fileName: string) -> table, | |
write: (gist: Gist, data: JSONString) -> table, | |
writeFile: (gist: Gist, fileName: string, data: JSONString) -> table, | |
} | |
local gistUtil: gistUtil = {} | |
export type Gist = { | |
Url: string, | |
Headers: { ["Authorization"]: string }, | |
Id: string, | |
Token: string, | |
} | |
-- Function to create a new Gist | |
-- @param id The ID of the Gist | |
-- @param token The token for authorization | |
-- @return The new Gist | |
function gistUtil.new(id: string, token: string): Gist | |
return { | |
Url = `{API}/{id}`, | |
Headers = { | |
["Authorization"] = `Bearer {token}`, | |
}, | |
Id = id, | |
Token = token, | |
} | |
end | |
export type Fields = { | |
Url: string, | |
Method: string, | |
Headers: { [string]: string }, | |
Body: table?, | |
} | |
export type Response = { | |
Body: table, | |
Success: boolean, | |
} | |
-- Function to handle HTTP requests | |
-- @param fields The request fields | |
-- @return The response from the request | |
local function handleRequest(fields: Fields): Response | |
local response = HttpService:RequestAsync(fields) | |
local responseBody = decode(response.Body) | |
if response.Success then | |
response.Body = responseBody | |
else | |
warn(`{response.StatusCode}: {responseBody.message}`) | |
end | |
return response | |
end | |
-- Function to create a Gist | |
-- @param token The token for authorization | |
-- @param fileName The name of the file to be created | |
-- @param content The content of the file to be created | |
-- @return The response from the create operation | |
function gistUtil.create(token: string, fileName: string?, content: JSONString?) | |
return handleRequest({ | |
Url = API, | |
Method = "POST", | |
Headers = { | |
["Authorization"] = `Bearer {token}`, | |
}, | |
Body = encode({ | |
files = { | |
[fileName or "README.md"] = { | |
content = encode(content or "Hello, world!"), | |
}, | |
}, | |
}), | |
}) | |
end | |
-- Function to delete a Gist | |
-- @param gist The Gist to be deleted | |
-- @return The response from the delete operation | |
function gistUtil.delete(gist: Gist) | |
return handleRequest({ | |
Url = `{API}/{gist.Id}`, | |
Method = "DELETE", | |
Headers = { | |
["Authorization"] = `Bearer {gist.Token}`, | |
}, | |
}) | |
end | |
-- Function to read a Gist | |
-- @param gist The Gist to be read | |
-- @return The content of the Gist | |
function gistUtil.read(gist: Gist) | |
return handleRequest({ | |
Url = gist.Url, | |
Method = "GET", | |
Headers = gist.Headers, | |
}) | |
end | |
-- Function to read a file from a Gist | |
-- @param gist The Gist containing the file | |
-- @param fileName The name of the file to be read | |
-- @return The content of the file | |
function gistUtil.readFile(gist: Gist, fileName: string) | |
local response = gistUtil.read(gist) | |
return response and decode(response.Body["files"][fileName]["content"]) | |
end | |
-- Function to write to a Gist | |
-- @param gist The Gist to be written to | |
-- @param data The data to be written | |
-- @return The response from the write operation | |
function gistUtil.write(gist: Gist, data: JSONString) | |
return handleRequest({ | |
Url = gist.Url, | |
Method = "POST", | |
Headers = gist.Headers, | |
Body = encode(data), | |
}) | |
end | |
-- Function to write to a file in a Gist | |
-- @param gist The Gist containing the file | |
-- @param fileName The name of the file to be written to | |
-- @param data The data to be written | |
-- @return The response from the write operation | |
function gistUtil.writeFile(gist: Gist, fileName: string, data: JSONString) | |
return gistUtil.write(gist, { | |
["files"] = { | |
[fileName] = { | |
["content"] = encode(data), | |
}, | |
}, | |
}) | |
end | |
return gistUtil |
Appreciate you for making this (i'm VSCPlays in roblox btw) <3
Appreciate you for making this (i'm VSCPlays in roblox btw) <3
wait do u know how we can use this to go over the 1mb limit?
wait do u know how we can use this to go over the 1mb limit?
idk, but I’m wondering how do I add plugin support…
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
W developer