-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
131 lines (116 loc) · 5 KB
/
client.py
File metadata and controls
131 lines (116 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if you have not received it, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import json
import socket
import threading
import http.server
import socketserver
from urllib.parse import urlparse, parse_qs
import logging
import bpy
bk_logger = logging.getLogger(__name__)
def handle_login_response(api_key: str, error: str = None):
"""Handles the API key received from callback. Stores it if successful, logs out on error."""
preferences = bpy.context.preferences.addons[__package__].preferences
preferences.login_attempt = False
if api_key:
preferences.api_key = api_key
bk_logger.info("API key stored successfully.")
else:
preferences.api_key = ""
bk_logger.error(f"Login failed: {error}")
ALLOWED_ORIGIN = "http://localhost:3005"
class CallbackHandler(http.server.BaseHTTPRequestHandler):
def _set_cors_headers(self):
self.send_header("Access-Control-Allow-Origin", ALLOWED_ORIGIN)
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
requested_headers = self.headers.get("Access-Control-Request-Headers")
if requested_headers:
self.send_header("Access-Control-Allow-Headers", requested_headers)
else:
self.send_header("Access-Control-Allow-Headers", "*")
def do_OPTIONS(self):
"""Handle preflight OPTIONS requests for CORS."""
self.send_response(200)
self._set_cors_headers()
self.end_headers()
bk_logger.debug("Sent OPTIONS response")
def do_GET(self):
parsed_path = urlparse(self.path)
if parsed_path.path == "/callback":
query_params = parse_qs(parsed_path.query)
api_key = query_params.get("api_key")
if api_key:
response_body = {"message": "Login successful", "success": True}
response_body_bytes = json.dumps(response_body).encode('utf-8')
self.send_response(200)
self._set_cors_headers()
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(response_body_bytes)))
self.end_headers()
self.wfile.write(response_body_bytes)
handle_login_response(api_key[0])
bk_logger.debug("Sent 200 response with API key")
else:
response_body = {"message": "No API key provided", "success": False}
response_body_bytes = json.dumps(response_body).encode('utf-8')
self.send_response(400)
self._set_cors_headers()
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(response_body_bytes)))
self.end_headers()
self.wfile.write(response_body_bytes)
handle_login_response(None, "No API key provided")
bk_logger.debug("Sent 400 response: No API key")
return
response_body = {"message": "Not Found", "success": False}
response_body_bytes = json.dumps(response_body).encode('utf-8')
self.send_response(404)
self._set_cors_headers()
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(response_body_bytes)))
self.end_headers()
self.wfile.write(response_body_bytes)
bk_logger.debug("Sent 404 response")
_server = None
_server_thread = None
_stop_event = threading.Event()
def get_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
s.listen(1)
return s.getsockname()[1]
def start_server(callback_url):
global _server, _server_thread
port = urlparse(callback_url).port
bk_logger.debug(
f"Starting server with callback_url: {callback_url}, type: {type(callback_url)}"
)
_server = socketserver.TCPServer(("", port), CallbackHandler)
_stop_event.clear()
_server_thread = threading.Thread(target=_server.serve_forever, daemon=True)
_server_thread.start()
bk_logger.info(f"Started server on port {port}")
def stop_server():
global _server
if _server:
_stop_event.set()
_server.shutdown()
_server.server_close()
_server = None
bk_logger.info("Server stopped")