Skip to content

Commit

Permalink
adding a lot for review and going forward
Browse files Browse the repository at this point in the history
  • Loading branch information
jm1021 committed Nov 8, 2023
1 parent 03cbaea commit 94169a4
Show file tree
Hide file tree
Showing 9 changed files with 990 additions and 10 deletions.
12 changes: 12 additions & 0 deletions _includes/nav_cpthtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<tr>
<td><img src="{{site.baseurl}}//images/logo.png" height="60" title="Frontend" alt=""></td>
<td><a href="{{site.baseurl}}/cpt/home">Home</a></td>
<td><a href="{{site.baseurl}}/cpt/db-a">DB-A</a></td>
<td><a href="{{site.baseurl}}/cpt/db-b">DB-B</a></td>
<td><a href="{{site.baseurl}}/cpt/jwt">JWT</a></td>
<td><a href="{{site.baseurl}}/cpt/sass">Sass</a></td>
<td><a href="{{site.baseurl}}/cpt/game">Images</a></td>
<td><a href="{{site.baseurl}}/cpt/game">Game</a></td>
</tr>
</table>
1 change: 1 addition & 0 deletions _notebooks/2023-09-19-python-sql-U2-4a.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"courses: { csp: {week: 13} }\n",
"categories: []\n",
"type: ccc\n",
"permalink: /cpt/db-a\n",
"---"
]
},
Expand Down
1 change: 1 addition & 0 deletions _notebooks/2023-09-19-python-sql-U2-4b.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"description: Using Programs with Data is focused on SQL and database actions. Part B focuses on learning SQL commands, connections, and curses using an Imperative programming style.\n",
"courses: { csp: {week: 13} }\n",
"categories: []\n",
"permalink: /cpt/db-b\n",
"type: ccc\n",
"---"
]
Expand Down
194 changes: 194 additions & 0 deletions _notebooks/2023-11-13-PBL-control.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"---\n",
"layout: post\n",
"toc: true\n",
"title: API/Control\n",
"description: Between Model and View is Control. In this Web application it will be handled by APIs. Data will be input and displayed through View and stored in Model. Control will broker information between projects.\n",
"permalink: /cpt/api\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API/Control Terms\n",
"> APIs work with methods to GET, POST, PUT, and UPDATE data. Control helps with Requests, Response, and handling JSON. Control is glue layer, thus the term Model-View-Control (or MVC).\n",
"- POST APIs interact with CREATE methods in Model.\n",
"- GET with READ\n",
"- PUT with UPDATE.\n",
"- DELETE with DELETE.\n",
"\n",
"> During development it is best to work with Model and Control without involving View initially. To support this type of development ...\n",
"- Become familiar with [Postman](https://www.postman.com/downloads/)\n",
"- FYI, as an alternative you can become familiar with working with APIs through ***curl***\n",
"\n",
"### Resource\n",
"[API Docs](https://flask-restful.readthedocs.io/en/latest/api.html)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Control/API code\n",
"> ***Control/API concepts*** are receiving and API request and returning a response. \n",
"\n",
"1. [Define API flask objects (api/user.py)](https://github.com/nighthawkcoders/flask_portfolio/blob/main/api/user.py#L7-L11). Flask contains object that help in API definition.\n",
"2. [Register API objects (main.py)](https://github.com/nighthawkcoders/flask_portfolio/blob/main/main.py#L11-L23). Every Flask object in the project needs to be registered with the \"main\" objects.\n",
"3. [Create/POST method](https://github.com/nighthawkcoders/flask_portfolio/blob/main/api/user.py#L14-L54). Post method contains a lot of checking code, but ultimately it creates a database row in the Model and returns that row to the View.\n",
"4. [Read/GET method](https://github.com/nighthawkcoders/flask_portfolio/blob/main/api/user.py#L14-L54). This shows off Object Relational Manager performing a User (class operation) to extract all records from the table and putting them into User objects.\n",
"5. [Define API endpoints](https://github.com/nighthawkcoders/flask_portfolio/blob/main/api/user.py#L62-L64). Endpoints are somewhat patterns to be matched. Note, url_prefix at top of file supplies \"/api/users\" prefix for each pattern. Each pattern when matched invokes the correspond class. Methods are defined in class to correspond to expectations (POST, GET, UPDATE, DELETE).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from flask import Blueprint, request, jsonify\n",
"from flask_restful import Api, Resource # used for REST API building\n",
"from datetime import datetime\n",
"\n",
"from model.users import User\n",
"\n",
"# blueprint, which is registered to app in main.py\n",
"user_api = Blueprint('user_api', __name__,\n",
" url_prefix='/api/users')\n",
"\n",
"# API docs https://flask-restful.readthedocs.io/en/latest/api.html#id1\n",
"api = Api(user_api)\n",
"class UserAPI: \n",
" class _Create(Resource):\n",
" def post(self):\n",
" ''' Read data for json body '''\n",
" body = request.get_json()\n",
" \n",
" ''' Avoid garbage in, error checking '''\n",
" # validate name\n",
" name = body.get('name')\n",
" if name is None or len(name) < 2:\n",
" return {'message': f'Name is missing, or is less than 2 characters'}, 210\n",
" # validate uid\n",
" uid = body.get('uid')\n",
" if uid is None or len(uid) < 2:\n",
" return {'message': f'User ID is missing, or is less than 2 characters'}, 210\n",
" # look for password and dob\n",
" password = body.get('password')\n",
" dob = body.get('dob')\n",
"\n",
" ''' #1: Key code block, setup USER OBJECT '''\n",
" uo = User(name=name, \n",
" uid=uid)\n",
" \n",
" ''' Additional garbage error checking '''\n",
" # set password if provided\n",
" if password is not None:\n",
" uo.set_password(password)\n",
" # convert to date type\n",
" if dob is not None:\n",
" try:\n",
" uo.dob = datetime.strptime(dob, '%m-%d-%Y').date()\n",
" except:\n",
" return {'message': f'Date of birth format error {dob}, must be mm-dd-yyyy'}, 210\n",
" \n",
" ''' #2: Key Code block to add user to database '''\n",
" # create user in database\n",
" user = uo.create()\n",
" # success returns json of user\n",
" if user:\n",
" return jsonify(user.read())\n",
" # failure returns error\n",
" return {'message': f'Processed {name}, either a format error or User ID {uid} is duplicate'}, 210\n",
"\n",
" class _Read(Resource):\n",
" def get(self):\n",
" users = User.query.all() # read/extract all users from database\n",
" json_ready = [user.read() for user in users] # prepare output in json\n",
" return jsonify(json_ready) # jsonify creates Flask response object, more specific to APIs than json.dumps\n",
"\n",
" # building RESTapi endpoint\n",
" api.add_resource(_Create, '/create')\n",
" api.add_resource(_Read, '/')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing APIs\n",
"> ***Backend Testing*** of APIs is best done through Browser for simple GET APIs, but other API methods (POST, UPDATE, DELETE) will require a tool like PostMan.\n",
"\n",
"1. [Download Postman](https://www.postman.com/downloads/). This tool test APIs effectively on localhost and is great aid for debugging.\n",
"\n",
"2. [Main.py runtime configuration](https://github.com/nighthawkcoders/flask_portfolio/blob/main/main.py#L44-L47). This configuration is setup to produce same port and localhost as deployment.\n",
"\n",
"***`Run` locally as you develop*** Select main.py file in VSCode and press Play button, or press down arrow next to Play button to activate Debug testing. The below dialog will appear in Terminal, though IP address will match you machines.\n",
"\n",
"```bash\n",
"(base) machine:flask_portfolio user$ cd /Users/user/vscode/flask_portfolio ; /usr/bin/env /Users/user/opt/anaconda3/bin/python /Users/user/.vscode/extensions/ms-python.python-2022.20.2/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 61127 -- /Users/user/vscode/flask_portfolio/main.py \n",
" * Serving Flask app \"__init__\" (lazy loading)\n",
" * Environment: production\n",
" WARNING: This is a development server. Do not use it in a production deployment.\n",
" Use a production WSGI server instead.\n",
" * Debug mode: on\n",
" * Running on all addresses.\n",
" WARNING: This is a development server. Do not use it in a production deployment.\n",
" * Running on http://192.168.1.75:8086/ (Press CTRL+C to quit)\n",
" * Restarting with watchdog (fsevents)\n",
" * Debugger is active!\n",
" * Debugger PIN: 403-552-045\n",
"```\n",
"\n",
"***`Test` API GET locally with Postman***. Observe that tests may be saved. \n",
"![](images/postmain_read_get_users.png)\n",
"\n",
"***`Test` API POST locally with Postman***. In this case, Postman can be used to add new records to the table. Observe options to pass data using Body raw-json.\n",
"![](images/postman_create_post_users.png)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hacks\n",
"Objective of these hacks is to complete full stack. This include Model (flask database), View (github pages markdown), and Control (flask API endpoint).\n",
"\n",
"1. Make Create and Read API endpoints for your project\n",
"2. Test API endpoints by creating test cases in Postman\n",
"3. Build a Frontend for Create and Read endpoints\n",
"4. Make a 30-60 second video showing this work"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.5"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8b82d5009c68ba5675978267e2b13a671f2a7143d61273c5a3813c97e0b2493d"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"title: JWT Project\n",
"description: A Python Project that is secured with JSON Web Tokens (JWT)\n",
"author: Nikhil Chakravarthula\n",
"courses: { csp: {week: 13, categories: [6.B]} }\n",
"categories: [C7.0]\n",
"type: ccc\n",
"permalink: /cpt/jwt\n",
"---"
]
},
Expand Down
Loading

0 comments on commit 94169a4

Please sign in to comment.