Skip to content

Commit b51c157

Browse files
removed default header from constructor
1 parent 2cdc7af commit b51c157

4 files changed

Lines changed: 39 additions & 43 deletions

File tree

src/brunt/client.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
from datetime import datetime
66
from types import TracebackType
7-
from typing import Any, List, Optional, Type, Union
7+
from typing import Any, Type
88

99
from aiohttp.client import ClientSession
1010
from requests import Session
@@ -32,7 +32,7 @@ def __init__(self, username: str = None, password: str = None):
3232
"""
3333
self._user: str | None = username
3434
self._pass: str | None = password
35-
self._things: List[Thing] | None = None
35+
self._things: list[Thing] | None = None
3636
self._last_login: datetime | None = None
3737
self._last_requested_position: dict[str, int] | None = None
3838

@@ -140,9 +140,9 @@ def __enter__(self) -> BruntClient:
140140

141141
def __exit__(
142142
self,
143-
exc_type: Optional[Type[BaseException]],
144-
exc_value: Optional[BaseException],
145-
traceback: Optional[TracebackType],
143+
exc_type: Type[BaseException] | None,
144+
exc_value: BaseException | None,
145+
traceback: TracebackType | None,
146146
) -> None:
147147
"""Exit the context manager."""
148148
self.close()
@@ -163,7 +163,7 @@ def login(self, username: str = None, password: str = None) -> bool:
163163
self._last_login = datetime.utcnow()
164164
return True
165165

166-
def get_things(self, force: bool = False) -> List[Thing]:
166+
def get_things(self, force: bool = False) -> list[Thing]:
167167
"""Get all the things.
168168
169169
Check if there are things in memory. otherwise first do the getThings call
@@ -175,7 +175,7 @@ def get_things(self, force: bool = False) -> List[Thing]:
175175
return self._get_things()
176176
return self._things
177177

178-
def _get_things(self) -> List[Thing]:
178+
def _get_things(self) -> list[Thing]:
179179
"""Get the things registered in your account.
180180
181181
:return: dict with things registered in the logged in account
@@ -211,7 +211,7 @@ def get_state(self, thing: str = None, thing_uri: str = None) -> Thing:
211211

212212
def change_key(
213213
self, key: str, value: Any, thing: str = None, thing_uri: str = None
214-
) -> Union[dict, list]:
214+
) -> dict | list:
215215
"""Change a variable of the thing. Mostly included for future additions.
216216
217217
:param key: The value you want to change
@@ -238,7 +238,7 @@ def change_key(
238238

239239
def change_request_position(
240240
self, request_position: int, thing: str = None, thing_uri: str = None
241-
) -> Union[dict, list]:
241+
) -> dict | list:
242242
"""Change the position of the thing.
243243
244244
:param request_position: The new position for the slide (0-100)
@@ -288,9 +288,9 @@ async def __aenter__(self) -> BruntClientAsync:
288288

289289
async def __aexit__(
290290
self,
291-
exc_type: Optional[Type[BaseException]],
292-
exc_value: Optional[BaseException],
293-
traceback: Optional[TracebackType],
291+
exc_type: Type[BaseException] | None,
292+
exc_value: BaseException | None,
293+
traceback: TracebackType | None,
294294
) -> None:
295295
"""Exit the context manager."""
296296
await self.async_close()
@@ -313,7 +313,7 @@ async def async_login(self, username: str = None, password: str = None) -> bool:
313313
self._last_login = datetime.utcnow()
314314
return True
315315

316-
async def async_get_things(self, force: bool = False) -> List[Thing]:
316+
async def async_get_things(self, force: bool = False) -> list[Thing]:
317317
"""Get the things registered in your account.
318318
319319
:param force: force a refresh from the server, otherwise get from variable.
@@ -323,7 +323,7 @@ async def async_get_things(self, force: bool = False) -> List[Thing]:
323323
return await self._async_get_things()
324324
return self._things
325325

326-
async def _async_get_things(self) -> List[Thing]:
326+
async def _async_get_things(self) -> list[Thing]:
327327
"""Get the things.
328328
329329
Check if there are things in memory, otherwise first do the getThings call and
@@ -359,7 +359,7 @@ async def async_get_state(self, thing: str = None, thing_uri: str = None) -> Thi
359359

360360
async def async_change_key(
361361
self, key: str, value: Any, thing: str = None, thing_uri: str = None
362-
) -> Union[dict, list]:
362+
) -> dict | list:
363363
"""Change a variable of the thing. Mostly included for future additions.
364364
365365
:param key: The value you want to change
@@ -386,7 +386,7 @@ async def async_change_key(
386386

387387
async def async_change_request_position(
388388
self, request_position: int, thing: str = None, thing_uri: str = None
389-
) -> Union[dict, list]:
389+
) -> dict | list:
390390
"""Change the position of the thing.
391391
392392
:param request_position: The new position for the slide (0-100)

src/brunt/const.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
"""Constants for Brunt."""
2+
13
MAIN_HOST = "https://sky.brunt.co"
24
MAIN_THINGS_PATH = {"path": "/thing", "host": MAIN_HOST}
35
THINGS_HOST = "https://thing.brunt.co:8080"
46
REQUEST_POSITION_KEY = "requestPosition"
57
DT_FORMAT_STRING = r"%a, %d-%b-%Y %H:%M:%S %Z"
6-
COOKIE_DOMAIN = "brunt.co"
8+
COOKIE_DOMAIN = "brunt.co"

src/brunt/http.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
"""Main code for brunt http."""
2+
from __future__ import annotations
3+
24
import json
35
import logging
46
from abc import abstractmethod, abstractproperty
57
from datetime import datetime
6-
from typing import Union
8+
from typing import Final
79

810
import requests
911
from aiohttp import ClientSession
10-
from multidict import CIMultiDict
11-
from requests.utils import CaseInsensitiveDict
1212

1313
from .const import COOKIE_DOMAIN, DT_FORMAT_STRING
1414
from .utils import RequestTypes
1515

1616
_LOGGER = logging.getLogger(__name__)
1717

18-
DEFAULT_HEADER = CaseInsensitiveDict(
19-
{
20-
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
21-
"Origin": "https://sky.brunt.co",
22-
"Accept-Language": "en-gb",
23-
"Accept": "application/vnd.brunt.v1+json",
24-
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) \
18+
DEFAULT_HEADER: Final = {
19+
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
20+
"Origin": "https://sky.brunt.co",
21+
"Accept-Language": "en-gb",
22+
"Accept": "application/vnd.brunt.v1+json",
23+
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) \
2524
AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E216",
26-
}
27-
)
25+
}
2826

2927

3028
class BaseBruntHTTP:
@@ -34,21 +32,21 @@ class BaseBruntHTTP:
3432
def _prepare_request(data: dict) -> dict:
3533
"""Prepare the payload and add the length to the header, payload might be empty."""
3634
payload = ""
37-
headers = {}
35+
headers = DEFAULT_HEADER.copy()
3836
if "data" in data:
3937
payload = json.dumps(data["data"])
4038
headers = {"Content-Length": str(len(payload))}
4139

4240
return {"url": data["host"] + data["path"], "data": payload, "headers": headers}
4341

4442
@abstractmethod
45-
def request(self, data: dict, request_type: RequestTypes) -> Union[dict, list]:
43+
def request(self, data: dict, request_type: RequestTypes) -> dict | list:
4644
"""Return the request response - abstract."""
4745

4846
@abstractmethod
4947
async def async_request(
5048
self, data: dict, request_type: RequestTypes
51-
) -> Union[dict, list]:
49+
) -> dict | list:
5250
"""Return the request response - abstract."""
5351

5452
@abstractproperty
@@ -62,7 +60,6 @@ class BruntHttp(BaseBruntHTTP):
6260
def __init__(self, session: requests.Session = None):
6361
"""Initialize the BruntHTTP object."""
6462
self.session = session if session else requests.Session()
65-
self.session.headers = DEFAULT_HEADER
6663

6764
@property
6865
def is_logged_in(self) -> bool:
@@ -81,11 +78,11 @@ def is_logged_in(self) -> bool:
8178

8279
async def async_request(
8380
self, data: dict, request_type: RequestTypes
84-
) -> Union[dict, list]:
81+
) -> dict | list:
8582
"""Raise error for using this call with sync."""
8683
raise NotImplementedError("You are using the sync version, please use request.")
8784

88-
def request(self, data: dict, request_type: RequestTypes) -> Union[dict, list]:
85+
def request(self, data: dict, request_type: RequestTypes) -> dict | list:
8986
"""Request the data.
9087
9188
:param session: session object from the Requests package
@@ -96,7 +93,8 @@ def request(self, data: dict, request_type: RequestTypes) -> Union[dict, list]:
9693
:raises: raises errors from Requests through the raise_for_status function
9794
"""
9895
resp = self.session.request(
99-
request_type.value, **BaseBruntHTTP._prepare_request(data)
96+
request_type.value,
97+
**BaseBruntHTTP._prepare_request(data),
10098
)
10199
# raise an error if it occured in the Request.
102100
resp.raise_for_status()
@@ -112,7 +110,6 @@ class BruntHttpAsync(BaseBruntHTTP):
112110
def __init__(self, session: ClientSession = None):
113111
"""Initialize the BruntHTTP object."""
114112
self.session = session if session else ClientSession()
115-
self.session._default_headers = CIMultiDict(DEFAULT_HEADER)
116113

117114
@property
118115
def is_logged_in(self) -> bool:
@@ -128,15 +125,15 @@ def is_logged_in(self) -> bool:
128125
)
129126
return False
130127

131-
def request(self, data: dict, request_type: RequestTypes) -> Union[dict, list]:
128+
def request(self, data: dict, request_type: RequestTypes) -> dict | list:
132129
"""Raise error for using this call with async."""
133130
raise NotImplementedError(
134131
"You are using the Async version, please use async_request."
135132
)
136133

137134
async def async_request(
138135
self, data: dict, request_type: RequestTypes
139-
) -> Union[dict, list]:
136+
) -> dict | list:
140137
"""Request the data.
141138
142139
:param session: session object from the Requests package
@@ -149,7 +146,7 @@ async def async_request(
149146
async with self.session.request(
150147
request_type.value,
151148
**BaseBruntHTTP._prepare_request(data),
152-
raise_for_status=True
149+
raise_for_status=True,
153150
) as resp:
154151
try:
155152
return await resp.json(content_type=None)

src/brunt/thing.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
from datetime import datetime
77
from typing import Any
88

9-
# state: {'TIMESTAMP': '1619179952875', 'NAME': 'Blind', 'SERIAL': '00140d6f1950f166', 'MODEL': 'BEAKR1601', 'requestPosition': '100', 'currentPosition': '100', 'moveState': '0', 'setLoad': '2418', 'currentLoad': '5', 'FW_VERSION': '1.361', 'overStatus': '0', 'Duration': '1000', 'ICON': 'b-icon-curtain_01', 'delay': 3810}
10-
# things: [{'TIMESTAMP': '1619179952875', 'NAME': 'Blind', 'SERIAL': '00140d6f1950f166', 'MODEL': 'BEAKR1601', 'requestPosition': '100', 'currentPosition': '100', 'moveState': '0', 'setLoad': '2418', 'currentLoad': '5', 'FW_VERSION': '1.361', 'overStatus': '0', 'Duration': '1000', 'ICON': 'b-icon-curtain_01', 'thingUri': '/hub/00140d6f1950f166', 'PERMISSION_TYPE': '"ownership"', 'delay': 2594}]
11-
129
_LOGGER = logging.getLogger(__name__)
1310

1411
MAPPING = {

0 commit comments

Comments
 (0)