G2A Many GEOs
SHARE
TWEET

RestDBApi

XZTablets Apr 8th, 2020 (edited) 188 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local rest_db = {}
  2.  
  3. local HTTP = game:GetService("HttpService")
  4. local PROXY_ = loadstring(game:HttpGet("https://pastebin.com/raw/mhLdXQWT"))()
  5.  
  6. local PROXY
  7.  
  8. function rest_db:SetProxyUrl(url)
  9.     self.PROXY = url
  10. end
  11.  
  12. function rest_db:SetProxyKey(key)
  13.     self.PROXY_KEY = key
  14. end
  15.  
  16. function rest_db:SetDBUrl(db_name)
  17.     self.DB = ("https://"..db_name..".restdb.io/rest/")
  18. end
  19.  
  20. function rest_db:SetDBKey(key)
  21.     self.DB_KEY = key
  22. end
  23.  
  24. function rest_db:GetDBUrl()
  25.     if self.DB then
  26.         return self.DB
  27.     else
  28.         return warn(":GetDB() - No database set. Use :SetDB(name) to set your Database.")
  29.     end
  30. end
  31.  
  32. function rest_db:GetDBKey()
  33.     if self.DB_KEY then
  34.         return self.DB_KEY
  35.     else
  36.         return warn("You must set your database's key before getting it.")
  37.     end
  38. end
  39.  
  40. function rest_db:GetProxyUrl()
  41.     if self.PROXY then
  42.         return self.PROXY
  43.     else
  44.         return warn("No proxy URL set.")
  45.     end
  46. end
  47.  
  48. function rest_db:GetProxyKey()
  49.     if self.PROXY_KEY then
  50.         return self.PROXY_KEY
  51.     else
  52.         return warn("No proxy access key set.")
  53.     end
  54. end
  55.  
  56. function rest_db:InitProxy()
  57.     if self.PROXY and self.PROXY_KEY then
  58.         PROXY = PROXY_:New(rest_db:GetProxyUrl(), rest_db:GetProxyKey())
  59.     else
  60.         return warn("You must set the proxy's url and key before trying to initialise the proxy.")
  61.     end
  62. end
  63.  
  64. function rest_db:GetProxy()
  65.     if PROXY then
  66.         return PROXY
  67.     else
  68.         warn("No proxy initiliased.")
  69.     end
  70. end
  71.  
  72. function rest_db:Verify(collection)
  73.     local ver = (rest_db:GetDBUrl()..collection)
  74.     local result = rest_db:GetProxy():Get(ver, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()})
  75.     local returned = HTTP:JSONDecode(result.body)
  76.     local values = #returned
  77.     if values > 0 then
  78.         return true
  79.     else
  80.         return false
  81.     end
  82. end
  83.  
  84. function rest_db:GetCollection(collection)
  85.     if rest_db:Verify(collection) then
  86.         local result = rest_db:GetProxy():Get(rest_db:GetDBUrl()..collection, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()}).body
  87.         local returned = HTTP:JSONDecode(result)
  88.         return returned
  89.     else
  90.         return warn(":GetCollection() - Collection: '"..collection.."' is empty or does not exist.")
  91.     end
  92. end
  93.  
  94. function rest_db:AddDocument(collection, fields)
  95.     local field_data = HTTP:JSONEncode(fields)
  96.     local result = rest_db:GetProxy():Post(rest_db:GetDBUrl()..collection, field_data, Enum.HttpContentType.ApplicationJson, false, {["x-apikey"] = rest_db:GetDBKey()})
  97.     local returned = HTTP:JSONDecode(result.body)
  98.     return returned
  99. end
  100.  
  101. function rest_db:UpdateDocument(collection, id, fields)
  102.     if id then
  103.         local target_ = rest_db:GetDBUrl()..collection.."/"..id
  104.         local field_data = HTTP:JSONEncode(fields)
  105.         local result = rest_db:GetProxy():Put(target_, field_data, Enum.HttpContentType.ApplicationJson, false, {["x-apikey"] = rest_db:GetDBKey()})
  106.         local returned = HTTP:JSONDecode(result.body)
  107.         return returned
  108.     else
  109.         warn(":UpdateDocument() - Collection or document does not exist.")
  110.     end
  111. end
  112.  
  113. function rest_db:DeleteDocument(collection, id)
  114.     if id then
  115.         local target_ = rest_db:GetDBUrl()..collection.."/"..id
  116.         return rest_db:GetProxy():Delete(target_, true, {["cache-control"] = "no-cache", ["x-apikey"] = rest_db:GetDBKey()})
  117.     else
  118.         warn(":DeleteDocument() - Collection or document does not exist.")
  119.     end
  120. end
  121.  
  122. return rest_db
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top