From 9e38221e3256dead5f4d8d0cf6f623d6003d70d0 Mon Sep 17 00:00:00 2001 From: Keith Galli Date: Sun, 29 Oct 2023 16:24:20 -0400 Subject: [PATCH 1/3] Added processtext endpoint --- main_template.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/main_template.py b/main_template.py index 2b15666..9e910b4 100644 --- a/main_template.py +++ b/main_template.py @@ -7,7 +7,6 @@ swagger = Swagger(app) class UppercaseText(Resource): - def get(self): """ This method responds to the GET request for this endpoint and returns the data in uppercase. @@ -34,8 +33,63 @@ def get(self): """ text = request.args.get('text') - return jsonify({"text": text.upper()}) + return {"text": text.upper()}, 200 + +class ProcessText(Resource): + def get(self): + """ + This method responds to the GET request for processing text and returns the processed text. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be processed + - name: duplication_factor + in: query + type: integer + required: false + description: The number of times to duplicate the text + - name: capitalization + in: query + type: string + required: false + enum: [UPPER, LOWER, None] + description: The capitalization style for the text + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + processed_text: + type: string + description: The processed text + """ + text = request.args.get('text') + duplication_factor = int(request.args.get('duplication_factor', 1)) + capitalization = request.args.get('capitalization', 'None') + + # Validate capitalization input + if capitalization not in ['UPPER', 'LOWER', 'None']: + return {"error": "Invalid capitalization value"}, 400 + + # Process the text based on duplication_factor and capitalization + if capitalization == 'UPPER': + text = text.upper() + elif capitalization == 'LOWER': + text = text.lower() + + processed_text = text * duplication_factor + + return {"processed_text": processed_text}, 200 +api.add_resource(ProcessText, "/process_text") api.add_resource(UppercaseText, "/uppercase") if __name__ == "__main__": From 7c84c172ec33c9661941c75a02cd6f6e27dfc073 Mon Sep 17 00:00:00 2001 From: bryansup Date: Mon, 28 Oct 2024 18:59:13 +0700 Subject: [PATCH 2/3] adding stuff --- __pycache__/book_review.cpython-312.pyc | Bin 0 -> 2185 bytes app.py | 66 +++++++++++++++++++++++- testing.py | 8 +++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 __pycache__/book_review.cpython-312.pyc create mode 100644 testing.py diff --git a/__pycache__/book_review.cpython-312.pyc b/__pycache__/book_review.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ef209d82e8e48a6f93cb72eb6c80d05daa91ba6e GIT binary patch literal 2185 zcmah~-A@!(6u&cb_hV*uVG&pbMTZuH>le~gD}gpe!3x6G!nQHnht0V6f{U{sy)&Rt zOtz^>moyD)ObSWUgvT|ShX124AT(L0ZGx#uCYx zkU?5Ul$*y8VtI&LPgFOqfcTlpQ|+};D|-?k!Aap!og4qZKBRunIrQpG8sZHQ^XO}0 z?xPJqA=3D>^r*?TC(SKj7;glR$p1ZPBMFoJDZ$O6k})~pdcUcHht+VeY-fd8A=3Js z>!T6DmilukyRLs|EY^QvWH1r?^vYn=ZqReNRQL3pF>)o-9UHtde#>sKt{XF>qy1l8 z&i0(?PQN#%C##@4H(-zw{I-7tv4kek9dTY@`{#wJYFizvs&JDcg3}O8#7W_fm^lC* z;kS55j)z1r_po*|m;!R{UGr#OeB^OK8%Ggu)W!pY;}>^)r^a+Em6?R%$1mF6${`}! zYFfXYU^JO!#I&)QWmZJ8J=3`yWwyuCuN%~sbi?4vWOh1Zu^`-OE8H-V)0v(&IRu)f z?R?zzxk;JXm9;EgPddh3S7)tkM!RU}rrDL7wQgiHr}SLv6uqsda|Z3Yp3UBb%V$#b z*6G}=T|Y^!1YG9k!F4l&ozL2hwR|A8eM8 z#SNS_#kG4D5#|waSp+Wc&M(27%K*10y5qe7MSa-0BXm9xY~MtdrKb(u7E>f5vU;$x zJrgNrT0ldb(bLrAiD--uxgF&7S9+015q21KVfdMRO7rLsD5ZCycO_iHD>J3f){b-) zdNd{YNU>#kU=z1*sr8kFYx}^_cKYx!Y~bq(NY4l+j0%4; zVODx`XXa7O#AE1%jd?Ga3ehgTDkjM&FGgVRfW8)fMJL2fz7yVY{7qg(9&P!8#n`&9 zW%*huw%*oVu5Wr)EvIP{hs)JsY=0t=)>D~8V(!rRjqEKA`pIWbOHyWmUaQX8oKhJ- ztxr44_#XoR-3kw9w)VK{nh6mMTJ@a^jnSN;C#j~pnm~-2mNrUFQ=g=4KaX>O3ywWI z$OQ-BIVbsCpx-duEZU*P(MYo`W=&hk&FU%05yqYI4%j}LnMtv1#+IN4TkgL)RQ*i) zF7%HNCWZ!Vf92x|I|)YGp|&%x4sD(0GY%rhg!4R+usu1Jf)7Xtw5*MbLtJojGY!N8 zXi$TZ!>vm%dalV3v#h@`TT~zBk$S9KB4F3K0)~4rB@O{FBPv4dJ#f6 zP?&oxwLHc0nsRItN1kB0aC9-U5Lq5sl}le2A{$EYChq&ki#$rjqPjh_iQCF*OMajn zYF<+BsVftuSn1Mg`&!5QMRg-|c9pD$dhHzUC00!W% literal 0 HcmV?d00001 diff --git a/app.py b/app.py index 997d548..1918282 100644 --- a/app.py +++ b/app.py @@ -127,11 +127,73 @@ def post(self): else: return {"message": "Failed to add record"}, 500 - +class StringGenerator(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: message + in: query + type: string + required: true + description: The text to be converted to uppercase dude! + - name: duplication_factor + in: query + type: integer + required: false + description: number of times to duplicates the text (default is 1) + - name: capitalization + in: query + type: string + required: false + enum: [UPPER, LOWER] + description: Capitalization style UPPER, LOWER, or None (default) + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + generated_text: + type: string + description: The generated text + """ + + args = request.args + message = args["message"] + + duplication_factor = int(args.get("duplication_factor",1)) + capitalization = args.get("capitalization", None) + + if capitalization == "UPPER": + message = message.upper() + elif capitalization == "LOWER": + message = message.lower() + generated_text = (message + " ") * duplication_factor + return {"generated_text": generated_text}, 200 api.add_resource(AddRecord, "/add-record") api.add_resource(Records, "/records") api.add_resource(UppercaseText, "/uppercase") +api.add_resource(StringGenerator,"/generator") if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(debug=True) + +# +#In this code, we have a Flask application with a RESTful API. The application has four endpoints: `/generate`, `/uppercase`, `/records`, and `/add-record`. Each endpoint corresponds to a different resource in the `resources` module. +# +#The `StringGenerator` resource handles the `/generate` endpoint. It takes a message and optional parameters for duplication factor and capitalization style. The message is then processed according to the provided parameters and returned as a generated text. +# +#The `UppercaseText` resource handles the `/uppercase` endpoint. It takes a text and converts it to uppercase. +# +#The `Records` resource handles the `/records` endpoint. It retrieves a specified number of books from the database and returns them as a list of dictionaries. +# +#The `AddRecord` resource handles the `/add-record` endpoint. It takes a JSON object containing a book and a rating, and adds a new record to the database. +# +#The `flasgger` module is used \ No newline at end of file diff --git a/testing.py b/testing.py new file mode 100644 index 0000000..d19e10a --- /dev/null +++ b/testing.py @@ -0,0 +1,8 @@ +import requests +import json + +url_endpoint = "http://127.0.0.1:5000/uppercase" +resp = requests.get(url_endpoint, params = {"text" : "this is some text"}) + +print(resp.json()) +print(resp.status_code) \ No newline at end of file From 477ac5d84f9691fd1924f8e6795eef78651ac25f Mon Sep 17 00:00:00 2001 From: bryansup Date: Mon, 28 Oct 2024 18:59:22 +0700 Subject: [PATCH 3/3] adding stuff --- .DS_Store | Bin 0 -> 8196 bytes .env | 5 + Book_Review_class.py | 141 ++++++++++++ .../book_review_module4.cpython-310.pyc | Bin 0 -> 1659 bytes .../book_review_module4.cpython-312.pyc | Bin 0 -> 2442 bytes __pycache__/whatsappImportant.cpython-312.pyc | Bin 0 -> 286 bytes book_review_module1.py | 28 +++ book_review_module2.py | 42 ++++ book_review_module3.py | 51 +++++ book_review_module4.py | 64 ++++++ bryan_api.py | 208 ++++++++++++++++++ python_class_api.py | 114 ++++++++++ testing.py | 8 - testing_env_cuy.py | 17 ++ testing_env_variable.py | 11 + testing_to_hit_api.py | 10 + testingoy.py | 5 + 17 files changed, 696 insertions(+), 8 deletions(-) create mode 100644 .DS_Store create mode 100644 .env create mode 100644 Book_Review_class.py create mode 100644 __pycache__/book_review_module4.cpython-310.pyc create mode 100644 __pycache__/book_review_module4.cpython-312.pyc create mode 100644 __pycache__/whatsappImportant.cpython-312.pyc create mode 100644 book_review_module1.py create mode 100644 book_review_module2.py create mode 100644 book_review_module3.py create mode 100644 book_review_module4.py create mode 100644 bryan_api.py create mode 100644 python_class_api.py delete mode 100644 testing.py create mode 100644 testing_env_cuy.py create mode 100644 testing_env_variable.py create mode 100644 testing_to_hit_api.py create mode 100644 testingoy.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e31d77d57d88b68caf346b16871211f13c420fe6 GIT binary patch literal 8196 zcmeHM&2G~`5dOBUpjE3XDnIQ7$xjfezyr7;IaC4;?E|<;Q?(IEQ_>{B&3E2`SK#?L z!8fz3vW_LDO8YweC@&wMlc&BWF?09fvp+j-% zopzb3!{uJuY77_ybq1t&-)6U7;09kR{d;jd9G~}w;~q8SM|H&6{HcRyz$5nhH$0kR zxuE-fL3bBDjCp*Zp9#MX%rW5lDQPQyzD0hXqIG9`64@4vcE}iRc_d<3yW~S)H^OvalC_^RZ0hkQ*~6J1<0Ym)0h z$_|q2IA4$P7RPvl4&LJwooZz$ld*P#?01PQKBnYFkQjeUy3m&gA*cmnM0}S1e#=`g7&P2HYKM8nH})X zeQYu(6{MX7q;Yybl7-05zrgMN&(gGE46J~Gb?@Uq`v1=I@Bb@^#9SBy#=zfVz-_co z+bwzzy>%@P>9sS?Gfo+zS9+-?T(ljh)OMWm?hiwrXH*rNJjl{ZUg5A00ZM}{#=uG$ F_yxcYH(US! literal 0 HcmV?d00001 diff --git a/.env b/.env new file mode 100644 index 0000000..d2851cd --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +TESTING_TOKEN=9876543210 +testing = 123 +AIRTABLE_API_KEY=patOstkQza7ECxkUO.3850dfefa32e1a5fb8ec1213bfb58da43eb76961a495d2cfd1a3455eb41b5ff1 +AIRTABLE_BASE_ID=app2X5z6J4Q5Q7J4 +AIRTABLE_TABLE_ID=tblYAreTaEsBGzRdG \ No newline at end of file diff --git a/Book_Review_class.py b/Book_Review_class.py new file mode 100644 index 0000000..d87a080 --- /dev/null +++ b/Book_Review_class.py @@ -0,0 +1,141 @@ +from flask import Flask, jsonify, request +from flask_restful import Api, Resource +from flasgger import Swagger +import book_review_module4 + +app = Flask(__name__) +api = Api(app) +swagger = Swagger(app) + +br = book_review_module4.BookReview() +class AllReview(Resource): + def get(self): + """ + This method responds to the GET request for retrieving all book reviews. + --- + tags: + - Book Reviews + parameters: + - name: sort + in: query + type: string + required: false + enum: ["ASC", "DESC"] + description: Sort reviews by rating in ascending or descending order (optional) + - name: max_records + in: query + type: integer + required: false + description: Maximum number of records to retrieve (optional) + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: array + items: + type: object + properties: + book_title: + type: string + description: The title of the book + book_rating: + type: number + description: The rating of the book + book_notes: + type: string + description: The notes of the book + """ + # Retrieve optional parameters from the query string + sort = request.args.get('sort',default=None) + max_records = int(request.args.get('max_records', default=10)) + + #Validate the sort parameter + if sort and sort not in ["ASC", "DESC"]: + return{"error": "Invalid value for 'sort' parameter"}, 400 + # Sort the reviews if sort parameter is provided + if sort == "ASC": + book_reviews = br.get_book_ratings(sort=sort, max_records=max_records) + elif sort == "DESC": + book_reviews = br.get_book_ratings(sort=sort, max_records=max_records) + else: + book_reviews = br.get_book_ratings(max_records=max_records) + #delete below + #else: + # book_reviews_sorted = book_reviews + + #(not needed handled in previous one) Limit the number of records if max_records parameter is provided + #if max_records: + # book_reviews_sorted = book_reviews_sorted[:int(max_records)] + + return book_reviews, 200 + +class PostReview(Resource): + def post(self): + """ + This method responds to the GET request for retrieving all book reviews. + --- + tags: + - Post Book Reviews + parameters: + - in: body + name: body + required: true + schema: + id: BookReview + required: + - book + - rating + properties: + book: + type: string + description: The title of the book + rating: + type: integer + description: Insert Book rating here (Required) + notes: + type: string + description: Insert notes here (Required) + + # - name: book_title + # in: query + # type: string + # required: true + # description: Insert Book Title here (Required) + # - name: book_rating + # in: query + # type: integer + # required: true + # description: Insert Book Rating here, example 1.0 (Required) + # - name: notes + # in: query + # type: string + # required: false + # description: Notes about the book / summary (optional) + responses: + 200: + description: A successful POST request + 400: + description: Bad request, missing 'Book' or 'Rating' in the request body + """ + data = request.json() + if not data: + return{"error":"Request body must be in JSON format."}, 400 + + book = data.get("book") + rating = data.get("rating") + notes = data.get("notes","") + + if not book or not rating: + return {"error":"Both 'book' and 'rating' are required fields."}, 400 + + br.add_book_ratings(book, rating, notes) + return{"message":"Book Review added successfully."},201 + +# Add the resource to the API +api.add_resource(AllReview, '/all_reviews') +api.add_resource(PostReview, '/post_reviews') + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/__pycache__/book_review_module4.cpython-310.pyc b/__pycache__/book_review_module4.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..399612e21c54a9e0e284d97afa97255069039712 GIT binary patch literal 1659 zcmb7E&2Aev5GJ`lTCY~JEEiR_JtMrmL{#gzEt_&#wyqd&L#{T)O1fuf+`fiBNH3;UJu4C^WMNeP z3QVUvO2>~kUY1Ii8-+1RG8h!6x!HJrZi-xNWl33XjLywbk*_7AbWNQl!%?O-)@?Yi z&l8DJVved-_u1}??&kN~qWk=r_-=cjx0BJx?4R^MJ38C{W^kmx?eeziXZu~PUL@P) z=FWIm?o|Fksxnzt{%&H@{BVG!E)EBbCos#0AQa(L63!%*?2>RtE=f=Nmz29Qlr7o{AMv@S#E&D+iFNJd^Y@Rn_`QsYEKPz}lbz%5JH$gEvGkL*$=N|3BJ& zXhdk+i5M30G*ge(N9R=_M4G2Yhz{f}vAPz0hmP<4)A#zVNADU5Kfuy&hX=~vFl>S` zp^^f(0?7{PYbRd@+X1^JU2E3>7II7{3>m&hZY&3E|BAhKCoX_g8$V4CI-R^QowCs8 z^RTlpv@)kU?;sZI7M%4Y%N`DsGiYs4Xjy)_pvOSXy)ErSU6uRw_TJXGwl?Pnl6b-K z4Fh=Lzx2D{8Z%wT2nRC9o^FD!-$Q{Pxvwq3tcyL1xoBZT3(iu^akfsD7$qhMoPgr5 z$y&^%Yc#Rn+x0xSqbUe`c;46}rXm;@xCq1$ZO zUHpe(lIB9_d+_ZV*{kn^h*vZ^SN{C@(F_b8j?R-*TVmNO$^*EY_GQvb7>F03@j-xnD3CMy0SYWee+Z&teLXMyKWeUmFXq?qD|}1ryA+U!M$~s)8q#J6d;!)( U^nur8!F?at!?=q5uE@ literal 0 HcmV?d00001 diff --git a/__pycache__/book_review_module4.cpython-312.pyc b/__pycache__/book_review_module4.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6e279d52e89f28cc5b26f3a177b6d3edde476639 GIT binary patch literal 2442 zcmb7FO-vg{6rTO{dTo9j{tBevK-#cT0HsZn3bjZ;fuyZy3TXvfl-1%Hg3aP}XO{#c zM{-o8LfR@us^lDUNTnVUg+mWL^q6}urfw9oO{7RYor!=NbN}b=FR(? znfJXn`)f^&49NK5&a?TSB>?`U3x5b@WKAsSv4s00aH!ad?ohik(VDM*ApNi~I{Z|BcyfY?eX;ICc(AgruandpO1^7U#6 z1rn{*F+Cp1D0GkYcs1$96GsltkY>`15_w8PPbiENGE5z6(6mwV zGFfs%src&Y{?9Cgt$x$Cbv+g{my>q?`Lt~&wGl(Nto~HmUNDordMe(FR`jKmf%^MA z_mCGzOX>E4Q$KWW^1{&Y$D`WN`E%MwqccuKPo?ac#kn&JD>GxU1^nTV6S3!vnIViW z=%d!~`06Aacf|QPGN9#%le!&GUW!#`p$r7r?;_GXDF&5+KxHAb22>6TkXvI^9*U4B zvyf($fFlo^=Heu}97k7T+JBLfoR zfvwLaf;$Gkl>nwvhc!uRE+Gn-!x0P|!Pe&tV#=J%E7JdE+8kn70&@strbOPB|OuQH!GYyFFY!$k*FQYp-{QTA+0zyf@zgF{0E~`BddX4uODdBRvF%9 z3CLtGOy4Ah#S!Ykk;saG8wrnJr-UjU5xWiLnpm{{-NWG~s;egzrFl5JM%HS36{asL zj}jN_JRw=ID}r!+ZuI(KsktMkZj6;`59Q{sCrizpxi2=xo;0-N2Q~-tgPVhQ7ITBe zhJiwO;7Oz@-?rJ7@7V0P{rNBLxsGCFbX_RbHLizo3ke#dfcv8Y{#hc)gA9{lGaO`Y zaU|vc9b|rs_4}0pq^GzEg>!g%Loda`1bG21g~7B{{Va+xjC3276Y0oqdhEDOnk`h7 zDn}rABg=JtDXlt$uTJZW%9CVstDBH4D7ALqS-k(wcI4c;Sdzjw>T#) z&`agCWZWA(m)`SUr$Kz0$mpwQJtAb6V;JTS5PS?e9)q?$5isGkno?*aEA4>LP3ZwR zveyU#^18C#bwk-u3XN|TsAsbthiA$)qP1N#~=<2Fhd!iO@NH)3@HpLj5!QZ3``8EjHwJ+EHGsVdNm`2 z&B&0#6vbT0sLA{iB;Z%YuWe?eU}0foXk=h$X=G`l$#RRixU%>bYe7+FUdb(%g2a-H zTfDjXNtrpR@p+}WNvTCE89syb{&Ld~Elw>e*3T~~NlZ*m&M(a?(GRFB$-&PgmT z)-R|m$;i*sO)SXNO|3}GEyzjLFV9FUDNZaX@XRg9FDgmQE72>c{Ka9Do1apelWJE4 o@-WDu#S%c`12ZEd<8216n+&WC$?X}987=$|1QZ*2i#ULi0M>d*umAu6 literal 0 HcmV?d00001 diff --git a/book_review_module1.py b/book_review_module1.py new file mode 100644 index 0000000..a3a8b78 --- /dev/null +++ b/book_review_module1.py @@ -0,0 +1,28 @@ +import os +from dotenv import load_dotenv +from pyairtable import Api + +#this is from loading the .env file, which contains our AirTable API key. +load_dotenv() +env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env'#add r in the right side of = sign for windows custom according to your own path +load_dotenv(dotenv_path=env_path) # take environment variables from .env. +#section ends here for the dot env load + +#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable + +#to get airtable api +#api_key = os.getenv("AIRTABLE_API_KEY") +#print(testing_variable) +#print(api_key) + +api = Api(os.getenv("AIRTABLE_API_KEY"))#custom variable name in your dot env +print(api) +#table = api.table('appExampleBaseId', 'tblExampleTableId') +table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG')#custom according to your own path +#table.all() +print(table.all()) + +#to get certain section to like Rating only or Book title +print(table.all()[0]['fields']['Rating']) +print(table.all()[0]['fields']['Book']) +print(table.all()[0]['fields']['Notes']) diff --git a/book_review_module2.py b/book_review_module2.py new file mode 100644 index 0000000..72551cc --- /dev/null +++ b/book_review_module2.py @@ -0,0 +1,42 @@ +import os +from dotenv import load_dotenv +from pyairtable import Api + +#this is from loading the .env file, which contains our AirTable API key. +load_dotenv() +env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env' +load_dotenv(dotenv_path=env_path) # take environment variables from .env. +#section ends here for the dot env load + +#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable + +#to get airtable api +#api_key = os.getenv("AIRTABLE_API_KEY") +#print(testing_variable) +#print(api_key) + +api = Api(os.getenv("AIRTABLE_API_KEY")) +print(api) + +#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID +#table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG') + +#print(table.all()) + +#to get certain section to like Rating only or Book title +#print(table.all()[0]['fields']['Rating']) + +class BookReview: + def __init__(self): + self.api=Api(os.getenv("AIRTABLE_API_KEY")) + self.table = self.api.table('apptYkb7hxYFchrJA','tblYAreTaEsBGzRdG') + + def get_book_ratings(self): + table = self.table.all() + return table + def add_book_ratings(self, book_title, book_rating, notes=None): + pass + +if __name__ == "__main__": + br = BookReview() + print(br.get_book_ratings()) \ No newline at end of file diff --git a/book_review_module3.py b/book_review_module3.py new file mode 100644 index 0000000..762128f --- /dev/null +++ b/book_review_module3.py @@ -0,0 +1,51 @@ +import os +from dotenv import load_dotenv +from pyairtable import Api + +#this is from loading the .env file, which contains our AirTable API key. +load_dotenv() +env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env' +load_dotenv(dotenv_path=env_path) # take environment variables from .env. +#section ends here for the dot env load + +#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable + +#to get airtable api +#api_key = os.getenv("AIRTABLE_API_KEY") +#print(testing_variable) +#print(api_key) + +api = Api(os.getenv("AIRTABLE_API_KEY")) +print(api) + +#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID +#table = api.table('apptYkb7hxYFchrJA', 'tblYAreTaEsBGzRdG') + +#print(table.all()) + +#to get certain section to like Rating only or Book title +#print(table.all()[0]['fields']['Rating']) +#print(table.all()[0]['fields']['Book']) +#print(table.all()[0]['fields']['Notes']) + +class BookReview: + def __init__(self): + #setting up authentication API key and which airtable documents + self.api=Api(os.getenv("AIRTABLE_API_KEY")) #ini authentication ke airtablenya supaya bisa akses + self.table = self.api.table('apptYkb7hxYFchrJA','tblYAreTaEsBGzRdG') # menghubungi airtable yang mana + + def get_book_ratings(self): #store all data from airtable in a variable called table then return the value of it + table = self.table.all() + return table + def add_book_ratings(self, book_title, book_rating, notes=None): + fields = {"Book":book_title, "Rating":book_rating, "Notes":notes} + self.table.create(fields=fields) + +if __name__ == "__main__": + br = BookReview() + get_book_ratings = br.get_book_ratings() + #print(br.add_book_ratings("Fantastic Beast : and what they gossiping about",2, "Al about some beasts hanging around and gossiping about their neighbour")) +# print(br.add_book_ratings("Wilson and Fantastic Beast and how to tame them",9.5,"About Wilson catching pokemons")) + print(br.add_book_ratings("Gavin Radiant Guide series 100",100,"Get to radiant under 1 seconds")) + print(br.get_book_ratings()) + diff --git a/book_review_module4.py b/book_review_module4.py new file mode 100644 index 0000000..3e6605b --- /dev/null +++ b/book_review_module4.py @@ -0,0 +1,64 @@ +import os +from dotenv import load_dotenv +from pyairtable import Api + +#this is from loading the .env file, which contains our AirTable API key. +load_dotenv() +env_path = '/Users/ottaaccount/Pyton_Class/python-api-example/.env' +load_dotenv(dotenv_path=env_path) # take environment variables from .env. +#section ends here for the dot env load + +#testing_variable = os.getenv("TESTING_TOKEN") just to test out getting an environmental variable + +#to get airtable api +#api_key = os.getenv("AIRTABLE_API_KEY") +#print(testing_variable) +#print(api_key) + +api = Api(os.getenv("AIRTABLE_API_KEY")) +print(api) + +#table = api.table('appExampleBaseId', 'tblExampleTableId') inside is the airtable base ID and table ID +base_id = str("apptYkb7hxYFchrJA") +table_id = str("tblYAreTaEsBGzRdG") +table = api.table(base_id, table_id) + +#print(table.all()) + +#to get certain section to like Rating only or Book title +print(table.all()[0]['fields']['Rating']) + +class BookReview: + def __init__(self): + self.api=Api(os.getenv("AIRTABLE_API_KEY")) + self.table = self.api.table(base_id ,table_id) + + def get_book_ratings(self, sort = None, max_records=10): + """if not sort: + return self.table.all(max_records=max_records)""" + if sort == "ASC": + rating = ["Rating"] + elif sort == "DESC": + rating = ["-Rating"] #from the big to small + else : + return self.table.all(max_records=max_records) + + table = self.table.all(sort=rating, max_records=max_records) + return table + + def add_book_ratings(self, book_title, book_rating, notes=None): + fields = {"Book":book_title, "Rating":book_rating, "Notes":notes} + self.table.create(fields=fields) + +if __name__ == "__main__": + br = BookReview() + get_book_ratings = br.get_book_ratings() + + #print(br.add_book_ratings("Fantastic Beast : and what they gossiping about",2, "Al about some beasts hanging around and gossiping about their neighbour")) + """ + print(get_book_ratings) + get_book_ratings = br.get_book_ratings(sort= "DESC") + print(get_book_ratings) + """ + get_book_ratings = br.get_book_ratings(sort= "ASC",max_records = 3) + print(get_book_ratings) \ No newline at end of file diff --git a/bryan_api.py b/bryan_api.py new file mode 100644 index 0000000..d44776c --- /dev/null +++ b/bryan_api.py @@ -0,0 +1,208 @@ +from flask import Flask, jsonify, request +from flask_restful import Api, Resource +from flasgger import Swagger + +app = Flask(__name__) +api = Api(app) +swagger = Swagger(app) + +class UppercaseText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be converted to uppercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + """ + text = request.args.get('text') + + return {"text": text.upper()}, 200 + +class LowercaseText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in lowercase. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be converted to lowercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in lowercase + """ + text=request.args.get("text") + lowercaseText = text.lower() + return {"text": lowercaseText}, 200 + +class MixedcaseText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in lowercase. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be converted to uppercase and lowercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + Mixedtext: + type: string + description: The text in mixed case + """ + text=request.args.get("text") + uppercase=text.upper() + lowercase=text.lower() + Mixedtext = uppercase + " " + lowercase + #Mixedtext = uppercase, lowercase + print (lowercase) + print (uppercase) + return {"Mixedtext": Mixedtext}, 200 + +class ProcessText(Resource): + def get(self): + """ + This method responds to the GET request for processing text and returns the processed text. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be processed + - name: duplication_factor + in: query + type: integer + required: false + description: The number of times to duplicate the text + - name: capitalization + in: query + type: string + required: false + enum: [UPPER, lower, Mixed, None] + description: The capitalization style for the text + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + processed_text: + type: string + description: The processed text + """ + text = request.args.get('text') + duplication_factor = int(request.args.get('duplication_factor', 1)) + capitalization = request.args.get('capitalization', 'None') + + # Validate capitalization input + if capitalization not in ['UPPER', 'lower','Mixed', 'None']: + return {"error": "Invalid capitalization value"}, 400 + + # Process the text based on duplication_factor and capitalization + if capitalization == 'UPPER': + text = text.upper() + elif capitalization == 'lower': + text = text.lower() + elif capitalization == 'Mixed': + uppertext = text.upper() + lowertext = text.lower() + mixed_text = uppertext + " " + lowertext + "//" + text = mixed_text + + processed_text = (text + " ") * duplication_factor + + return {"processed_text": processed_text}, 200 + + +class BrianCase(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be converted to variation of capitalization + - name: Capitalization + in: query + type: string + required: true + enum: [UPPERCASE, lowercase, None] + description: Option for the capitalization output + - name: duplication + in: query + type: integer + required: true + description: Option for the duplication factor output + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + """ + text = request.args.get('text') + + return {"text": text.upper()}, 200 + +api.add_resource(ProcessText, "/process_text") +api.add_resource(MixedcaseText, "/mixedcase") +api.add_resource(UppercaseText, "/uppercase") +api.add_resource(LowercaseText, "/lowercase") +api.add_resource(BrianCase, "/briancase") + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/python_class_api.py b/python_class_api.py new file mode 100644 index 0000000..87778e6 --- /dev/null +++ b/python_class_api.py @@ -0,0 +1,114 @@ +from flask import Flask, jsonify, request +from flask_restful import Api, Resource +from flasgger import Swagger + +app = Flask(__name__) +api = Api(app) +swagger = Swagger(app) + +class UppercaseText(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: text + in: query + type: string + required: true + description: The text to be converted to uppercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + """ + text = request.args.get('text') + + return {"text": text.upper()}, 200 + + + + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: Messages + in: query + type: string + required: true + description: The text to be converted to uppercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + """ + text = request.args.get('text') + + return {"text": text.upper()}, 200 + +class StringGenerator(Resource): + def get(self): + """ + This method responds to the GET request for this endpoint and returns the data in uppercase. + --- + tags: + - Text Processing + parameters: + - name: Messages + in: query + type: string + required: true + description: The text to be converted to uppercase + + - name: Duplication + in: query + type: string + required: true + description: The text to be converted to uppercase + + - name: Capitalization + in: query + type: string + required: true + description: The text to be converted to uppercase + responses: + 200: + description: A successful GET request + content: + application/json: + schema: + type: object + properties: + text: + type: string + description: The text in uppercase + """ + text = request.args.get('text') + + return {"text": text.upper()}, 200 + + +api.add_resource(UppercaseText, "/uppercase") +api.add_resource(StringGenerator, "/stringgenerator") + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/testing.py b/testing.py deleted file mode 100644 index d19e10a..0000000 --- a/testing.py +++ /dev/null @@ -1,8 +0,0 @@ -import requests -import json - -url_endpoint = "http://127.0.0.1:5000/uppercase" -resp = requests.get(url_endpoint, params = {"text" : "this is some text"}) - -print(resp.json()) -print(resp.status_code) \ No newline at end of file diff --git a/testing_env_cuy.py b/testing_env_cuy.py new file mode 100644 index 0000000..571f432 --- /dev/null +++ b/testing_env_cuy.py @@ -0,0 +1,17 @@ +from dotenv import load_dotenv +import os +#if load_dotenv is empty like below, it's going to load .env in the same folder +load_dotenv() + +#if load_env filled with dotenv_path=env_path it will load the .env whichever it targetted to +env_path = '/Users/ottaaccount/Pyton_Class/.env' +load_dotenv(dotenv_path=env_path) # take environment variables from .env. + + +#testing_variable = os.getenv("TESTING_TOKEN") +#AIRTABLE_API_KEY = os.getenv("AIRTABLE_API_KEY") +#print(testing_variable) +#print(AIRTABLE_API_KEY) + +test_Variable = os.getenv("testing") +print(test_Variable) \ No newline at end of file diff --git a/testing_env_variable.py b/testing_env_variable.py new file mode 100644 index 0000000..bd18fbe --- /dev/null +++ b/testing_env_variable.py @@ -0,0 +1,11 @@ +from dotenv import load_dotenv +import os + +load_dotenv() # take environment variables from .env. + + +testing_variable = os.getenv("AIRTABLE_TOKEN") +print(testing_variable) + +#path = os.environ['PATH'] +#print(path) diff --git a/testing_to_hit_api.py b/testing_to_hit_api.py new file mode 100644 index 0000000..cc0f1d9 --- /dev/null +++ b/testing_to_hit_api.py @@ -0,0 +1,10 @@ +import requests +import json + +#url_endpoint = "http://127.0.0.1:5000/uppercase" +url_endpoint = "https://learning-api-app.onrender.com/generator" + +resp = requests.get(url_endpoint, params = {"message" : "I ","duplication_factor" : 4}) + +print(resp.json()) +print(resp.status_code) \ No newline at end of file diff --git a/testingoy.py b/testingoy.py new file mode 100644 index 0000000..96ea2c5 --- /dev/null +++ b/testingoy.py @@ -0,0 +1,5 @@ +api_key= "123123" +api_secret= "234234" + +print("API_KEY: ", api_key) +print("API_SECRET: ", api_secret) \ No newline at end of file