forked from udacity/cd0309-message-passing-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Lesson 4 files for combining message passing techniques
- Loading branch information
Showing
15 changed files
with
212 additions
and
28 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...e-passing-in-production/combining-message-passing-techniques-solution/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Flask Starter | ||
This is a bare-bones Flask application that showcases how simple it can be to set up a REST API. It also serves as a starter template for users to begin experimenting with writing their own REST API endpoints. | ||
|
||
Flask applications can look very different depending on how they are structured and implemented. This application is built to have the minimum number of dependencies to run. | ||
|
||
## Running the app | ||
1. Install Flask: `pip install Flask kafka-python` | ||
2. Run the app: `flask run` | ||
|
||
The application should be available at `localhost:5000`. |
Empty file.
52 changes: 52 additions & 0 deletions
52
lesson-4-message-passing-in-production/combining-message-passing-techniques-solution/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import json | ||
|
||
from kafka import KafkaProducer | ||
from flask import Flask, jsonify, request, g, Response | ||
|
||
from .services import retrieve_orders, create_order | ||
|
||
app = Flask(__name__) | ||
|
||
@app.before_request | ||
def before_request(): | ||
# Set up a Kafka producer | ||
TOPIC_NAME = 'items' | ||
KAFKA_SERVER = 'localhost:9092' | ||
producer = KafkaProducer(bootstrap_servers=KAFKA_SERVER) | ||
# Setting Kafka to g enables us to use this | ||
# in other parts of our application | ||
g.kafka_producer = producer | ||
|
||
|
||
@app.route('/health') | ||
def health(): | ||
return jsonify({'response': 'Hello World!'}) | ||
|
||
|
||
@app.route('/api/orders/computers', methods=['GET', 'POST']) | ||
def computers(): | ||
if request.method == 'GET': | ||
return jsonify(retrieve_orders()) | ||
elif request.method == 'POST': | ||
request_body = request.json | ||
result = create_order(request_body) | ||
return Response(status=202) | ||
else: | ||
raise Exception('Unsupported HTTP request type.') | ||
|
||
|
||
@app.route('/api/v2/orders/computers', methods=['GET', 'POST']) | ||
def computers(): | ||
if request.method == 'GET': | ||
return jsonify(retrieve_orders()) | ||
elif request.method == 'POST': | ||
request_body = request.json | ||
result = create_order(request_body) | ||
return Response(status=202) | ||
else: | ||
raise Exception('Unsupported HTTP request type.') | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
7 changes: 7 additions & 0 deletions
7
...on-4-message-passing-in-production/combining-message-passing-techniques-solution/enums.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from enum import Enum | ||
|
||
class Status(Enum): | ||
Queued = 'Queued' | ||
Processing = 'Processing' | ||
Completed = 'Completed' | ||
Failed = 'Failed' |
2 changes: 2 additions & 0 deletions
2
...sage-passing-in-production/combining-message-passing-techniques-solution/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask | ||
kafka-python |
40 changes: 40 additions & 0 deletions
40
...4-message-passing-in-production/combining-message-passing-techniques-solution/services.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import json | ||
from .app import g | ||
from .enums import Status | ||
|
||
|
||
def create_order(order_data): | ||
""" | ||
This is a stubbed method of retrieving a resource. It doesn't actually do anything. | ||
""" | ||
# Turn order_data into a binary string for Kafka | ||
kafka_data = json.dumps(order_data).encode() | ||
# Kafka producer has already been set up in Flask context | ||
kafka_producer = g.kafka_producer | ||
kafka_producer.send("items", kafka_data) | ||
|
||
|
||
def retrieve_orders(): | ||
""" | ||
This is a stubbed method of retrieving multiple resources. It doesn't actually do anything. | ||
""" | ||
return [ | ||
{ | ||
"id": "1", | ||
"status": Status.Queued.value, | ||
"created_at": "2020-10-16T10:31:10.969696", | ||
"created_by": "USER14", | ||
"equipment": [ | ||
"KEYBOARD", "MOUSE" | ||
] | ||
}, | ||
{ | ||
"id": "2", | ||
"status": Status.Queued.value, | ||
"created_at": "2020-10-16T10:29:10.969696", | ||
"created_by": "USER15", | ||
"equipment": [ | ||
"KEYBOARD", "WEBCAM" | ||
] | ||
} | ||
] |
10 changes: 10 additions & 0 deletions
10
lesson-4-message-passing-in-production/flask-kafka-starter/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Flask Starter | ||
This is a bare-bones Flask application that showcases how simple it can be to set up a REST API. It also serves as a starter template for users to begin experimenting with writing their own REST API endpoints. | ||
|
||
Flask applications can look very different depending on how they are structured and implemented. This application is built to have the minimum number of dependencies to run. | ||
|
||
## Running the app | ||
1. Install Flask: `pip install Flask kafka-python` | ||
2. Run the app: `flask run` | ||
|
||
The application should be available at `localhost:5000`. |
Empty file.
39 changes: 39 additions & 0 deletions
39
lesson-4-message-passing-in-production/flask-kafka-starter/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import json | ||
|
||
from kafka import KafkaProducer | ||
from flask import Flask, jsonify, request, g, Response | ||
|
||
from .services import retrieve_orders, create_order | ||
|
||
app = Flask(__name__) | ||
|
||
@app.before_request | ||
def before_request(): | ||
# Set up a Kafka producer | ||
TOPIC_NAME = 'items' | ||
KAFKA_SERVER = 'localhost:9092' | ||
producer = KafkaProducer(bootstrap_servers=KAFKA_SERVER) | ||
# Setting Kafka to g enables us to use this | ||
# in other parts of our application | ||
g.kafka_producer = producer | ||
|
||
|
||
@app.route('/health') | ||
def health(): | ||
return jsonify({'response': 'Hello World!'}) | ||
|
||
|
||
@app.route('/api/orders/computers', methods=['GET', 'POST']) | ||
def computers(): | ||
if request.method == 'GET': | ||
return jsonify(retrieve_orders()) | ||
elif request.method == 'POST': | ||
request_body = request.json | ||
result = create_order(request_body) | ||
return Response(status=202) | ||
else: | ||
raise Exception('Unsupported HTTP request type.') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
7 changes: 7 additions & 0 deletions
7
lesson-4-message-passing-in-production/flask-kafka-starter/enums.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from enum import Enum | ||
|
||
class Status(Enum): | ||
Queued = 'Queued' | ||
Processing = 'Processing' | ||
Completed = 'Completed' | ||
Failed = 'Failed' |
2 changes: 2 additions & 0 deletions
2
lesson-4-message-passing-in-production/flask-kafka-starter/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask | ||
kafka-python |
43 changes: 43 additions & 0 deletions
43
lesson-4-message-passing-in-production/flask-kafka-starter/services.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
from .app import g | ||
from .enums import Status | ||
|
||
|
||
def create_order(order_data): | ||
""" | ||
This is a stubbed method of retrieving a resource. It doesn't actually do anything. | ||
""" | ||
# Turn order_data into a binary string for Kafka | ||
# kafka_data = json.dumps(order_data).encode() | ||
# Kafka producer has already been set up in Flask context | ||
# kafka_producer = g.kafka_producer | ||
# TODO: send the data using kafka_producer using .send() | ||
kafka_data = json.dumps(order_data).encode() | ||
kafka_producer = g.kafka_producer | ||
kafka_producer.send("items", kafka_data) | ||
|
||
|
||
def retrieve_orders(): | ||
""" | ||
This is a stubbed method of retrieving multiple resources. It doesn't actually do anything. | ||
""" | ||
return [ | ||
{ | ||
"id": "1", | ||
"status": Status.Queued.value, | ||
"created_at": "2020-10-16T10:31:10.969696", | ||
"created_by": "USER14", | ||
"equipment": [ | ||
"KEYBOARD", "MOUSE" | ||
] | ||
}, | ||
{ | ||
"id": "2", | ||
"status": Status.Queued.value, | ||
"created_at": "2020-10-16T10:29:10.969696", | ||
"created_by": "USER15", | ||
"equipment": [ | ||
"KEYBOARD", "WEBCAM" | ||
] | ||
} | ||
] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.