Merge pull request #9 from jazzm0/YuriPetukhin

ProductCatalog Integration with Device
This commit is contained in:
WeiHan0801 2019-12-11 12:13:37 +01:00 committed by GitHub
commit f70c18694c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 139 additions and 109 deletions

View file

@ -1,3 +1,6 @@
# Hipster Shop: Participants
Yuri Petukhin
# Hipster Shop: Cloud-Native Microservices Demo Application
This project contains a 10-tier microservices application. The application is a

View file

@ -244,6 +244,9 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq
log.Infof("order confirmation email sent to %q", req.Email)
}
resp := &pb.PlaceOrderResponse{Order: orderResult}
registerDevicesFromOrder(req, orderResult, log)
return resp, nil
}

View file

@ -0,0 +1,61 @@
package main
import pb "github.com/GoogleCloudPlatform/microservices-demo/src/checkoutservice/genproto"
import (
"encoding/json"
"net/http"
)
import "github.com/sirupsen/logrus"
import "bytes"
type Device struct {
UserId string
ProductId string
}
type Devices []Device
func registerDevicesFromOrder(req *pb.PlaceOrderRequest, order *pb.OrderResult, log *logrus.Logger) {
var devices []Device
for _, orderItem := range order.GetItems() {
var item = orderItem.Item
device := Device{
UserId: req.Email,
ProductId: item.ProductId,
}
devices = append(devices, device)
}
log.Infof("Device registration, prepared payload: %+v", devices)
devicesJSON, err := json.Marshal(devices)
if err != nil {
log.Errorf("Device registration, failed to prepare payload as json, err: %+v", err)
}
url := "https://hackathon-sap19-wal-1025.appspot.com/devices"
httpRequest, httpError := http.NewRequest("POST", url, bytes.NewBuffer(devicesJSON))
if httpError == nil {
httpRequest.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
log.Infof("Device registration, http request: %+v", httpRequest)
httpResponse, httpResponseError := httpClient.Do(httpRequest)
if httpResponseError != nil {
log.Errorf("Device registration, http request preparation failed, err: %+v", httpResponseError)
}
log.Infof("Device registration, http response: %+v", httpResponse)
log.Infof("Device registration, http response Body: %+v", httpResponse.Body)
defer httpResponse.Body.Close()
} else {
log.Errorf("Device registration, http request preparation failed, err: %+v, URL: %+v", httpError, url)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View file

@ -18,15 +18,12 @@ import random
from locust import HttpLocust, TaskSet
products = [
'0PUK6V6EV0',
'1YMWWN1N4O',
'2ZYFJ3GM2N',
'66VCHSJNUP',
'6E92ZMYYFZ',
'9SIQT8TOJO',
'L9ECAV7KIM',
'LS4PSXUNUM',
'OLJCESPC7Z']
'0000000001',
'0000000002',
'0000000003',
'0000000004',
'0000000005',
'0000000006']
def index(l):
l.client.get("/")

View file

@ -1,110 +1,76 @@
{
"products": [
{
"id": "OLJCESPC7Z",
"name": "Vintage Typewriter",
"description": "This typewriter looks good in your living room.",
"picture": "/static/img/products/typewriter.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 67,
"nanos": 990000000
},
"categories": ["vintage"]
},
{
"id": "66VCHSJNUP",
"name": "Vintage Camera Lens",
"description": "You won't have a camera to use it and it probably doesn't work anyway.",
"picture": "/static/img/products/camera-lens.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 12,
"nanos": 490000000
},
"categories": ["photography", "vintage"]
},
{
"id": "1YMWWN1N4O",
"name": "Home Barista Kit",
"description": "Always wanted to brew coffee with Chemex and Aeropress at home?",
"picture": "/static/img/products/barista-kit.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 124
},
"categories": ["cookware"]
},
{
"id": "L9ECAV7KIM",
"name": "Terrarium",
"description": "This terrarium will looks great in your white painted living room.",
"picture": "/static/img/products/terrarium.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 36,
"nanos": 450000000
},
"categories": ["gardening"]
},
{
"id": "2ZYFJ3GM2N",
"name": "Film Camera",
"description": "This camera looks like it's a film camera, but it's actually digital.",
"picture": "/static/img/products/film-camera.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 2245
},
"categories": ["photography", "vintage"]
},
{
"id": "0PUK6V6EV0",
"name": "Vintage Record Player",
"description": "It still works.",
"picture": "/static/img/products/record-player.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 65,
"nanos": 500000000
},
"categories": ["music", "vintage"]
},
{
"id": "LS4PSXUNUM",
"name": "Metal Camping Mug",
"description": "You probably don't go camping that often but this is better than plastic cups.",
"picture": "/static/img/products/camp-mug.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 24,
"nanos": 330000000
},
"categories": ["cookware"]
},
{
"id": "9SIQT8TOJO",
"name": "City Bike",
"description": "This single gear bike probably cannot climb the hills of San Francisco.",
"picture": "/static/img/products/city-bike.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 789,
"nanos": 500000000
},
"categories": ["cycling"]
},
{
"id": "6E92ZMYYFZ",
"name": "Air Plant",
"description": "Have you ever wondered whether air plants need water? Buy one and figure out.",
"picture": "/static/img/products/air-plant.jpg",
"id": "0000000001",
"name": "Tamagotchi schwarz Blau",
"description": "Bandai Tamagotchi Elektronisches Spiel, durchscheinend blau.",
"picture": "/static/img/products/tamagotchi_dark_blue.jpg",
"priceUsd": {
"currencyCode": "USD",
"units": 12,
"nanos": 300000000
},
"categories": ["gardening"]
}
"categories": ["toy"]
},
{
"id": "0000000002",
"name": "Tamagotchi Red",
"description": "Nice red tamagotchi",
"picture": "/static/img/products/tamagotchi_red.jpg",
"priceUsd": {
"currencyCode": "EUR",
"units": 10,
"nanos": 300000000
},
"categories": ["toy"]
},
{
"id": "0000000003",
"name": "Tamagotchi Yellow",
"description": "Boring yellow tamagotchi",
"picture": "/static/img/products/tamagotchi_yellow.jpg",
"priceUsd": {
"currencyCode": "EUR",
"units": 10,
"nanos": 300000000
},
"categories": ["toy"]
},
{
"id": "0000000004",
"name": "Tamagotchi Blue",
"description": "New blue tamagotchi",
"picture": "/static/img/products/tamagotchi_blue.jpg",
"priceUsd": {
"currencyCode": "JPY",
"units": 1001,
"nanos": 300000000
},
"categories": ["toy"]
},
{
"id": "0000000005",
"name": "Tamagotchi Purple",
"description": "Useless purple tamagotchi",
"picture": "/static/img/products/tamagotchi_purple.jpg",
"priceUsd": {
"currencyCode": "CAD",
"units": 20,
"nanos": 300000000
},
"categories": ["toy"]
},
{
"id": "0000000006",
"name": "Tamagotchi Pink",
"description": "Pikachu pink tamagotchi",
"picture": "/static/img/products/tamagotchi_pink.jpg",
"priceUsd": {
"currencyCode": "EUR",
"units": 40,
"nanos": 300000000
},
"categories": ["toy"]
}
]
}