Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flask-connexion-rest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

26 changes: 26 additions & 0 deletions version_1/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Import Flask, the Python micro web framework
from flask import (
Flask,
render_template
)


# Create the application instance
app = Flask(__name__, template_folder="templates")


# Create a URL route in our application for "/"
@app.route('/')
def home():
"""
This function just responds to the browser URL
localhost:5000/

:return: the rendered template "home.html"
"""
return render_template("home.html")


if __name__ == '__main__':
app.run(debug=True)
12 changes: 12 additions & 0 deletions version_1/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Application Home Page</title>
</head>
<body>
<h2>
Hello World!
</h2>
</body>
</html>
Empty file added version_2/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions version_2/people.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This is the people module and supports all the ReST actions for the PEOPLE collection

"""

# System modules
from datetime import datetime


def get_timestamp():
return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))


# Data to serve with our API
PEOPLE = {
"Farrell": {
"fname": "Doug",
"lname": "Farrell",
"timestamp": get_timestamp()
},
"Brockman": {
"fname": "Kent",
"lname": "Brockman",
"timestamp": get_timestamp()
},
"Easter": {
"fname": "Bunny",
"lname": "Easter",
"timestamp": get_timestamp()
}
}


def read():
"""
This function responds to a request for /api/people
with the complete lists of people

:return: sorted list of people
"""
# create the list of people from our data
return [PEOPLE[key] for key in sorted(PEOPLE.keys())]
30 changes: 30 additions & 0 deletions version_2/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Main module of the server file
"""

# 3rd party moudles
from flask import render_template
import connexion


# Create the application instance
app = connexion.App(__name__, specification_dir='./')

# read the swagger.yml file to configure the endpoints
app.add_api('swagger.yml')


# Create a URL route in our application for "/"
@app.route('/')
def home():
"""
This function just responds to the browser URL
localhost:5000/

:return: the rendered template "home.html"
"""
return render_template("home.html")


if __name__ == '__main__':
app.run(debug=True)
34 changes: 34 additions & 0 deletions version_2/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
swagger: "2.0"
info:
description: This is the swagger file that goes with our server code
version: "1.0.0"
title: Swagger ReST Article
consumes:
- application/json
produces:
- application/json

basePath: /api

# Paths supported by the server application
paths:
/people:
get:
operationId: people.read
tags:
- People
summary: The people data structure supported by the server application
description: Read the list of people
responses:
200:
description: Successful read people list operation
schema:
type: array
items:
properties:
fname:
type: string
lname:
type: string
timestamp:
type: string
12 changes: 12 additions & 0 deletions version_2/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Application Home Page</title>
</head>
<body>
<h2>
Hello World!
</h2>
</body>
</html>
Empty file added version_3/__init__.py
Empty file.
130 changes: 130 additions & 0 deletions version_3/people.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""
This is the people module and supports all the ReST actions for the PEOPLE collection
"""

# System modules
from datetime import datetime

# 3rd party modules
from flask import (
request,
make_response,
abort
)


def get_timestamp():
return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))


# Data to serve with our API
PEOPLE = {
"Farrell": {
"fname": "Doug",
"lname": "Farrell",
"timestamp": get_timestamp()
},
"Brockman": {
"fname": "Kent",
"lname": "Brockman",
"timestamp": get_timestamp()
},
"Easter": {
"fname": "Bunny",
"lname": "Easter",
"timestamp": get_timestamp()
}
}


def read_all():
"""
This function responds to a request for /api/people
with the complete lists of people

:return: json string of list of people
"""
# Create the list of people from our data
return [PEOPLE[key] for key in sorted(PEOPLE.keys())]



def read_one(lname):
"""
This function responds to a request for /api/people/{lname}
with one matching person from people

:param lname: last name of person to find
:return: person matching last name
"""
# Does the person exist in people?
if lname in PEOPLE:
person = PEOPLE.get(lname)

# otherwise, nope, not found
else:
abort(404, 'Person with last name {lname} not found'.format(lname=lname))

return person


def create(person):
"""
This function creates a new person in the people structure
based on the passed in person data

:param person: person to create in people structure
:return: 201 on success, 406 on person exists
"""
lname = person.get('lname', None)
fname = person.get('fname', None)

# Does the person exist already?
if lname not in PEOPLE and lname is not None:
PEOPLE[lname] = {
'lname': lname,
'fname': fname,
"timestamp": get_timestamp()
}
return make_response('{lname} successfully created'.format(lname=lname), 201)

# Otherwise, they exist, that's an error
else:
abort(406, 'Peron with last name {lname} already exists'.format(lname=lname))


def update(lname, person):
"""
This function updates an existing person in the people structure

:param lname: last name of person to update in the people structure
:param person: person to update
:return: updated person structure
"""
# Does the person exist in people?
if lname in PEOPLE:
PEOPLE[lname]['fname'] = person.get('fname')
PEOPLE[lname]['timestamp'] = get_timestamp()

return PEOPLE[lname]

# otherwise, nope, that's an error
else:
abort(404, 'Person with last name {lname} not found'.format(lname=lname))


def delete(lname):
"""
This function deletes a person from the people structure

:param lname: last name of person to delete
:return: 200 on successful delete, 404 if not found
"""
# Does the person to delete exist?
if lname in PEOPLE:
del PEOPLE[lname]
return make_response('{lname} successfully deleted'.format(lname=lname), 200)

# Otherwise, nope, person to delete not found
else:
abort(404, 'Person with last name {lname} not found'.format(lname=lname))
30 changes: 30 additions & 0 deletions version_3/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Main module of the server file
"""

# 3rd party moudles
from flask import render_template
import connexion


# create the application instance
app = connexion.App(__name__, specification_dir='./')

# Cead the swagger.yml file to configure the endpoints
app.add_api('swagger.yml')


# Create a URL route in our application for "/"
@app.route('/')
def home():
"""
This function just responds to the browser URL
localhost:5000/

:return: the rendered template "home.html"
"""
return render_template("home.html")


if __name__ == '__main__':
app.run(debug=True)
Loading