Created
May 29, 2022 13:37
-
-
Save inthuriel/94c65578bedfa830fa930d9f27422927 to your computer and use it in GitHub Desktop.
Sample integration with weather API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import requests | |
from requests.exceptions import HTTPError | |
WEATHER_API_URL="http://<api-url>" | |
WEATHER_API_ENDPOINT = "/api/v1/sensor" | |
def run() -> None: | |
api = f"{WEATHER_API_URL}{WEATHER_API_ENDPOINT}" | |
try: | |
response = requests.get(api) | |
response.raise_for_status() | |
except HTTPError as http_err: | |
print(f'HTTP error occurred: {http_err}') | |
except Exception as err: | |
print(f'Other error occurred: {err}') | |
else: | |
print('Now, at {time} we have {temp:.0f} degrees Celsius!'.format( | |
time = datetime.fromtimestamp(response.json().get('timestamp', 0)), | |
temp = response.json().get('environment', {}).get('temperature', 0) | |
)) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment