Skip to content

Commit 0ce1cde

Browse files
Fix mypy errors in dining.py (#200)
* Fix mypy errors in dining.py * Add from __future__ import annotations for Python 3.9 support
1 parent 40c9a14 commit 0ce1cde

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

pittapi/dining.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
"""
1919

20+
from __future__ import annotations
21+
2022
import requests
21-
import json
2223
from datetime import datetime
2324
from typing import Any
2425

26+
JSON = dict[str, Any]
27+
2528
REQUEST_HEADERS = {"User-Agent": "Chrome/103.0.5026.0"}
2629

2730
LOCATIONS = {
@@ -68,24 +71,26 @@
6871
MENU_URL = "https://api.dineoncampus.com/v1/location/{location_id}/periods/{period_id}?platform=0&date={date_str}"
6972

7073

71-
def get_locations() -> dict[str, Any]:
74+
def get_locations() -> dict[str, JSON]:
7275
"""Gets data about all dining locations"""
7376
resp = requests.get(LOCATIONS_URL, headers=REQUEST_HEADERS)
74-
locations = json.loads(resp.content)["locations"]
77+
locations = resp.json()["locations"]
7578
dining_locations = {location["name"].upper(): location for location in locations}
7679

7780
return dining_locations
7881

7982

80-
def get_location_hours(location_name: str, date: datetime) -> dict[str, Any]:
83+
def get_location_hours(location_name: str | None = None, date: datetime | None = None) -> dict[str, list[dict[str, int]]]:
8184
"""Returns dictionary containing Opening and Closing times of locations open on date.
82-
-Ex:{'The Eatery': [{'start_hour': 7, 'start_minutes': 0, 'end_hour': 0, 'end_minutes': 0}]}
85+
- Ex:{'The Eatery': [{'start_hour': 7, 'start_minutes': 0, 'end_hour': 0, 'end_minutes': 0}]}
8386
- if location_name is None, returns times for all locations
8487
- date must be in YYYY,MM,DD format, will return data on current day if None
8588
"""
8689

87-
if location_name is not None and location_name.upper() not in LOCATIONS:
88-
raise ValueError("Invalid Dining Location")
90+
if location_name is not None:
91+
location_name = location_name.upper()
92+
if location_name not in LOCATIONS:
93+
raise ValueError("Invalid Dining Location")
8994

9095
if date is None:
9196
date = datetime.now()
@@ -99,7 +104,7 @@ def get_location_hours(location_name: str, date: datetime) -> dict[str, Any]:
99104
if resp.status_code == 502:
100105
raise ValueError("Invalid Date")
101106

102-
locations = json.loads(resp.content)["the_locations"]
107+
locations = resp.json()["the_locations"]
103108

104109
if location_name is None:
105110
hours = {
@@ -108,27 +113,29 @@ def get_location_hours(location_name: str, date: datetime) -> dict[str, Any]:
108113
return hours
109114

110115
for location in locations:
111-
if location["name"].upper() == location_name.upper():
116+
if location["name"].upper() == location_name:
112117
hours = {location["name"]: day["hours"] for day in location["week"] if day["date"] == date_str}
113118
return hours
114119

115120
return {}
116121

117122

118-
def get_location_menu(location: str, date: datetime, period_name: str):
123+
def get_location_menu(location: str, date: datetime | None = None, period_name: str | None = None) -> JSON:
119124
"""Returns menu data for given dining location on given day/period
120125
- period_name used for locations with different serving periods(i.e. 'Breakfast','Lunch','Dinner','Late Night')
121126
- None -> Returns menu for first(or only) period at location
122127
"""
123-
124-
if location.upper() not in LOCATIONS:
128+
location = location.upper()
129+
if location not in LOCATIONS:
125130
raise ValueError("Invalid Dining Location")
126131

127132
if date is None:
128133
date = datetime.today()
134+
if period_name is not None:
135+
period_name = period_name.lower()
129136

130137
date_str = date.strftime("%y-%m-%d")
131-
location_id = get_locations()[location.upper()]["id"]
138+
location_id = get_locations()[location]["id"]
132139
periods_resp = requests.get(
133140
PERIODS_URL.format(location_id=location_id, date_str=date_str),
134141
headers=REQUEST_HEADERS,
@@ -137,18 +144,18 @@ def get_location_menu(location: str, date: datetime, period_name: str):
137144
if periods_resp.status_code == 502:
138145
raise ValueError("Invalid Date")
139146

140-
periods = json.loads(periods_resp.content)["periods"]
147+
periods = periods_resp.json()["periods"]
141148
if period_name is None or len(periods) == 1:
142149
period_id = periods[0]["id"]
143150
else:
144151
for period in periods:
145-
if period["name"].lower() == period_name.lower():
152+
if period["name"].lower() == period_name:
146153
period_id = period["id"]
147154

148155
menu_resp = requests.get(
149156
MENU_URL.format(location_id=location_id, period_id=period_id, date_str=date_str),
150157
headers=REQUEST_HEADERS,
151158
)
152-
menu = json.loads(menu_resp.content)["menu"]
159+
menu: JSON = menu_resp.json()["menu"]
153160

154161
return menu

0 commit comments

Comments
 (0)