Skip to content

Commit

Permalink
GF-16: Update endpoint logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfedorchuk committed Dec 27, 2023
1 parent 0d0a71a commit 6cd4160
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
load_dotenv()


def record_data(temp: str) -> tuple:
def record_data(temp: int, location: str) -> tuple:
try:
# Init BigQuery
client = bigquery.client.Client()
Expand All @@ -18,7 +18,7 @@ def record_data(temp: str) -> tuple:
# Insert rows
errors = client.insert_rows(
table,
[{'dt': datetime.utcnow(), 'location': 'home', 'sensor_id': 1, 'scale': 'celsius', 'temperature': temp}]
[{'dt': datetime.utcnow(), 'location': location, 'sensor_id': 1, 'scale': 'celsius', 'temperature': temp}]
)

if errors:
Expand All @@ -35,10 +35,11 @@ def record_data(temp: str) -> tuple:
def record(request):
request_args = request.args

if request_args and 'temperature' in request_args:
return record_data(request_args['temperature'])
if request_args and 'temperature' in request_args and 'location' in request_args:
return record_data(request_args['temperature'], request_args['temperature'])

if request_args and 'get_table' in request_args:
return f"Table from env: {os.environ.get('TABLE')}"
if request_args and 'readme' in request_args:
return "Endpoint for logging temperature data to the BigQuery. Pass temperature:int and location: str as a " \
"params at current endpoint POST request"

return 'Try one more time ...'
return 'Following params needed: temperature & location. More info - send readme as a parameter'

0 comments on commit 6cd4160

Please sign in to comment.