Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rest_db = {}
- local HTTP = game:GetService("HttpService")
- local PROXY_ = loadstring(game:HttpGet("https://pastebin.com/raw/mhLdXQWT"))()
- local PROXY
- function rest_db:SetProxyUrl(url)
- self.PROXY = url
- end
- function rest_db:SetProxyKey(key)
- self.PROXY_KEY = key
- end
- function rest_db:SetDBUrl(db_name)
- self.DB = ("https://"..db_name..".restdb.io/rest/")
- end
- function rest_db:SetDBKey(key)
- self.DB_KEY = key
- end
- function rest_db:GetDBUrl()
- if self.DB then
- return self.DB
- else
- return warn(":GetDB() - No database set. Use :SetDB(name) to set your Database.")
- end
- end
- function rest_db:GetDBKey()
- if self.DB_KEY then
- return self.DB_KEY
- else
- return warn("You must set your database's key before getting it.")
- end
- end
- function rest_db:GetProxyUrl()
- if self.PROXY then
- return self.PROXY
- else
- return warn("No proxy URL set.")
- end
- end
- function rest_db:GetProxyKey()
- if self.PROXY_KEY then
- return self.PROXY_KEY
- else
- return warn("No proxy access key set.")
- end
- end
- function rest_db:InitProxy()
- if self.PROXY and self.PROXY_KEY then
- PROXY = PROXY_:New(rest_db:GetProxyUrl(), rest_db:GetProxyKey())
- else
- return warn("You must set the proxy's url and key before trying to initialise the proxy.")
- end
- end
- function rest_db:GetProxy()
- if PROXY then
- return PROXY
- else
- warn("No proxy initiliased.")
- end
- end
- function rest_db:Verify(collection)
- local ver = (rest_db:GetDBUrl()..collection)
- local result = rest_db:GetProxy():Get(ver, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()})
- local returned = HTTP:JSONDecode(result.body)
- local values = #returned
- if values > 0 then
- return true
- else
- return false
- end
- end
- function rest_db:GetCollection(collection)
- if rest_db:Verify(collection) then
- local result = rest_db:GetProxy():Get(rest_db:GetDBUrl()..collection, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()}).body
- local returned = HTTP:JSONDecode(result)
- return returned
- else
- return warn(":GetCollection() - Collection: '"..collection.."' is empty or does not exist.")
- end
- end
- function rest_db:AddDocument(collection, fields)
- local field_data = HTTP:JSONEncode(fields)
- local result = rest_db:GetProxy():Post(rest_db:GetDBUrl()..collection, field_data, Enum.HttpContentType.ApplicationJson, false, {["x-apikey"] = rest_db:GetDBKey()})
- local returned = HTTP:JSONDecode(result.body)
- return returned
- end
- function rest_db:UpdateDocument(collection, id, fields)
- if id then
- local target_ = rest_db:GetDBUrl()..collection.."/"..id
- local field_data = HTTP:JSONEncode(fields)
- local result = rest_db:GetProxy():Put(target_, field_data, Enum.HttpContentType.ApplicationJson, false, {["x-apikey"] = rest_db:GetDBKey()})
- local returned = HTTP:JSONDecode(result.body)
- return returned
- else
- warn(":UpdateDocument() - Collection or document does not exist.")
- end
- end
- function rest_db:DeleteDocument(collection, id)
- if id then
- local target_ = rest_db:GetDBUrl()..collection.."/"..id
- return rest_db:GetProxy():Delete(target_, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()})
- else
- warn(":DeleteDocument() - Collection or document does not exist.")
- end
- end
- return rest_db
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement