1- """The API wrapper for sending data to the CubeServer server
2- See https://github.com/snorklerjoe/CubeServer-api-circuitpython for more info!
3- """
1+ """CircuitPython implementation of the CubeServer API Wrapper Library"""
2+
3+ from . common import *
44
55import ssl
66import wifi
77import socketpool
88from gc import collect
9- from collections import namedtuple
10- from binascii import b2a_base64
119from errno import EAGAIN
12- from json import loads , dumps
13-
14- try :
15- import client_config
16- except ImportError :
17- pass
18-
10+ from binascii import b2a_base64
11+ from json import loads
1912
2013# Helpers:
21- DEGREE_SIGN = u"\xb0 "
22-
2314def enum (** enums ):
2415 """Fake enum-maker"""
2516 return type ('Enum' , (), enums )
@@ -28,25 +19,6 @@ def basic_auth_str(user: str, pwd: str) -> str:
2819 """Encodes the username and password as per RFC7617 on Basic Auth"""
2920 return b2a_base64 (f"{ user } :{ pwd } " .encode ()).strip ().decode ("utf-8" )
3021
31- def urlencode (stuff : str ) -> str :
32- """URL-encodes a string"""
33- output = ""
34- for char in stuff :
35- if char .isalpha () or char .isdigit ():
36- output += char
37- else :
38- hex_repr = hex (ord (char )).lstrip ('0x' ).upper ()
39- if ord (char ) <= 0xFF :
40- output += '%' + hex_repr
41- elif 0xFF < ord (char ) <= 0xFFFF :
42- if len (hex_repr ) == 3 :
43- hex_repr = '0' + hex_repr
44- output += '%' + hex_repr [:2 ] + '%' + hex_repr [2 :]
45- else :
46- raise ValueError ("Unencodable character" )
47-
48- return output
49-
5022
5123DataClass = enum (
5224 TEMPERATURE = "temperature" ,
@@ -118,46 +90,33 @@ class ConnectionError(Exception):
11890class AuthorizationError (ConnectionError ):
11991 """Indicates an issue with the team credentials"""
12092
121- class ConnectionConfig :
122- """The configuration of the connection to the server"""
123- TIMEOUT : int = 60
124- if 'client_config' in globals ():
125- AP_SSID : str = client_config .CONF_AP_SSID
126- API_CN : str = client_config .CONF_API_CN
127- API_HOST : str = client_config .CONF_API_HOST
128- API_PORT : int = client_config .API_PORT
129- else :
130- AP_SSID : str = "CubeServer-API"
131- API_CN : str = "api.local"
132- API_HOST : str = "https://api.local"
133- API_PORT : int = 8081
134-
135- CUBESERVER_DEFAULT_CONFIG = ConnectionConfig ()
136-
137- GameStatus = namedtuple ("GameStatus" ,
138- ['unix_time' ,
139- 'score' ,
140- 'strikes' ]
141- )
142-
143- HTTPResponse = namedtuple ("HTTPResponse" ,
144- ['code' , 'body' ]
145- )
93+ def urlencode (stuff : str ) -> str :
94+ """URL-encodes a string"""
95+ output = ""
96+ for char in stuff :
97+ if char .isalpha () or char .isdigit ():
98+ output += char
99+ else :
100+ hex_repr = hex (ord (char )).lstrip ('0x' ).upper ()
101+ if ord (char ) <= 0xFF :
102+ output += '%' + hex_repr
103+ elif 0xFF < ord (char ) <= 0xFFFF :
104+ if len (hex_repr ) == 3 :
105+ hex_repr = '0' + hex_repr
106+ output += '%' + hex_repr [:2 ] + '%' + hex_repr [2 :]
107+ else :
108+ raise ValueError ("Unencodable character" )
146109
147- def _if_conf_exists (key : str ):
148- """Returns the config value if client_config is imported, else None"""
149- if 'client_config' not in globals ():
150- return None
151- return getattr (client_config , key )
110+ return output
152111
153112class Connection :
154113 """A class for connecting to the server"""
155114
156115 def __init__ (
157116 self ,
158- team_name : str = _if_conf_exists ('TEAM_NAME' ),
159- team_secret : str = _if_conf_exists ('TEAM_SECRET' ),
160- server_cert : str = _if_conf_exists ('SERVER_CERT' ),
117+ team_name : str = conf_if_exists ('TEAM_NAME' ),
118+ team_secret : str = conf_if_exists ('TEAM_SECRET' ),
119+ server_cert : str = conf_if_exists ('SERVER_CERT' ),
161120 conf = CUBESERVER_DEFAULT_CONFIG ,
162121 verbose : bool = False ,
163122 _force : bool = False ,
@@ -172,6 +131,7 @@ def __init__(
172131 ) or \
173132 not isinstance (conf , ConnectionConfig ):
174133 raise TypeError ("Bad parameters or client config" )
134+
175135 self .team_name = team_name
176136 self .team_secret = team_secret
177137 self .server_cert = server_cert
@@ -401,3 +361,9 @@ def post(self, point: DataPoint) -> bool:
401361 content_type = 'application/json' ,
402362 headers = ['User-Agent: Dude' ]
403363 ).code == 201
364+
365+ def __exit__ (self ):
366+ if self .v :
367+ print ("Closing the server connection-" )
368+ self .close_socket ()
369+ self .close_wifi ()
0 commit comments