Skip to content

Instantly share code, notes, and snippets.

@inthuriel
Created May 29, 2022 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inthuriel/94c65578bedfa830fa930d9f27422927 to your computer and use it in GitHub Desktop.
Save inthuriel/94c65578bedfa830fa930d9f27422927 to your computer and use it in GitHub Desktop.
Sample integration with weather API
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