Bottom Page

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
buran wrote Apr-14-2020, 01:38 PM:
TOKEN removed. You don't want your token exposed in public forum. Maybe it is best to generate a new one
Quote
#2
Are you just sharing code? Did you have a question? By the way, whitespace is your friend.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)