Skip to content

Commit

Permalink
Merge pull request #24 from fenix-hub/dev
Browse files Browse the repository at this point in the history
add storage support
  • Loading branch information
fenix-hub authored Jul 9, 2021
2 parents e84feb1 + 78684bc commit 1853e9a
Show file tree
Hide file tree
Showing 12 changed files with 562 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A lightweight addon which integrates Supabase APIs for Godot Engine out of the b
- [x] Authentication (/auth)
- [x] Database (/database)
- [x] Realtime (/realtime)
- [ ] Storage (/storage)
- [x] Storage (/storage)

### examples and demos
A collection of examples and live demos is available at [*fenix-hub/godot-engine.supabase-examples*](https://github.com/fenix-hub/godot-engine.supabase-examples), both with source code and exported binaries.
Expand Down
7 changes: 7 additions & 0 deletions addons/supabase/Auth/auth.gd
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func sign_in(email : String, password : String = "") -> AuthTask:
_process_task(auth_task)
return auth_task


# Sign in as an anonymous user
func sign_in_anonymous() -> void:
_auth = _config.supabaseKey
_bearer[0] = _bearer[0] % _auth
emit_signal("signed_in", null)

# Sign in with a Provider
# @provider = Providers.PROVIDER
func sign_in_with_provider(provider : String, grab_from_browser : bool = true, port : int = 3000) -> void:
Expand Down
8 changes: 5 additions & 3 deletions addons/supabase/Database/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func _init(conf : Dictionary, head : PoolStringArray) -> void:
func query(supabase_query : SupabaseQuery) -> DatabaseTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + supabase_query.build_query()
var task : DatabaseTask = DatabaseTask.new(
var task : DatabaseTask = DatabaseTask.new()
task._setup(
supabase_query,
supabase_query.request,
endpoint,
Expand All @@ -39,7 +40,8 @@ func query(supabase_query : SupabaseQuery) -> DatabaseTask:
func rpc(function_name : String, arguments : Dictionary = {}, supabase_query : SupabaseQuery = null) -> DatabaseTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "rpc/{function}".format({function = function_name}) + (supabase_query.build_query() if supabase_query!=null else "")
var task : DatabaseTask = DatabaseTask.new(
var task : DatabaseTask = DatabaseTask.new()
task._setup(
supabase_query,
-2,
endpoint,
Expand All @@ -57,7 +59,7 @@ func _process_task(task : DatabaseTask) -> void:

# .............. HTTPRequest completed
func _on_task_completed(task : DatabaseTask) -> void:
task._handler.queue_free()
if task._handler != null: task._handler.queue_free()
if task.data!=null and not task.data.empty():
match task._code:
SupabaseQuery.REQUESTS.SELECT: emit_signal("selected", task.data)
Expand Down
1 change: 0 additions & 1 deletion addons/supabase/Database/database_error.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func _init(dictionary : Dictionary = {}) -> void:
if not _error.empty():
code = _error.code if _error.has("code") else "empty"
message = _error.message
# id = _error.error_id
hint = _error.hint if _error.has("hint") and _error.hint != null else "empty"
details = _error.details if _error.has("details") and _error.details != null else "empty"
### always different behavior ???
Expand Down
10 changes: 7 additions & 3 deletions addons/supabase/Database/database_task.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ var error : SupabaseDatabaseError

var _handler : HTTPRequest

func _init(query : SupabaseQuery, code : int, endpoint : String, headers : PoolStringArray, payload : String = ""):
func _init(data = null, error : SupabaseDatabaseError = null) -> void:
self.data = data
self.error = error

func _setup(query : SupabaseQuery, code : int, endpoint : String, headers : PoolStringArray, payload : String = ""):
_query = query
_code = code
_endpoint = endpoint
_headers = headers
_payload = payload
_method = match_code(code)


func match_code(code : int) -> int:
match code:
Expand All @@ -51,5 +55,5 @@ func _on_task_completed(result : int, response_code : int, headers : PoolStringA
func complete(_result, _error : SupabaseDatabaseError = null) -> void:
data = _result
error = _error
_handler.queue_free()
if _handler : _handler.queue_free()
emit_signal("completed", self)
2 changes: 1 addition & 1 deletion addons/supabase/Database/query.gd
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func text_seach(column : String, query : String, type : String = "", config : St
"websearch": filter = Filters.WFTS
_: filter = Filters.FTS
query = query.replacen(" ", "%20")
filter(column, filter, query, {config = config})
filter(column, filter, query, {config = config} if config != "" else {})
return self

func clean() -> void:
Expand Down
130 changes: 130 additions & 0 deletions addons/supabase/Storage/storage.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
class_name SupabaseStorage
extends Node

signal listed_buckets(buckets)
signal got_bucket(details)
signal created_bucket(details)
signal updated_bucket(details)
signal emptied_bucket(details)
signal deleted_bucket(details)
signal error(error)

const _rest_endpoint : String = "/storage/v1/"

var _config : Dictionary
var _header : PoolStringArray = ["Content-type: application/json"]
var _bearer : PoolStringArray = ["Authorization: Bearer %s"]

var _pooled_tasks : Array = []


func _init(config : Dictionary) -> void:
_config = config
name = "Storage"

func list_buckets() -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket"
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.LIST_BUCKETS,
endpoint,
_header + _bearer)
_process_task(task)
return task


func get_bucket(id : String) -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket/" + id
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.GET_BUCKET,
endpoint,
_header + _bearer)
_process_task(task)
return task


func create_bucket(_name : String, id : String, public : bool = false) -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket"
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.CREATE_BUCKET,
endpoint,
_header + _bearer,
to_json({"name" : _name, id = id, public = public}))
_process_task(task)
return task


func update_bucket(id : String, public : bool) -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket/" + id
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.UPDATE_BUCKET,
endpoint,
_header + _bearer,
to_json({public = public}))
_process_task(task)
return task


func empty_bucket(id : String) -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket/" + id + "/empty"
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.EMPTY_BUCKET,
endpoint,
_bearer)
_process_task(task)
return task


func delete_bucket(id : String) -> StorageTask:
_bearer = Supabase.auth._bearer
var endpoint : String = _config.supabaseUrl + _rest_endpoint + "bucket/" + id
var task : StorageTask = StorageTask.new()
task._setup(
task.METHODS.DELETE_BUCKET,
endpoint,
_bearer)
_process_task(task)
return task


func from(id : String) -> StorageBucket:
for bucket in get_children():
if bucket.id == id:
return bucket
var storage_bucket : StorageBucket = StorageBucket.new(id, _config)
add_child(storage_bucket)
return storage_bucket

# ---

func _process_task(task : StorageTask) -> void:
var httprequest : HTTPRequest = HTTPRequest.new()
add_child(httprequest)
task.connect("completed", self, "_on_task_completed")
task.push_request(httprequest)
_pooled_tasks.append(task)

# .............. HTTPRequest completed
func _on_task_completed(task : StorageTask) -> void:
if task._handler : task._handler.queue_free()
if task.data!=null and not task.data.empty():
match task._code:
task.METHODS.LIST_BUCKETS: emit_signal("listed_buckets", task.data)
task.METHODS.GET_BUCKET: emit_signal("got_bucket", task.data)
task.METHODS.CREATE_BUCKET: emit_signal("created_bucket", task.data)
task.METHODS.UPDATE_BUCKET: emit_signal("updated_bucket", task.data)
task.METHODS.EMPTY_BUCKET: emit_signal("emptied_bucket", task.data)
task.METHODS.DELETE_BUCKET: emit_signal("deleted_bucket", task.data)
_:
emit_signal("rpc_completed", task.data)
elif task.error != null:
emit_signal("error", task.error)
Loading

0 comments on commit 1853e9a

Please sign in to comment.