this repo has no description

Fix jwt error + Better http request handling

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