Python Forum
hi everyone , i new to
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hi everyone , i new to
#1
import sys
import requests
import json
import os
TOKEN = (os.getenv("UPTIMEROBOT_API_KEY") or "#########")
DEBUG = True
API_ENDPOINT = "https://api.uptimerobot.com/v2/"
VALID_ACTIONS = ["add_monitor", "remove_monitor", "get_monitor"]
def print_banner():
  print(sys.argv[0], ": a non-interactive cli tool for uptimerobot")
def print_usage():
  print("usage: " + sys.argv[0] + " <add_monitor|remove_monitor|get_monitor> <url>")
def validate_params(list_of_args = []):
  if (len(list_of_args) <=1) or (list_of_args[1] not in VALID_ACTIONS):
    print_usage()
    print("invalid params; exiting.")
    exit(1)
  else:
    print("invoked with action: " + list_of_args[1] + " and url: " + list_of_args[2])
def make_http_call(resource = "", additional_payload = ""):
  endpoint_url = API_ENDPOINT + resource
  payload = "api_key=" + TOKEN + "&format=json&logs=1&" + additional_payload 
  headers = {"content-type": "application/x-www-form-urlencoded", "cache-control": "no-cache"}
  print("making http POST request to endpoint: " + endpoint_url + " with payload: " + payload)
  response = requests.request("POST", endpoint_url, data=payload, headers=headers)
  print("response code: " + str(response.status_code))
  if DEBUG:
    print("response content: " + response.text)
  if response.status_code == 200:
    return response.text
  else:
    return None
def get_monitor(url = ""):
  method_name = "getMonitors"
  print("calling make_http_call with method: " + method_name)
  raw_response = make_http_call(method_name)
  if raw_response is None:
    print("call failed; as response is None. exiting.")
    exit(1)
  response = json.loads(raw_response)
  monitor_found = False
  monitor_id = -1
  if len(response["monitors"]) > 0:
    for monitor in response["monitors"]:
      if monitor["url"] == url:
        monitor_found = True
        monitor_id = monitor["id"]
        break
  final_response = {url: monitor_id}
  print("monitor: " + str(final_response))
  return final_response
def add_monitor(url = ""):
  # to-do implement add_monitor
  pass
def delete_monitor(url = ""):
  # to-do implement delete_monitor
  pass
if __name__ == "__main__":
  print_banner()
  list_of_args = sys.argv
  validate_params(list_of_args)
  action = list_of_args[1]
  url = list_of_args[2]
  if(action == "get_monitor"):
    print(get_monitor(url))
  elif(action == "add_monitor"):
    print(add_monitor(url))
  else:
    print(delete_monitor(url))
hello everyone , i seeking help to debug the program , i wanted to know what's happening in the each module level , please help me out
Thanks
Reply
#2
Are you just sharing code? Did you have a question? By the way, whitespace is your friend.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020