-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (112 loc) · 3.82 KB
/
app.py
File metadata and controls
133 lines (112 loc) · 3.82 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from urllib.request import Request, urlopen
from urllib.error import HTTPError
import json
import random
APPLICATION_ID = '82e58e45-694d-483e-b80f-44888d4573ce'
REST_API_KEY = 'pOFiv8szIhCnUCK7Mfuq2zeVOYU9UwTe'
BASE_URL = 'https://parse.buddy.com/parse'
CLASSES = '/classes'
CLASS_COURSE = '/course'
PARSE_HEADERS = {
'X-Parse-Application-Id': APPLICATION_ID,
'X-Parse-REST-API-Key': REST_API_KEY,
'Content-Type': 'application/json'
}
def create_course(title, author_first_name, author_last_name):
url = BASE_URL + CLASSES + CLASS_COURSE
data = {
'title': title,
'author_first_name': author_first_name,
'author_last_name': author_last_name,
'votes': 0,
'stars': 0
}
q = Request(url, method='POST', headers=PARSE_HEADERS, data=json.dumps(data).encode('utf-8'))
try:
conn = urlopen(q)
s_data = ''.join([line.decode('utf-8') for line in conn.readlines()])
result = json.loads(s_data)
conn.close()
except HTTPError as e:
result = json.dumps({
'error': e.reason,
'code': e.code
})
finally:
return result
def read_course(object_id = None):
url = BASE_URL + CLASSES + CLASS_COURSE
if object_id is not None:
url += '/' + object_id
q = Request(url, method='GET', headers=PARSE_HEADERS)
try:
conn = urlopen(q)
s_data = ''.join([line.decode('utf-8') for line in conn.readlines()])
result = json.loads(s_data)
conn.close()
except HTTPError as e:
result = json.dumps({
'error': e.reason,
'code': e.code
})
finally:
return result
def delete_course(object_id):
url = BASE_URL + CLASSES + CLASS_COURSE + '/' + object_id
q = Request(url, method='DELETE', headers=PARSE_HEADERS)
try:
conn = urlopen(q)
s_data = ''.join([line.decode('utf-8') for line in conn.readlines()])
result = json.loads(s_data)
conn.close()
except HTTPError as e:
result = json.dumps({
'error': e.reason,
'code': e.code
})
finally:
return result
def rate_course(object_id, rating):
course = read_course(object_id)
stars = course['stars']
votes = course['votes']
new_average = ((stars * votes) + rating) / (votes + 1)
url = BASE_URL + CLASSES + CLASS_COURSE + '/' + object_id
data = {
'stars': new_average,
'votes': votes + 1
}
q = Request(url, method='PUT', headers=PARSE_HEADERS, data = json.dumps(data).encode('utf-8'))
try:
conn = urlopen(q)
s_data = ''.join([line.decode('utf-8') for line in conn.readlines()])
result = json.loads(s_data)
conn.close()
except HTTPError as e:
result = json.dumps({
'error': e.reason,
'code': e.code
})
finally:
return result
if __name__ == '__main__':
create_course('Getting Started with the Python Standard Library', 'Douglas', 'Starnes')
create_course('Python 101', 'John', 'Doe')
all_courses = read_course()
for course in all_courses['results']:
print('{0}: {1}'.format(course['objectId'], course['title']))
print('Getting details for: {0]', all_courses['results'][0])
first_object_id = all_courses['results'][0]['objectId']
course = read_course(first_object_id)
print(course)
for _ in range(10):
rating = random.randint(1, 5)
print('Adding rating of {0}'.format(rating))
rate_course(first_object_id, rating)
updated_course = read_course(first_object_id)
print('Average rating is now {0}'.format(updated_course['stars']))
print('removing course')
result = delete_course(first_object_id)
print(result)
print('Trying to read nonexistent course')
print(read_course(first_object_id))