43 lines
1.0 KiB
GDScript
43 lines
1.0 KiB
GDScript
class_name PetApi
|
|
extends RefCounted
|
|
|
|
var _api_client: ApiClient
|
|
|
|
|
|
func _init(api_client: ApiClient = null) -> void:
|
|
_api_client = api_client
|
|
|
|
|
|
func set_api_client(api_client: ApiClient) -> void:
|
|
_api_client = api_client
|
|
|
|
|
|
func fetch_list(token: String) -> Dictionary:
|
|
return await _post("pet/get-list", _with_token(token))
|
|
|
|
|
|
func set_enter_war(token: String, pet_id: int) -> Dictionary:
|
|
return await _post("pet/set-enter-war", _with_token(token, {"pet_id": pet_id}))
|
|
|
|
|
|
func feed(token: String, pet_id: int, item_id: int) -> Dictionary:
|
|
return await _post("pet/feed", _with_token(token, {"pet_id": pet_id, "item_id": item_id}))
|
|
|
|
|
|
func _with_token(token: String, payload: Dictionary = {}) -> Dictionary:
|
|
var body := payload.duplicate(true)
|
|
body["token"] = token
|
|
return body
|
|
|
|
|
|
func _post(path: String, payload: Dictionary) -> Dictionary:
|
|
if _api_client == null:
|
|
return {
|
|
"ok": false,
|
|
"offline": true,
|
|
"status": 0,
|
|
"body": {},
|
|
"text": "PetApi missing ApiClient."
|
|
}
|
|
return await _api_client.post_json(path, payload)
|