11"""Main BalatroBot client for communicating with the game."""
22
33import json
4+ import logging
45import socket
56from typing import Any , Literal , Self
67
1819 StartRunRequest ,
1920)
2021
22+ logger = logging .getLogger (__name__ )
23+
2124
2225class BalatroClient :
2326 """Client for communicating with the BalatroBot game API."""
@@ -58,6 +61,7 @@ def connect(self) -> None:
5861 if self ._connected :
5962 return
6063
64+ logger .info (f"Connecting to BalatroBot API at { self .host } :{ self .port } " )
6165 try :
6266 self ._socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
6367 self ._socket .settimeout (self .timeout )
@@ -66,7 +70,11 @@ def connect(self) -> None:
6670 )
6771 self ._socket .connect ((self .host , self .port ))
6872 self ._connected = True
73+ logger .info (
74+ f"Successfully connected to BalatroBot API at { self .host } :{ self .port } "
75+ )
6976 except (socket .error , OSError ) as e :
77+ logger .error (f"Failed to connect to { self .host } :{ self .port } : { e } " )
7078 raise ConnectionFailedError (
7179 f"Failed to connect to { self .host } :{ self .port } " ,
7280 error_code = "E008" ,
@@ -76,6 +84,7 @@ def connect(self) -> None:
7684 def disconnect (self ) -> None :
7785 """Disconnect from the BalatroBot game API."""
7886 if self ._socket :
87+ logger .info (f"Disconnecting from BalatroBot API at { self .host } :{ self .port } " )
7988 self ._socket .close ()
8089 self ._socket = None
8190 self ._connected = False
@@ -106,6 +115,7 @@ def _send_request(self, name: str, arguments: dict[str, Any]) -> dict[str, Any]:
106115
107116 # Create and validate request
108117 request = APIRequest (name = name , arguments = arguments )
118+ logger .info (f"Sending API request: { name } " )
109119
110120 try :
111121 # Send request
@@ -118,17 +128,21 @@ def _send_request(self, name: str, arguments: dict[str, Any]) -> dict[str, Any]:
118128
119129 # Check for error response
120130 if "error" in response_data :
131+ logger .error (f"API request { name } failed: { response_data .get ('error' )} " )
121132 raise create_exception_from_error_response (response_data )
122133
134+ logger .info (f"API request { name } completed successfully" )
123135 return response_data
124136
125137 except socket .error as e :
138+ logger .error (f"Socket error during API request { name } : { e } " )
126139 raise ConnectionFailedError (
127140 f"Socket error during communication: { e } " ,
128141 error_code = "E008" ,
129142 context = {"error" : str (e )},
130143 ) from e
131144 except json .JSONDecodeError as e :
145+ logger .error (f"Invalid JSON response from API request { name } : { e } " )
132146 raise BalatroError (
133147 f"Invalid JSON response from game: { e } " ,
134148 error_code = "E001" ,
0 commit comments