-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz.py
88 lines (80 loc) · 3.17 KB
/
fizzbuzz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /bin/env python
import sys, argparse, httplib, json
def parseargs():
parser = argparse.ArgumentParser(
description='Query the Fizbuzz server JSON API.')
parser.add_argument('-n', '--number', help='A resource number')
parser.add_argument('-f', '--favourite', help='A number to make favourite')
parser.add_argument(
'-u', '--unfavourite', help='A number to make no favourite')
parser.add_argument(
'-p', '--page', metavar=('PAGE', 'SIZE'), help='Page number and size',
nargs=2)
return parser.parse_args()
def printnumber(data):
""" Print a singe number JSON data"""
print "Id:", data["id"], "Favourite:", data["attributes"]["favourite"], \
"Value:", data["attributes"]["value"]
def printerrorjson(jsondata):
""" Print the ERROR JSON values returned by the server"""
print "Error! Status: ", jsondata["errors"]["status"], " Title: ", \
jsondata["errors"]["title"], "Detail: ", jsondata["errors"]["detail"]
def makefavourite(number, favourite, connection):
""" Mark a number resource as favourite """
body = json.dumps(
{'data' : {'type' : 'numbers', 'id' : number,
'attributes' : {'favourite' : favourite}}})
connection = httplib.HTTPConnection('localhost', 8080)
connection.request('PATCH', '/numbers/' + number, body)
response = connection.getresponse()
connection.close()
def outputnumber(number, connection):
""" Query a single number resource and print its values """
connection = httplib.HTTPConnection('localhost', 8080)
connection.request('GET', '/numbers/' + number)
response = connection.getresponse()
if response.status == 404:
print "Not found!"
elif response.status == 200:
body = response.read()
jsondata = json.loads(body)
data = jsondata["data"]
printnumber(data)
else:
print "Unexpected server reply: ", response.status, response.reason
connection.close()
def outputpage(page, pagesize, connection):
""" Retrieve the page number of the given size """
connection = httplib.HTTPConnection('localhost', 8080)
url = '/numbers?page[number]=' + page + '&page[size]=' + pagesize
connection.request('GET', url)
response = connection.getresponse()
if response.status == 404:
print "Not found!"
if response.status == 400:
body = response.read()
jsondata = json.loads(body)
printerrorjson(jsondata)
elif response.status == 200:
body = response.read()
jsondata = json.loads(body)
data = jsondata["data"]
for number in data:
printnumber(number)
else:
print "Unexpected server reply: ", response.status, response.reason
connection.close()
def main():
args = parseargs()
connection = httplib.HTTPConnection('localhost', 8080)
if args.number:
outputnumber(args.number, connection)
if args.page:
outputpage(args.page[0], args.page[1], connection)
if args.favourite:
makefavourite(args.favourite, 'true', connection)
if args.unfavourite:
makefavourite(args.unfavourite, 'false', connection)
connection.close()
if __name__ == "__main__":
main()