Skip to content

Commit

Permalink
Add Lesson 4 files for combining message passing techniques
Browse files Browse the repository at this point in the history
  • Loading branch information
leejustin committed Oct 26, 2020
1 parent 70508b8 commit 757fee9
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 28 deletions.
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`.
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()
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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask
kafka-python
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"
]
}
]
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 lesson-4-message-passing-in-production/flask-kafka-starter/app.py
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()
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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask
kafka-python
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"
]
}
]
22 changes: 0 additions & 22 deletions lesson-4-name-of-lesson/exercises/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions lesson-4-name-of-lesson/exercises/solution/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions lesson-4-name-of-lesson/exercises/starter/README.md

This file was deleted.

0 comments on commit 757fee9

Please sign in to comment.