import requests import json base_url="http://127.0.0.1:5000{endpoint}" def list_all_counters(): response = requests.get( base_url.format(endpoint="/counters") ) print("----------------------------------") print("All Counters: ") for k,v in response.json()["all_counters"].items(): print("{next_key}: {next_value}".format( next_key=k, next_value=v )) print("----------------------------------") print("") def add_counter(counter_name, initial_value): response = requests.post( base_url.format( endpoint = "/counters" ), data=json.dumps({ 'new_counter_name': counter_name, 'new_counter_starting_value': initial_value }), headers={ 'Content-Type': 'application/json' } ) print("----------------------------------") if response.status_code == 200: print("Successfully Added New Counter") else: print("Failed to Add New Counter, Check Server Log") print("----------------------------------") print("") def list_specific_counter(counter_name): response = requests.get( base_url.format( endpoint = "/counters/{counter_name}".format( counter_name=counter_name ) ) ) print("----------------------------------") if(response.status_code == 404): print("Counter name {counter_name} does not exist".format(counter_name=counter_name)) else: print("Counter Name: {counter_name}".format(counter_name=counter_name)) print("Counter Value: {counter_value}".format(counter_value=response.json()['counter_value'])) print("----------------------------------") print("") def modify_counter(counter_name, increment): response = requests.post( base_url.format( endpoint = "/counters/{counter_name}/{inc_dec}".format( counter_name=counter_name, inc_dec = "increment" if increment else "decrement" ) ) ) print("----------------------------------") if(response.status_code == 404): print("Counter name {counter_name} does not exist".format(counter_name=counter_name)) else: print("Counter Updated: {counter_name}".format(counter_name=counter_name)) print("Old Value: {old_value}".format(old_value=response.json()['old_value'])) print("New Value: {new_value}".format(new_value=response.json()['new_value'])) print("----------------------------------") print("") while True: print("What do you want to do?") print("1. List All Counters") print("2. List a Specific Counter") print("3. Add a Counter") print("4. Increment a Specific Counter") print("5. Decrement a Specific Counter") print("6. Quit") user_input = input("Your Choice: ") user_input_as_int = 0 try: user_input_as_int = int(user_input) except: print("User input is not a number, try again") print("") continue if user_input_as_int == 1: list_all_counters() elif user_input_as_int == 2: what_counter = input("What Counter?: ") list_specific_counter(what_counter) elif user_input_as_int == 3: new_counter_name = input("New Counter Name: ") string_counter_initial_value = input("New Counter Initial Value: ") int_counter_initial_value = 0 try: int_counter_initial_value = int(string_counter_initial_value) except: print("Input initial value is not an integer") print("") continue add_counter(new_counter_name, int_counter_initial_value) elif user_input_as_int == 4: what_counter = input("What Counter?: ") modify_counter(what_counter, True) elif user_input_as_int == 5: what_counter = input("What Counter?: ") modify_counter(what_counter, False) elif user_input_as_int == 6: break else: print("Not a valid input number") print("")