Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created October 19, 2023 01:39
Show Gist options
  • Save heiswayi/dca58ea84e57345aba8fa929ddf9bebe to your computer and use it in GitHub Desktop.
Save heiswayi/dca58ea84e57345aba8fa929ddf9bebe to your computer and use it in GitHub Desktop.
Python script to query REST API endpoint with X interval
import requests
import time
def query_rest_api(api_url):
try:
response = requests.get(api_url)
if response.status_code == 200:
return response.json() # Assuming the API returns JSON data
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")
return None
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
def main():
api_url = "https://api.example.com/data" # Replace with the actual API URL you want to query
while True:
data = query_rest_api(api_url)
if data:
# Process the retrieved data as needed
print("Retrieved data:", data)
# Sleep for a period of time before making the next request (e.g., every 5 minutes)
time.sleep(300) # Sleep for 300 seconds (5 minutes)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment