-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotes.py
210 lines (145 loc) · 4.66 KB
/
notes.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'''
intro:
- discuss what backend is
- talk about flask, its uses, compare to node and django
- explain what an api is
- explain how rest apis evolved from socket connections
- "in this workshop, we are going to be making a simple rest api using flask
'''
'''
setup:
- you are welcome to follow along or just watch
- if you want to follow along, you can open up a replit project -> click on the package manager (box) -> search for flask -> click the plus
- if you are using a native encvironment on your own machine, you can install flask with 'pip install Flask'
- ^^ explain what pip is and explain that python is required for native setups
'''
'''
NOTES:
- To run the client on replit, navigate to 'shell' -> 'python client.py'
- link: https://replit.com/@hershyz/HackGwinnett-Backend-Workshop#main.py
'''
# init: explain modules, what this below code does, etc
from flask import Flask
import random
app = Flask(__name__)
@app.route("/")
def home():
return "hello flask!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=random.randint(2000, 9000))
# get requests: explain server vs client, api endpoints, get requests
# -- main.py (server)
from flask import Flask
import random
app = Flask(__name__)
@app.route("/")
def home():
return "hello flask!"
@app.route("/get_endpoint", methods=["GET"])
def get_endpoint():
return "hello client!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=random.randint(2000, 9000))
# -- client.py
import requests
r = requests.get("https://HackGwinnett-Backend-Workshop.hershyz.repl.co/get_endpoint")
print(r.text)
# payloads: explain what json is (parameterized generic data), explain how we can send json to apis, and how apis can parse it
# --main.py (server)
from flask import Flask, request # MAKE SURE TO INCLUDE REQUEST IMPORT AND EXPLAIN IT
import random
app = Flask(__name__)
@app.route("/")
def home():
return "hello flask!"
@app.route("/get_greeting", methods=["GET"])
def get_greeting():
data = request.get_json()
name = data['name']
return "hello " + name + "!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=random.randint(2000, 9000))
# --client.py
import requests
payload = {
"name": "bob"
}
r = requests.get("https://HackGwinnett-Backend-Workshop.hershyz.repl.co/get_greeting", json=payload)
print(r.text)
# post requests: explain what post requests are (distinguished from get requests)
'''
- let's say I wanted to make a web application where users can enter their names and then find the names of others
- for adding the names to the server, I'm going to use something called a post request
- a post request is another type of http request, just like get requests, but the parameters sent through the post request can't be seen by the url
- post requests are more secure for the transfer of data and typically used when adding values from a client
'''
# --main.py (server)
from flask import Flask, request
import random
app = Flask(__name__)
fnames = []
lnames = []
@app.route("/")
def home():
return "hello flask!"
@app.route("/put_name", methods=["POST"])
def put_name():
data = request.get_json()
fname = data['fname']
lname = data['lname']
fnames.append(fname)
lnames.append(lname)
return "added " + str(fname) + ", " + str(lname)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=random.randint(2000, 9000))
# --client.py
import requests
payload = {
"fname": "john",
"lname": "doe"
}
r = requests.post("https://HackGwinnett-Backend-Workshop.hershyz.repl.co/put_name", json=payload)
print(r.text)
# get request iterating through our arrays
# -- main.py (server)
from flask import Flask, request
import random
app = Flask(__name__)
fnames = []
lnames = []
@app.route("/")
def home():
return "hello flask!"
@app.route("/put_name", methods=["POST"])
def put_name():
data = request.get_json()
fname = data['fname']
lname = data['lname']
fnames.append(fname)
lnames.append(lname)
return "added " + str(fname) + ", " + str(lname)
@app.route("/get_last_name", methods=["GET"])
def get_last_name():
data = request.get_json()
fname = str(data['fname'])
for i in range(0, len(fnames)):
if fnames[i] == fname:
return lnames[i]
return "could not find user!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=random.randint(2000, 9000))
# client.py (adding users, post)
import requests
payload = {
"fname": "john",
"lname": "doe"
}
r = requests.post("https://HackGwinnett-Backend-Workshop.hershyz.repl.co/put_name", json=payload)
print(r.text)
# client.py (getting users, get)
import requests
payload = {
"fname": "john",
}
r = requests.get("https://HackGwinnett-Backend-Workshop.hershyz.repl.co/get_last_name", json=payload)
print(r.text)