-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheety_data_manager.py
More file actions
76 lines (65 loc) · 2.8 KB
/
sheety_data_manager.py
File metadata and controls
76 lines (65 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import requests
from requests.auth import HTTPBasicAuth
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Sheety API endpoint for flight deals
SHEETY_PRICES_ENDPOINT = "https://api.sheety.co/8f7dac75c174db2fc7a1b07db76b96fd/flightDeals/prices"
class SheetyDataManager:
def __init__(self):
# Initialize authentication credentials from environment variables
self._user = os.environ["USER_NAME"] # Username for authentication
self._password = os.environ["PASSWORD"] # Password for authentication
self._authorization = HTTPBasicAuth(self._user, self._password) # HTTP Basic Authentication
self.display_data = {} # Dictionary to store fetched data
def get_sheet_data(self):
"""
Fetches all data from the Sheety API and stores it in `self.display_data`.
Returns the fetched data.
"""
response = requests.get(url=SHEETY_PRICES_ENDPOINT, auth=self._authorization)
if response.status_code == 200:
data = response.json()
self.display_data = data["prices"] # Extract "prices" data from the API response
return self.display_data
else:
print(f"Error fetching data: {response.status_code}, {response.text}")
return None
def update_sheet_data(self):
"""
Updates the IATA codes for all cities in the Sheety database.
Uses the `iataCode` field for each city to send PUT requests.
"""
for city in self.display_data:
new_data = {
"price": {
"iataCode": city["iataCode"] # New IATA code to update
}
}
# Send a PUT request to update the data for the current city
response = requests.put(
url=f"{SHEETY_PRICES_ENDPOINT}/{city['id']}", # Target URL with city ID
json=new_data, # JSON data for the update
auth=self._authorization
)
# Log the response status and details
print(f"Response for {city['city']}: {response.status_code}, {response.text}")
def delete_sheet_data(self):
"""
Resets the IATA codes for all cities in the Sheety database to an empty string.
"""
for city in self.display_data:
update = {
"price": {
"iataCode": "" # Reset the IATA code
}
}
# Send a PUT request to reset the IATA code for the current city
response = requests.put(
url=f"{SHEETY_PRICES_ENDPOINT}/{city['id']}",
json=update,
auth=self._authorization
)
# Log the response details
print(f"Reset response for {city['city']}: {response.text}")