Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery Starbot ⭐ refactored IceBotYT/lacrosse_view #1

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 24 additions & 40 deletions src/lacrosse_view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,31 @@ async def get_locations(self) -> list[Location]:
if self.token == "":
raise LoginError("Login first.")

headers = {"Authorization": f"Bearer {self.token}"}

data: dict[str, Any]

locations_url = (
"https://lax-gateway.appspot.com/"
"_ah/api/lacrosseClient/v1.1/active-user/locations"
)
headers = {"Authorization": "Bearer " + self.token}

data: dict[str, Any]

if not self.websession:
async with aiohttp.ClientSession() as session:
async with session.get(locations_url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get locations, status code: "
+ str(response.status)
f"Failed to get locations, status code: {str(response.status)}"
)

data = await response.json()
else:
async with self.websession.get(locations_url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get locations, status code: " + str(response.status)
f"Failed to get locations, status code: {str(response.status)}"
)

Comment on lines +104 to +128
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LaCrosse.get_locations refactored with the following changes:

data = await response.json()

return [
Expand Down Expand Up @@ -158,32 +160,25 @@ async def get_sensors(
if start > end:
raise ValueError("Start time cannot be after end time.")

devices = list()
headers = {"Authorization": "Bearer " + self.token}
headers = {"Authorization": f"Bearer {self.token}"}

sensors_url = f"https://lax-gateway.appspot.com/_ah/api/lacrosseClient/v1.1/active-user/location/{str(location.id)}/sensorAssociations?prettyPrint=false"

sensors_url = (
"https://lax-gateway.appspot.com/"
"_ah/api/lacrosseClient/v1.1/active-user/location/"
+ str(location.id)
+ "/sensorAssociations?prettyPrint=false"
)
if not self.websession:
async with aiohttp.ClientSession() as session:
async with session.get(sensors_url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get location, status code: "
+ str(response.status)
)
raise HTTPError(f"Failed to get location, status code: {str(response.status)}")
data = await response.json()
else:
async with self.websession.get(sensors_url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get location, status code: " + str(response.status)
)
raise HTTPError(f"Failed to get location, status code: {str(response.status)}")
data = await response.json()

aggregates = "ai.ticks.1"

devices = []
Comment on lines -161 to +181
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LaCrosse.get_sensors refactored with the following changes:

for device in data.get("items"):
sensor = device.get("sensor")
device = {
Expand All @@ -205,8 +200,6 @@ async def get_sensors(
else None
)

aggregates = "ai.ticks.1"

url = (
"https://ingv2.lacrossetechnology.com/"
"api/v1.1/active-user/device-association/ref.user-device.{id}/"
Expand All @@ -225,23 +218,18 @@ async def get_sensors(
)
)

headers = {"Authorization": "Bearer " + self.token}
headers = {"Authorization": f"Bearer {self.token}"}

if not self.websession:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get sensor, status code: "
+ str(response.status)
)
raise HTTPError(f"Failed to get sensor, status code: {str(response.status)}")
data = await response.json()
else:
async with self.websession.get(url, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to get sensor, status code: " + str(response.status)
)
raise HTTPError(f"Failed to get sensor, status code: {str(response.status)}")
data = await response.json()

device["data"] = data.get("ref.user-device." + device["device_id"])[
Expand All @@ -262,30 +250,26 @@ async def logout(self) -> bool:
"_ah/api/lacrosseClient/v1.1/user/devices"
"?prettyPrint=false"
)
headers = {"Authorization": "Bearer " + self.token}
headers = {"Authorization": f"Bearer {self.token}"}
Comment on lines -265 to +253
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LaCrosse.logout refactored with the following changes:

body = {
"firebaseRegistrationToken": "fpxASxqXfE_rvyNdMGe2Bd:APA91bH53k_fq0pWNpIwTla9CiOQgx8G1PLrKpp74AfdTHPgwh3g0RZNopQQ-POqmNVyaW_2vT9I7nYz0RdWqY1DU4uNIx4vOzZPQwn7mHD6uvtYH8qxwedB3cLOBmSpOdAOkH2jTN4c"
}
if not self.websession:
async with aiohttp.ClientSession() as session:
async with session.delete(url, json=body, headers=headers) as response:
if response.status != 200:
raise HTTPError(
"Failed to logout, status code: " + str(response.status)
)
raise HTTPError(f"Failed to logout, status code: {str(response.status)}")
data = await response.json()
if data["message"] != "Operation Successful":
raise HTTPError("Failed to logout, message: " + data["message"])
self.token = ""
return True
else:
async with self.websession.delete(
url, json=body, headers=headers
) as response:
url, json=body, headers=headers
) as response:
if response.status != 200:
raise HTTPError(
"Failed to logout, status code: " + str(response.status)
)
raise HTTPError(f"Failed to logout, status code: {str(response.status)}")
data = await response.json()
if data["message"] != "Operation Successful":
raise HTTPError("Failed to logout, message: " + data["message"])
Expand Down