saving intial files"

This commit is contained in:
2022-01-10 11:09:49 -06:00
commit a572b69670
7 changed files with 292 additions and 0 deletions
+152
View File
@@ -0,0 +1,152 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
+15
View File
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Request Handler",
"type": "python",
"request": "launch",
"program": "./request_handler.py",
"console": "integratedTerminal",
"autoReload": {
"enable": true
}
}
]
}
+97
View File
@@ -0,0 +1,97 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
from views import get_all_animals
from http.server import BaseHTTPRequestHandler, HTTPServer
# Here's a class. It inherits from another class.
# For now, think of a class as a container for functions that
# work together for a common purpose. In this case, that
# common purpose is to respond to HTTP requests from a client.
class HandleRequests(BaseHTTPRequestHandler):
# This is a Docstring it should be at the beginning of all classes and functions
# It gives a description of the class or function
"""Controls the functionality of any GET, PUT, POST, DELETE requests to the server
"""
# Here's a class function
def _set_headers(self, status):
# Notice this Docstring also includes information about the arguments passed to the function
"""Sets the status code, Content-Type and Access-Control-Allow-Origin
headers on the response
Args:
status (number): the status code to return to the front end
"""
self.send_response(status)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
# Another method! This supports requests with the OPTIONS verb.
def do_OPTIONS(self):
"""Sets the options headers
"""
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE')
self.send_header('Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept')
self.end_headers()
# Here's a method on the class that overrides the parent's method.
# It handles any GET request.
def do_GET(self):
"""Handles GET requests to the server
"""
# Set the response code to 'Ok'
self._set_headers(200)
# Your new console.log() that outputs to the terminal
print(self.path)
if self.path == "/animals":
response = get_all_animals()
# It's an if..else statement
else:
response = []
# This weird code sends a response back to the client
self.wfile.write(f"{response}".encode())
# Here's a method on the class that overrides the parent's method.
# It handles any POST request.
def do_POST(self):
"""Handles POST requests to the server
"""
# Set response code to 'Created'
self._set_headers(201)
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
response = f"received post request:<br>{post_body}"
self.wfile.write(response.encode())
# Here's a method on the class that overrides the parent's method.
# It handles any PUT request.
def do_PUT(self):
"""Handles PUT requests to the server
"""
self.do_POST()
# This function is not inside the class. It is the starting
# point of this application.
def main():
"""Starts the server on port 8088 using the HandleRequests class
"""
host = ''
port = 8088
HTTPServer((host, port), HandleRequests).serve_forever()
if __name__ == "__main__":
main()
+1
View File
@@ -0,0 +1 @@
from .animal_requests import get_all_animals
+27
View File
@@ -0,0 +1,27 @@
ANIMALS = [
{
"id": 1,
"name": "Snickers",
"species": "Dog",
"locationId": 1,
"customerId": 4
},
{
"id": 2,
"name": "Gypsy",
"species": "Dog",
"locationId": 1,
"customerId": 2
},
{
"id": 3,
"name": "Blue",
"species": "Cat",
"locationId": 2,
"customerId": 1
}
]
def get_all_animals():
return ANIMALS
View File
View File