From 89a70e8b4931907cb8fb0325fc67289b97a3bc80 Mon Sep 17 00:00:00 2001 From: Nitish Chauhan Date: Mon, 2 Oct 2017 15:53:44 +0530 Subject: [PATCH] handled edge cases || refactored the code || handled responses of endpoints --- app.py | 10 ++-------- app.pyc | Bin 0 -> 1383 bytes joke.py | 13 ++++++++----- joke.pyc | Bin 654 -> 799 bytes 4 files changed, 10 insertions(+), 13 deletions(-) create mode 100644 app.pyc diff --git a/app.py b/app.py index b6459a5..1606ac8 100644 --- a/app.py +++ b/app.py @@ -13,22 +13,16 @@ class APP(Resource): def get(self): + # function is called when the user makes a GET request to / endpoint. return {'this is soon to become an awesome->': 'website'} - def post(self): - jk = joke.getJoke() - jk = jk.encode('ascii', 'ignore').decode('ascii') - #jk = jk.encode('utf-8') - return jk - - class API(Resource): def get(self): + # function is called when the user makes a GET request to /api endpoint. jk = joke.getJoke() jk = jk.encode('ascii', 'ignore').decode('ascii') - # jk = jk.encode('utf-8') return jk diff --git a/app.pyc b/app.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5f17808c604faa9f29035f3309d1ee6b96c38d69 GIT binary patch literal 1383 zcmb_b&5qMR4E7}b-;^x~^#GSjTq2>88`=X}0gI5}uv(>DHkT-yaob7rQ)MPvtpum# z2|01#EqDO1C++r%Dx%n9dp!2|v&a2C9$ig-T&J-6Tw31}>0iVo{Q`1;LO>)yu{vy10bN)M zkUjVga1U@Dc_90H>)^pU1{^~62#~giaPLr{Rv&{LwR#`q32Ypwe{$aJ6r=|@p`b#P zRD=r)e@q3cKlJ&6B-17KF;DcycUT<|3sF~1ouXk--fh*TW~at1=Vz_y>goYEj`5yI zKPHC2a1mNmN8~`buqjgZVd8KXMEZo7B1ific)jp>Z(hUoopZnwnZ5fstwMQtqPLu_SN>z*0(})%I6snaO~|YE^%BY1;()%kv)!!N;WyEVUN^9S}Prs zJ7gHRJ2r98*GjX$GRZWFNqm|Oz>oV6Yy0)=Io%S5z1VaX(-#ReJ{A- ziB6R=T%1~!RgFGFY?v`kpM;Nc`wBFzFg zo_H>7Z$ciNaKMar4q1-x#E{}VCE9m_kqp~nu~D1E1G5muo=+^sG6HRe?!%@g)RBXOBDxv^lYa%E>G zOi2F= D6bKD* literal 0 HcmV?d00001 diff --git a/joke.py b/joke.py index 5bcdae9..68a52a8 100644 --- a/joke.py +++ b/joke.py @@ -5,12 +5,15 @@ def random_digits(joke_count): # Return a joke index between first and last joke in data - return randint(1, joke_count) + if joke_count and joke_count >= 1: + return [True,randint(1, joke_count)] + return [False,"json dataset is empty"] -def get_joke(): +def getJoke(): # Return random joke with open('data.json') as data_file: data = json.load(data_file) - joke = data[random_digits(len(data))] - print joke - return joke + joke = random_digits(len(data)) + if joke[0]: + return data[joke[1]] + return joke[1] \ No newline at end of file diff --git a/joke.pyc b/joke.pyc index f665d7fe7e5a84343547431a5ff6f480f8361aef..e0f82c992612189e32bac25130bba1aeb86b7dab 100644 GIT binary patch literal 799 zcmbtS%SyvQ6up!9EEPd<(Oow}5H)^)h@f=gqF^KV*p$$W?btLEI=2hKu5|Ce`73^a zo_nMC0S=jyb0_zCCh_N1E7|?HAJNZ>@IJ>5hkyc)p_1sK;SrUFZi<-Ra1>Foq9hil zELlZ>`@-rc29yO3MuM_eurIJf3V0!!?+YSux+a`q8+nq zR9P87I#LDhKrkpMV}hyeYt2^4{jMJfk_R6V^T)yUVjQX&pYN}m@4Q3}P0N?SF8{$?6Jy3(qnfQO1_9kg5$D1Sr}z+h)hKOxx%@1Myjo-o# zfOi)JbDj9*-TCfv?;L%#njc4>_mA|uG`?rp{t#gBD$0o-Dy}J4bYmiVgG-aC35hsp z5Z965zOj0-0u2lYQkQE+*yq^34M6+?sA-`n5WV76wA3iVVLivGZXan?rFEFIv@os+ zG1m&4l_Rr5AJSO}jX&EKCWb)*eh(~pE+;mbdpk=qYsZ~YIkug2>Jld1=}TxKh^v>A zp>dfDo`v`|0I4-RB7ys2mq<7ULEBKVRP?^fTA+5MSqMB3b&JVe-$7J}Xi3yw;7rrS zC14;DdY*eWX-K;xUlwtVOEQ#GTL@vdki1MypO0iXqLuT8PcF9-z*dz4MXRdk*=9Ce zA(cDwO#rEFwWC_9uB5L%3l9v{n53CL-@*USR+#q&5>F( Bd@uk2