+1
-1
Atproto/manifest.json
+1
-1
Atproto/manifest.json
+59
-40
gdscript/mods/Atproto/atproto_client.gd
+59
-40
gdscript/mods/Atproto/atproto_client.gd
···
14
var refreshJwt
15
var Atproto
16
17
-
# HTTP
18
-
var requester: HTTPRequest
19
-
20
func _enter_tree():
21
-
self.requester = HTTPRequest.new()
22
Atproto = self.get_parent()
23
-
self.add_child(self.requester, true)
24
25
func connected() -> bool:
26
return accessJwt != null
27
28
func is_token_expired() -> bool:
29
-
var json = Marshalls.base64_to_utf8(self.accessJwt.split(".")[1])
30
-
var data = parse_json(json)
31
var expires = data.exp
32
var unix = floor(Time.get_unix_time_from_system())
33
return expires < unix
···
53
var json_payload = JSON.print(payload)
54
json_payload = json_payload.replace("at_type", "$type")
55
56
-
var req = self.requester
57
req.request(pds + "/xrpc/com.atproto.repo.createRecord", get_header(), true, HTTPClient.METHOD_POST, json_payload)
58
-
req.connect("request_completed", self, "_create_record_handler", [callback])
59
60
-
func _create_record_handler(_result, code, _headers, body: PoolByteArray, callback: FuncRef):
61
-
var req = self.requester
62
-
req.disconnect("request_completed", self, "_create_record_handler")
63
if callback == null:
64
return
65
66
var res = parse_json(body.get_string_from_utf8())
67
callback.call_func(res)
68
69
func list_records(callback: FuncRef, collection: String, limit: int = 50, cursor = ""):
70
-
var req = self.requester
71
-
var query_string = "repo=" + did.http_escape()
72
query_string += "&collection=" + collection.http_escape()
73
query_string += "&limit=" + str(limit).http_escape()
74
query_string += "&cursor=" + str(limit).http_escape()
75
-
req.request(pds + "/xrpc/com.atproto.repo.listRecords?" + query_string, get_header(), true, HTTPClient.METHOD_GET)
76
-
req.connect("request_completed", self, "_list_record_handler", [callback])
77
78
79
-
func _list_record_handler(_result, code, _headers, body: PoolByteArray, callback: FuncRef):
80
-
var req = self.requester
81
-
req.disconnect("request_completed", self, "_list_record_handler")
82
-
var res = parse_json(body.get_string_from_utf8())
83
callback.call_func(res.records)
84
-
85
func get_record(callback: FuncRef, did: String, collection: String, rkey: String):
86
-
var req = self.requester
87
var query_string = "repo=" + did.http_escape()
88
query_string += "&collection=" + collection.http_escape()
89
query_string += "&rkey=" + rkey.http_escape()
90
req.request(pds + "/xrpc/com.atproto.repo.getRecord?" + query_string, get_header(), true, HTTPClient.METHOD_GET)
91
-
req.connect("request_completed", self, "_get_record_handle", [callback])
92
93
-
func _get_record_handle(_result, code, _headers, body: PoolByteArray, callback: FuncRef):
94
-
var req = self.requester
95
-
req.disconnect("request_completed", self, "_get_record_handle")
96
var res = parse_json(body.get_string_from_utf8())
97
callback.call_func(res)
98
-
99
-
func put_record(uri, record):
100
if is_token_expired():
101
refresh_token("put_record", [uri, record])
102
return
···
114
var json_payload = JSON.print(payload)
115
json_payload = json_payload.replace("at_type", "$type")
116
117
-
var req = self.requester
118
req.request(pds + "/xrpc/com.atproto.repo.putRecord", get_header(), true, HTTPClient.METHOD_POST, json_payload)
119
120
################
121
# LOGIN #
122
################
123
124
func login(handle, password):
125
-
var req = self.requester
126
127
-
req.connect("request_completed", self, "after_handle_resolver", [password])
128
req.request("https://bsky.social/xrpc/com.atproto.identity.resolveHandle?handle=" + handle)
129
130
131
-
func after_handle_resolver(_result, code, _headers, body: PoolByteArray, password):
132
-
var req = self.requester
133
req.disconnect("request_completed", self, "after_handle_resolver")
134
135
if code != 200:
···
139
var res = parse_json(body.get_string_from_utf8())
140
self.did = res.did
141
142
-
req.connect("request_completed", self, "after_get_pds", [password])
143
req.request("https://plc.directory/" + self.did)
144
145
146
-
func after_get_pds(_result, code, _headers, body: PoolByteArray, password):
147
-
var req = self.requester
148
req.disconnect("request_completed", self, "after_get_pds")
149
150
if code != 200:
···
161
password = password
162
}
163
164
-
req.connect("request_completed", self, "after_create_session")
165
req.request(pds + "/xrpc/com.atproto.server.createSession", ["Content-Type: application/json"], true, HTTPClient.METHOD_POST, JSON.print(payload))
166
167
-
func after_create_session(_result, code, _headers, body: PoolByteArray):
168
-
var req = self.requester
169
-
req.disconnect("request_completed", self, "after_create_session")
170
171
if code != 200:
172
emit_signal("connection", false)
···
14
var refreshJwt
15
var Atproto
16
17
func _enter_tree():
18
Atproto = self.get_parent()
19
20
func connected() -> bool:
21
return accessJwt != null
22
23
func is_token_expired() -> bool:
24
+
var token_data = self.accessJwt.split(".")[1]
25
+
var data = null
26
+
for x in ["", "=", "=="] :
27
+
var json = Marshalls.base64_to_utf8(token_data + x)
28
+
data = parse_json(json)
29
+
if data != null: break
30
var expires = data.exp
31
var unix = floor(Time.get_unix_time_from_system())
32
return expires < unix
···
52
var json_payload = JSON.print(payload)
53
json_payload = json_payload.replace("at_type", "$type")
54
55
+
var req = HTTPRequest.new()
56
+
self.add_child(req)
57
+
req.connect("request_completed", self, "_create_record_handler", [req, callback])
58
req.request(pds + "/xrpc/com.atproto.repo.createRecord", get_header(), true, HTTPClient.METHOD_POST, json_payload)
59
+
60
61
+
func _create_record_handler(_result, code, _headers, body: PoolByteArray, req: HTTPRequest, callback: FuncRef):
62
+
req.queue_free()
63
if callback == null:
64
return
65
66
var res = parse_json(body.get_string_from_utf8())
67
callback.call_func(res)
68
69
+
## LIST RECORDS
70
+
71
func list_records(callback: FuncRef, collection: String, limit: int = 50, cursor = ""):
72
+
var query_string = "repo=" + did
73
query_string += "&collection=" + collection.http_escape()
74
query_string += "&limit=" + str(limit).http_escape()
75
query_string += "&cursor=" + str(limit).http_escape()
76
+
var req_str = pds + "/xrpc/com.atproto.repo.listRecords?" + query_string
77
78
+
var req = HTTPRequest.new()
79
+
self.add_child(req)
80
+
req.connect("request_completed", self, "_list_record_handler", [req, callback])
81
+
req.request(req_str, get_header(), true, HTTPClient.METHOD_GET)
82
83
+
func _list_record_handler(_result, code, _headers, body: PoolByteArray, req: HTTPRequest, callback: FuncRef):
84
+
req.queue_free()
85
+
var b = body.get_string_from_utf8()
86
+
var res = parse_json(b)
87
callback.call_func(res.records)
88
+
89
+
90
+
## GET RECORD
91
+
92
func get_record(callback: FuncRef, did: String, collection: String, rkey: String):
93
var query_string = "repo=" + did.http_escape()
94
query_string += "&collection=" + collection.http_escape()
95
query_string += "&rkey=" + rkey.http_escape()
96
+
97
+
var req = HTTPRequest.new()
98
+
self.add_child(req)
99
+
req.connect("request_completed", self, "_get_record_handler", [req, callback])
100
req.request(pds + "/xrpc/com.atproto.repo.getRecord?" + query_string, get_header(), true, HTTPClient.METHOD_GET)
101
102
+
func _get_record_handler(_result, code, _headers, body: PoolByteArray, req: HTTPRequest, callback: FuncRef):
103
+
req.queue_free()
104
var res = parse_json(body.get_string_from_utf8())
105
callback.call_func(res)
106
+
107
+
108
+
## PUT RECORD
109
+
110
+
func put_record(uri, record, callback: FuncRef = null):
111
if is_token_expired():
112
refresh_token("put_record", [uri, record])
113
return
···
125
var json_payload = JSON.print(payload)
126
json_payload = json_payload.replace("at_type", "$type")
127
128
+
var req = HTTPRequest.new()
129
+
self.add_child(req)
130
+
req.connect("request_completed", self, "_put_record_handler", [req, callback])
131
req.request(pds + "/xrpc/com.atproto.repo.putRecord", get_header(), true, HTTPClient.METHOD_POST, json_payload)
132
+
133
+
func _put_record_handler(_result, code, _headers, body: PoolByteArray, req: HTTPRequest, callback: FuncRef):
134
+
req.queue_free()
135
+
if callback == null:
136
+
return
137
+
var res = parse_json(body.get_string_from_utf8())
138
+
callback.call_func(res)
139
140
################
141
# LOGIN #
142
################
143
144
func login(handle, password):
145
+
var req = HTTPRequest.new()
146
+
self.add_child(req)
147
148
+
req.connect("request_completed", self, "after_handle_resolver", [req, password])
149
req.request("https://bsky.social/xrpc/com.atproto.identity.resolveHandle?handle=" + handle)
150
151
152
+
func after_handle_resolver(_result, code, _headers, body: PoolByteArray, req: HTTPRequest, password):
153
req.disconnect("request_completed", self, "after_handle_resolver")
154
155
if code != 200:
···
159
var res = parse_json(body.get_string_from_utf8())
160
self.did = res.did
161
162
+
req.connect("request_completed", self, "after_get_pds", [req, password])
163
req.request("https://plc.directory/" + self.did)
164
165
166
+
func after_get_pds(_result, code, _headers, body: PoolByteArray,req: HTTPRequest, password):
167
req.disconnect("request_completed", self, "after_get_pds")
168
169
if code != 200:
···
180
password = password
181
}
182
183
+
req.connect("request_completed", self, "after_create_session", [req])
184
req.request(pds + "/xrpc/com.atproto.server.createSession", ["Content-Type: application/json"], true, HTTPClient.METHOD_POST, JSON.print(payload))
185
+
186
187
+
func after_create_session(_result, code, _headers, body: PoolByteArray, req: HTTPRequest):
188
+
req.queue_free()
189
190
if code != 200:
191
emit_signal("connection", false)