Initial Commit
This commit is contained in:
commit
10451a566f
139
apiclient.py
Normal file
139
apiclient.py
Normal file
@ -0,0 +1,139 @@
|
||||
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("")
|
||||
|
92
apiserver_in_memory.py
Normal file
92
apiserver_in_memory.py
Normal file
@ -0,0 +1,92 @@
|
||||
from flask import Flask, request, jsonify
|
||||
|
||||
data_store = {}
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/counters", methods=['GET', 'POST'])
|
||||
def handle_counters_endpoint():
|
||||
if request.method == 'GET':
|
||||
response_dict = {
|
||||
'all_counters': data_store
|
||||
}
|
||||
return jsonify(response_dict), 200
|
||||
elif request.method == 'POST':
|
||||
if request.content_type == 'application/json':
|
||||
body = request.get_json()
|
||||
|
||||
if not "new_counter_name" in body.keys() or not "new_counter_starting_value" in body.keys():
|
||||
error_response = {
|
||||
'status': 'ERROR',
|
||||
'reason': 'Body for posting to /counters endpoint must contain both new_counter_name and new_counter_starting_value',
|
||||
'input_body': body
|
||||
}
|
||||
return jsonify(error_response), 400
|
||||
|
||||
data_store[request.get_json()['new_counter_name']] = request.get_json()['new_counter_starting_value']
|
||||
return jsonify({'status': 'OK'}), 200
|
||||
else:
|
||||
error_response = {
|
||||
'status': 'ERROR',
|
||||
'reason': 'Content Type for endpoint /counters must be application/json'
|
||||
}
|
||||
return jsonify(error_response), 400
|
||||
|
||||
@app.route("/counters/<counter_name>", methods=['GET'])
|
||||
def handle_counters_subcounter_endpoint(counter_name):
|
||||
if not counter_name in data_store.keys():
|
||||
error_response = {
|
||||
'status': 'ERROR',
|
||||
'reason': 'Counter name {counter} does not exist'.format(counter=counter_name)
|
||||
}
|
||||
return jsonify(error_response), 404
|
||||
|
||||
data_response = {
|
||||
'counter_name': counter_name,
|
||||
'counter_value': data_store[counter_name]
|
||||
}
|
||||
|
||||
return jsonify(data_response), 200
|
||||
|
||||
@app.route("/counters/<counter_name>/increment", methods=['POST'])
|
||||
def handle_counters_subcounter_increment(counter_name):
|
||||
if not counter_name in data_store.keys():
|
||||
error_response = {
|
||||
'status': 'ERROR',
|
||||
'reason': 'Counter name {counter} does not exist'.format(counter=counter_name)
|
||||
}
|
||||
return jsonify(error_response), 404
|
||||
|
||||
data_response = {
|
||||
'counter_name': counter_name,
|
||||
'old_value': data_store[counter_name],
|
||||
'new_value': data_store[counter_name] + 1
|
||||
}
|
||||
|
||||
data_store[counter_name] = data_store[counter_name] + 1
|
||||
|
||||
return jsonify(data_response), 200
|
||||
|
||||
@app.route("/counters/<counter_name>/decrement", methods=['POST'])
|
||||
def handle_counters_subcounter_decrement(counter_name):
|
||||
if not counter_name in data_store.keys():
|
||||
error_response = {
|
||||
'status': 'ERROR',
|
||||
'reason': 'Counter name {counter} does not exist'.format(counter=counter_name)
|
||||
}
|
||||
return jsonify(error_response), 404
|
||||
|
||||
data_response = {
|
||||
'counter_name': counter_name,
|
||||
'old_value': data_store[counter_name],
|
||||
'new_value': data_store[counter_name] - 1
|
||||
}
|
||||
|
||||
data_store[counter_name] = data_store[counter_name] - 1
|
||||
|
||||
return jsonify(data_response), 200
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
Loading…
Reference in New Issue
Block a user