@@ -98,6 +98,8 @@ def getForwardHeaders(request):
9898
9999 return headers
100100
101+
102+ # The UI:
101103@app .route ('/' )
102104@app .route ('/index.html' )
103105def index ():
@@ -109,66 +111,138 @@ def index():
109111
110112 return render_template ('index.html' , serviceTable = table )
111113
114+
112115@app .route ('/health' )
113116def health ():
114117 return 'Product page is healthy'
115118
119+
116120@app .route ('/login' , methods = ['POST' ])
117121def login ():
118122 user = request .values .get ('username' )
119123 response = app .make_response (redirect (request .referrer ))
120124 response .set_cookie ('user' , user )
121125 return response
122126
127+
123128@app .route ('/logout' , methods = ['GET' ])
124129def logout ():
125130 response = app .make_response (redirect (request .referrer ))
126131 response .set_cookie ('user' , '' , expires = 0 )
127132 return response
128133
134+
129135@app .route ('/productpage' )
130136def front ():
137+ product_id = 0 # TODO: replace default value
131138 headers = getForwardHeaders (request )
132139 user = request .cookies .get ("user" , "" )
133- bookdetails = getDetails (headers )
134- bookreviews = getReviews (headers )
135- return render_template ('productpage.html' , details = bookdetails , reviews = bookreviews , user = user )
140+ product = getProduct (product_id )
141+ (detailsStatus , details ) = getProductDetails (product_id , headers )
142+ (reviewsStatus , reviews ) = getProductReviews (product_id , headers )
143+ return render_template (
144+ 'productpage.html' ,
145+ detailsStatus = detailsStatus ,
146+ reviewsStatus = reviewsStatus ,
147+ product = product ,
148+ details = details ,
149+ reviews = reviews ,
150+ user = user )
151+
152+
153+ # The API:
154+ @app .route ('/api/v1/products' )
155+ def productsRoute ():
156+ return json .dumps (getProducts ()), 200 , {'Content-Type' : 'application/json' }
157+
158+
159+ @app .route ('/api/v1/products/<product_id>' )
160+ def productRoute (product_id ):
161+ headers = getForwardHeaders (request )
162+ (status , details ) = getProductDetails (product_id , headers )
163+ return json .dumps (details ), status , {'Content-Type' : 'application/json' }
136164
137- def getReviews (headers ):
138- for i in range (2 ):
139- try :
140- res = requests .get (reviews ['name' ]+ "/" + reviews ['endpoint' ], headers = headers , timeout = 3.0 )
141- except :
142- res = None
143165
144- if res and res .status_code == 200 :
145- return res .text
166+ @app .route ('/api/v1/products/<product_id>/reviews' )
167+ def reviewsRoute (product_id ):
168+ headers = getForwardHeaders (request )
169+ (status , reviews ) = getProductReviews (product_id , headers )
170+ return json .dumps (reviews ), status , {'Content-Type' : 'application/json' }
171+
172+
173+ @app .route ('/api/v1/products/<product_id>/ratings' )
174+ def ratingsRoute (product_id ):
175+ headers = getForwardHeaders (request )
176+ (status , ratings ) = getProductRatings (product_id , headers )
177+ return json .dumps (ratings ), status , {'Content-Type' : 'application/json' }
178+
179+
180+
181+ # Data providers:
182+ def getProducts ():
183+ return [
184+ {
185+ 'id' : 0 ,
186+ 'title' : 'The Comedy of Errors' ,
187+ 'descriptionHtml' : '<a href="https://en.wikipedia.org/wiki/The_Comedy_of_Errors">Wikipedia Summary</a>: The Comedy of Errors is one of <b>William Shakespeare\' s</b> early plays. It is his shortest and one of his most farcical comedies, with a major part of the humour coming from slapstick and mistaken identity, in addition to puns and word play.'
188+ }
189+ ]
190+
146191
147- return """<h3>Sorry, product reviews are currently unavailable for this book.</h3>"""
192+ def getProduct (product_id ):
193+ products = getProducts ()
194+ if product_id + 1 > len (products ):
195+ return None
196+ else :
197+ return products [product_id ]
148198
149199
150- def getDetails ( headers ):
200+ def getProductDetails ( product_id , headers ):
151201 try :
152- res = requests .get (details ['name' ]+ "/" + details ['endpoint' ], headers = headers , timeout = 1.0 )
202+ url = details ['name' ] + "/" + details ['endpoint' ] + "/" + str (product_id )
203+ res = requests .get (url , headers = headers , timeout = 3.0 )
153204 except :
154205 res = None
206+ if res and res .status_code == 200 :
207+ return (200 , res .json ())
208+ else :
209+ status = (res .status_code if res != None and res .status_code else 500 )
210+ return (status , {'error' : 'Sorry, product details are currently unavailable for this book.' })
211+
155212
213+ def getProductReviews (product_id , headers ):
214+ try :
215+ url = reviews ['name' ] + "/" + reviews ['endpoint' ] + "/" + str (product_id )
216+ res = requests .get (url , headers = headers , timeout = 3.0 )
217+ except :
218+ res = None
156219 if res and res .status_code == 200 :
157- return res .text
220+ return ( 200 , res .json ())
158221 else :
159- return """<h3>Sorry, product details are currently unavailable for this book.</h3>"""
222+ status = (res .status_code if res != None and res .status_code else 500 )
223+ return (status , {'error' : 'Sorry, product reviews are currently unavailable for this book.' })
160224
161225
162- class Writer (object ):
226+ def getProductRatings (product_id , headers ):
227+ try :
228+ url = ratings ['name' ] + "/" + ratings ['endpoint' ] + "/" + str (product_id )
229+ res = requests .get (url , headers = headers , timeout = 3.0 )
230+ except :
231+ res = None
232+ if res and res .status_code == 200 :
233+ return (200 , res .json ())
234+ else :
235+ status = (res .status_code if res != None and res .status_code else 500 )
236+ return (status , {'error' : 'Sorry, product ratings are currently unavailable for this book.' })
163237
238+ class Writer (object ):
164239 def __init__ (self , filename ):
165240 self .file = open (filename ,'w' )
166241
167242 def write (self , data ):
168243 self .file .write (data )
169244 self .file .flush ()
170245
171-
172246if __name__ == '__main__' :
173247 if len (sys .argv ) < 2 :
174248 print "usage: %s port" % (sys .argv [0 ])
@@ -177,5 +251,6 @@ def write(self, data):
177251 p = int (sys .argv [1 ])
178252 sys .stderr = Writer ('stderr.log' )
179253 sys .stdout = Writer ('stdout.log' )
254+ print "start at port %s" % (p )
180255 app .run (host = '0.0.0.0' , port = p , debug = True , threaded = True )
181256
0 commit comments