Add emailservice

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
michaelawyu 2018-07-24 21:06:39 -07:00 committed by Ahmet Alp Balkan
parent 432c5462a0
commit 05381b3626
14 changed files with 2460 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,20 @@
# Use the grpc.io provided Python image as the base image
FROM grpc/python:1.0
# show python logs as they occur
ENV PYTHONUNBUFFERED=0
# install pip for python3
RUN apt-get -qqy update && \
apt-get -qqy install python3-pip
# get packages
WORKDIR /email_server
COPY requirements.txt .
RUN pip3 install -r requirements.txt
# Add the application
COPY . .
EXPOSE 8080
ENTRYPOINT [ "python3", "email_server.py" ]

1750
src/emailservice/demo_pb2.py Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,475 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc
import demo_pb2 as demo__pb2
class CartServiceStub(object):
"""-----------------Cart service-----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.AddItem = channel.unary_unary(
'/hipstershop.CartService/AddItem',
request_serializer=demo__pb2.AddItemRequest.SerializeToString,
response_deserializer=demo__pb2.Empty.FromString,
)
self.GetCart = channel.unary_unary(
'/hipstershop.CartService/GetCart',
request_serializer=demo__pb2.GetCartRequest.SerializeToString,
response_deserializer=demo__pb2.Cart.FromString,
)
self.EmptyCart = channel.unary_unary(
'/hipstershop.CartService/EmptyCart',
request_serializer=demo__pb2.EmptyCartRequest.SerializeToString,
response_deserializer=demo__pb2.Empty.FromString,
)
class CartServiceServicer(object):
"""-----------------Cart service-----------------
"""
def AddItem(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def GetCart(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def EmptyCart(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_CartServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'AddItem': grpc.unary_unary_rpc_method_handler(
servicer.AddItem,
request_deserializer=demo__pb2.AddItemRequest.FromString,
response_serializer=demo__pb2.Empty.SerializeToString,
),
'GetCart': grpc.unary_unary_rpc_method_handler(
servicer.GetCart,
request_deserializer=demo__pb2.GetCartRequest.FromString,
response_serializer=demo__pb2.Cart.SerializeToString,
),
'EmptyCart': grpc.unary_unary_rpc_method_handler(
servicer.EmptyCart,
request_deserializer=demo__pb2.EmptyCartRequest.FromString,
response_serializer=demo__pb2.Empty.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.CartService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class RecommendationServiceStub(object):
"""---------------Recommendation service----------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.ListRecommendations = channel.unary_unary(
'/hipstershop.RecommendationService/ListRecommendations',
request_serializer=demo__pb2.ListRecommendationsRequest.SerializeToString,
response_deserializer=demo__pb2.ListRecommendationsResponse.FromString,
)
class RecommendationServiceServicer(object):
"""---------------Recommendation service----------
"""
def ListRecommendations(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_RecommendationServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'ListRecommendations': grpc.unary_unary_rpc_method_handler(
servicer.ListRecommendations,
request_deserializer=demo__pb2.ListRecommendationsRequest.FromString,
response_serializer=demo__pb2.ListRecommendationsResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.RecommendationService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class ProductCatalogServiceStub(object):
"""---------------Product Catalog----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.ListProducts = channel.unary_unary(
'/hipstershop.ProductCatalogService/ListProducts',
request_serializer=demo__pb2.Empty.SerializeToString,
response_deserializer=demo__pb2.ListProductsResponse.FromString,
)
self.GetProduct = channel.unary_unary(
'/hipstershop.ProductCatalogService/GetProduct',
request_serializer=demo__pb2.GetProductRequest.SerializeToString,
response_deserializer=demo__pb2.Product.FromString,
)
self.SearchProducts = channel.unary_unary(
'/hipstershop.ProductCatalogService/SearchProducts',
request_serializer=demo__pb2.SearchProductsRequest.SerializeToString,
response_deserializer=demo__pb2.SearchProductsResponse.FromString,
)
class ProductCatalogServiceServicer(object):
"""---------------Product Catalog----------------
"""
def ListProducts(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def GetProduct(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def SearchProducts(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_ProductCatalogServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'ListProducts': grpc.unary_unary_rpc_method_handler(
servicer.ListProducts,
request_deserializer=demo__pb2.Empty.FromString,
response_serializer=demo__pb2.ListProductsResponse.SerializeToString,
),
'GetProduct': grpc.unary_unary_rpc_method_handler(
servicer.GetProduct,
request_deserializer=demo__pb2.GetProductRequest.FromString,
response_serializer=demo__pb2.Product.SerializeToString,
),
'SearchProducts': grpc.unary_unary_rpc_method_handler(
servicer.SearchProducts,
request_deserializer=demo__pb2.SearchProductsRequest.FromString,
response_serializer=demo__pb2.SearchProductsResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.ProductCatalogService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class ShippingServiceStub(object):
"""---------------Shipping Service----------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.GetQuote = channel.unary_unary(
'/hipstershop.ShippingService/GetQuote',
request_serializer=demo__pb2.GetQuoteRequest.SerializeToString,
response_deserializer=demo__pb2.GetQuoteResponse.FromString,
)
self.ShipOrder = channel.unary_unary(
'/hipstershop.ShippingService/ShipOrder',
request_serializer=demo__pb2.ShipOrderRequest.SerializeToString,
response_deserializer=demo__pb2.ShipOrderResponse.FromString,
)
class ShippingServiceServicer(object):
"""---------------Shipping Service----------
"""
def GetQuote(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def ShipOrder(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_ShippingServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'GetQuote': grpc.unary_unary_rpc_method_handler(
servicer.GetQuote,
request_deserializer=demo__pb2.GetQuoteRequest.FromString,
response_serializer=demo__pb2.GetQuoteResponse.SerializeToString,
),
'ShipOrder': grpc.unary_unary_rpc_method_handler(
servicer.ShipOrder,
request_deserializer=demo__pb2.ShipOrderRequest.FromString,
response_serializer=demo__pb2.ShipOrderResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.ShippingService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class CurrencyServiceStub(object):
"""-----------------Currency service-----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.GetSupportedCurrencies = channel.unary_unary(
'/hipstershop.CurrencyService/GetSupportedCurrencies',
request_serializer=demo__pb2.Empty.SerializeToString,
response_deserializer=demo__pb2.GetSupportedCurrenciesResponse.FromString,
)
self.Convert = channel.unary_unary(
'/hipstershop.CurrencyService/Convert',
request_serializer=demo__pb2.CurrencyConversionRequest.SerializeToString,
response_deserializer=demo__pb2.Money.FromString,
)
class CurrencyServiceServicer(object):
"""-----------------Currency service-----------------
"""
def GetSupportedCurrencies(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def Convert(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_CurrencyServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'GetSupportedCurrencies': grpc.unary_unary_rpc_method_handler(
servicer.GetSupportedCurrencies,
request_deserializer=demo__pb2.Empty.FromString,
response_serializer=demo__pb2.GetSupportedCurrenciesResponse.SerializeToString,
),
'Convert': grpc.unary_unary_rpc_method_handler(
servicer.Convert,
request_deserializer=demo__pb2.CurrencyConversionRequest.FromString,
response_serializer=demo__pb2.Money.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.CurrencyService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class PaymentServiceStub(object):
"""-------------Payment service-----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.Charge = channel.unary_unary(
'/hipstershop.PaymentService/Charge',
request_serializer=demo__pb2.ChargeRequest.SerializeToString,
response_deserializer=demo__pb2.ChargeResponse.FromString,
)
class PaymentServiceServicer(object):
"""-------------Payment service-----------------
"""
def Charge(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_PaymentServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'Charge': grpc.unary_unary_rpc_method_handler(
servicer.Charge,
request_deserializer=demo__pb2.ChargeRequest.FromString,
response_serializer=demo__pb2.ChargeResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.PaymentService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class EmailServiceStub(object):
"""-------------Email service-----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.SendOrderConfirmation = channel.unary_unary(
'/hipstershop.EmailService/SendOrderConfirmation',
request_serializer=demo__pb2.SendOrderConfirmationRequest.SerializeToString,
response_deserializer=demo__pb2.Empty.FromString,
)
class EmailServiceServicer(object):
"""-------------Email service-----------------
"""
def SendOrderConfirmation(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_EmailServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'SendOrderConfirmation': grpc.unary_unary_rpc_method_handler(
servicer.SendOrderConfirmation,
request_deserializer=demo__pb2.SendOrderConfirmationRequest.FromString,
response_serializer=demo__pb2.Empty.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.EmailService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
class CheckoutServiceStub(object):
"""-------------Checkout service-----------------
"""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.CreateOrder = channel.unary_unary(
'/hipstershop.CheckoutService/CreateOrder',
request_serializer=demo__pb2.CreateOrderRequest.SerializeToString,
response_deserializer=demo__pb2.CreateOrderResponse.FromString,
)
self.PlaceOrder = channel.unary_unary(
'/hipstershop.CheckoutService/PlaceOrder',
request_serializer=demo__pb2.PlaceOrderRequest.SerializeToString,
response_deserializer=demo__pb2.PlaceOrderResponse.FromString,
)
class CheckoutServiceServicer(object):
"""-------------Checkout service-----------------
"""
def CreateOrder(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def PlaceOrder(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_CheckoutServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
'CreateOrder': grpc.unary_unary_rpc_method_handler(
servicer.CreateOrder,
request_deserializer=demo__pb2.CreateOrderRequest.FromString,
response_serializer=demo__pb2.CreateOrderResponse.SerializeToString,
),
'PlaceOrder': grpc.unary_unary_rpc_method_handler(
servicer.PlaceOrder,
request_deserializer=demo__pb2.PlaceOrderRequest.FromString,
response_serializer=demo__pb2.PlaceOrderResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'hipstershop.CheckoutService', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))

View File

@ -0,0 +1,32 @@
import grpc
import demo_pb2
import demo_pb2_grpc
# from opencensus.trace.tracer import Tracer
# from opencensus.trace.exporters import stackdriver_exporter
# from opencensus.trace.ext.grpc import client_interceptor
# try:
# exporter = stackdriver_exporter.StackdriverExporter()
# tracer = Tracer(exporter=exporter)
# tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(tracer, host_port='0.0.0.0:8080')
# except:
# tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()
def send_confirmation_email(email, order):
channel = grpc.insecure_channel('0.0.0.0:8080')
# channel = grpc.intercept_channel(channel, tracer_interceptor)
stub = demo_pb2_grpc.EmailServiceStub(channel)
try:
response = stub.SendOrderConfirmation(demo_pb2.SendOrderConfirmationRequest(
email = email,
order = order
))
print('Request sent.')
except grpc.RpcError as err:
print(err.details())
print('{}, {}'.format(err.code().name, err.code().value))
if __name__ == '__main__':
print('Client for email service.')

View File

@ -0,0 +1,113 @@
from concurrent import futures
import argparse
import os
import sys
import time
from jinja2 import Environment, FileSystemLoader, select_autoescape, TemplateError
from google.api_core.exceptions import GoogleAPICallError
import grpc
import demo_pb2
import demo_pb2_grpc
# from opencensus.trace.ext.grpc import server_interceptor
# from opencensus.trace.samplers import always_on
# from opencensus.trace.exporters import stackdriver_exporter
# from opencensus.trace.exporters import print_exporter
# import googleclouddebugger
# try:
# sampler = always_on.AlwaysOnSampler()
# exporter = stackdriver_exporter.StackdriverExporter()
# tracer_interceptor = server_interceptor.OpenCensusServerInterceptor(sampler, exporter)
# except:
# tracer_interceptor = server_interceptor.OpenCensusServerInterceptor()
# try:
# googleclouddebugger.enable(
# module='emailserver',
# version='1.0.0'
# )
# except:
# pass
# Loads confirmation email template from file
env = Environment(
loader=FileSystemLoader('templates'),
autoescape=select_autoescape(['html', 'xml'])
)
template = env.get_template('confirmation.html')
class EmailService(demo_pb2_grpc.EmailServiceServicer):
def __init__(self):
raise Exception('cloud mail client not implemented')
super().__init__()
@staticmethod
def send_email(client, email_address, content):
response = client.send_message(
sender = client.sender_path(project_id, region, sender_id),
envelope_from_authority = '',
header_from_authority = '',
envelope_from_address = from_address,
simple_message = {
"from": {
"address_spec": from_address,
},
"to": [{
"address_spec": email_address
}],
"subject": "Your Confirmation Email",
"html_body": content
}
)
print("Message sent: {}".format(response.rfc822_message_id))
def SendOrderConfirmation(self, request, context):
email = request.email
order = request.order
try:
confirmation = template.render(order = order)
except TemplateError as err:
context.set_details("An error occurred when preparing the confirmation mail.")
print(err.message)
context.set_code(grpc.StatusCode.INTERNAL)
return demo_pb2.Empty()
try:
EmailService.send_email(self.client, email, confirmation)
except GoogleAPICallError as err:
context.set_details("An error occurred when sending the email.")
print(err.message)
context.set_code(grpc.StatusCode.INTERNAL)
return demo_pb2.Empty()
return demo_pb2.Empty()
class DummyEmailService(demo_pb2_grpc.EmailServiceServicer):
def SendOrderConfirmation(self, request, context):
print('A request to send order confirmation email to {} has been received.'.format(request.email))
return demo_pb2.Empty()
def start(dummy_mode):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))#, interceptors=(tracer_interceptor,))
if dummy_mode:
demo_pb2_grpc.add_EmailServiceServicer_to_server(DummyEmailService(), server)
else:
raise Exception('non-dummy mode not implemented')
server.add_insecure_port('[::]:8080')
server.start()
try:
while True:
time.sleep(3600)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
print('Starting the email service in dummy mode.')
start(dummy_mode = True)

3
src/emailservice/genproto.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash -e
python -m grpc_tools.protoc -I../../pb --python_out=. --grpc_python_out=. ../../pb/demo.proto

View File

@ -0,0 +1,30 @@
cachetools==2.1.0
certifi==2018.4.16
chardet==3.0.4
cryptography==1.7.1
google-api-core==1.2.1
google-auth==1.5.0
google-cloud-core==0.28.1
google-cloud-trace==0.19.0
googleapis-common-protos==1.5.3
grpc-google-iam-v1==0.11.4
grpcio==1.12.1
grpcio-tools==1.12.1
idna==2.7
Jinja2==2.10
keyring==10.1
keyrings.alt==1.3
MarkupSafe==1.0
opencensus==0.1.5
protobuf==3.6.0
pyasn1==0.4.3
pyasn1-modules==0.2.2
pycrypto==2.6.1
pygobject==3.22.0
pytz==2018.5
pyxdg==0.25
requests==2.19.1
rsa==3.4.2
SecretStorage==2.3.1
six==1.11.0
urllib3==1.23

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>Your Order Confirmation</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400,500" rel="stylesheet">
</head>
<style>
body{
font-family: 'Roboto', sans-serif;
}
</style>
<body>
<h2>Your Order Confirmation</h2>
<p>Thanks for shopping with us!<p>
<h3>Order ID</h3>
<p>#{{ order.order_id }}</p>
<h3>Shipping</h3>
<p>#{{ order.shipping_tracking_id }}</p>
<p>{{ order.shipping_cost.units }}. {{ "%02d" | format(order.shipping_cost.nanos // 10000000) }} {{ order.shipping_cost.currency_code }}</p>
<p>{{ order.shipping_address.street_address_1 }}, {{order.shipping_address.street_address_2}}, {{order.shipping_address.city}}, {{order.shipping_address.country}} {{order.shipping_address.zip_code}}</p>
<h3>Items</h3>
<table style="width:100%">
<tr>
<th>Item No.</th>
<th>Quantity</th>
<th>Price</th>
</tr>
{% for item in order.items %}
<tr>
<td>#{{ item.item.product_id }}</td>
<td>{{ item.item.quantity }}</td>
<td>{{ item.cost.units }}.{{ "%02d" | format(item.cost.nanos // 10000000) }} {{ item.cost.currency_code }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>