Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
daneshk authored Jul 17, 2021
2 parents e8cf7bb + 326941b commit bc838a1
Show file tree
Hide file tree
Showing 13 changed files with 1,081 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ch02/productinfo/java/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def grpcVersion = '1.39.0'
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
compile 'com.google.protobuf:protobuf-java:3.17.2'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'javax.annotation:javax.annotation-api:1.3.2'
}

buildscript {
Expand Down
2 changes: 1 addition & 1 deletion ch02/productinfo/java/server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
compile 'com.google.protobuf:protobuf-java:3.17.2'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'javax.annotation:javax.annotation-api:1.3.2'
}

buildscript {
Expand Down
1 change: 1 addition & 0 deletions ch03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ message CombinedShipment {

- [Go](./order-service/go/README.md)
- [Java](./order-service/java/README.md)
- [Python](./order-service/python/README.md)



9 changes: 5 additions & 4 deletions ch03/order-service/go/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"context"
"google.golang.org/grpc"
"log"
pb "ordermgt/client/ecommerce"
"time"

"google.golang.org/grpc"
)

const (
Expand All @@ -19,8 +20,8 @@ func main() {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
client := pb.NewOrderManagementClient(conn)
ctx , cancel := context.WithTimeout(context.Background(), time.Second*5)
client := pb.NewOrderManagementClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

// Add Order
Expand Down Expand Up @@ -110,7 +111,7 @@ func main() {
// if err := streamProcOrder.CloseSend(); err != nil {
// log.Fatal(err)
// }
// <- channel
// channel <- struct{}{}
}

//func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrdersClient, c chan struct{}) {
Expand Down
4 changes: 0 additions & 4 deletions ch03/order-service/go/service/README.md

This file was deleted.

1 change: 0 additions & 1 deletion ch03/order-service/proto/order_management.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Deprecated !!!!
syntax = "proto3";

import "google/protobuf/wrappers.proto";
Expand Down
43 changes: 43 additions & 0 deletions ch03/order-service/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## ``OrderManagement`` Service and Client - Python Implementation

## Prerequisites
- Python 3.5 or higher
- pip version 9.0.1 or higher

If necessary, upgrade your version of pip:
```
python -m pip install --upgrade pip
```

Install gRPC and gRPC tools

```
python -m pip install --upgrade pip
python -m pip install grpcio
python -m pip install grpcio-tools
```

## Code Generation

Generate Python code by pointing to the .proto file.

```
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. order_management.proto
```


## Running the Service
Run the server with:

```
python server.py
```

## Running the Client

Run the client with:
```
python server.py
```
93 changes: 93 additions & 0 deletions ch03/order-service/python/client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from google.protobuf import wrappers_pb2
import grpc
import order_management_pb2
import order_management_pb2_grpc

import time


def run():
channel = grpc.insecure_channel('localhost:50051')



stub = order_management_pb2_grpc.OrderManagementStub(channel)

order1 = order_management_pb2.Order(items=['Item - A', 'Item - B', 'Item - C'],
price=2450.50,
description='This is a Sample order - 1 : description.',
destination='San Jose, CA')


order = stub.getOrder(order_management_pb2.Order(id='101'))
print("Order service response", order)

# Unary RPC : Adding an Order
response = stub.addOrder(order1)
print('Add order response :', response)

# Server Streaming
for order_search_result in stub.searchOrders(wrappers_pb2.StringValue(value='Item - A')):
print('Search Result : ', order_search_result)

# Client Streaming
upd_order_iterator = generate_orders_for_updates()
upd_status = stub.updateOrders(upd_order_iterator)
print('Order update status : ', upd_status)


# Bi-di Streaming
proc_order_iterator = generate_orders_for_processing()
for shipment in stub.processOrders(proc_order_iterator):
print(shipment)


def generate_orders_for_updates():
ord1 = order_management_pb2.Order(id='101', price=1000,
items=['Item - A', 'Item - B', 'Item - C', 'Item - D'],
description='Sample order description.',
destination='Mountain View, CA')
ord2 = order_management_pb2.Order(id='102', price=1000,
items=['Item - E', 'Item - Q', 'Item - R', 'Item - D'],
description='Sample order description.',
destination='San Jose, CA')
ord3 = order_management_pb2.Order(id='103', price=1000,
items=['Item - A', 'Item - K'],
description='Sample order description.',
destination='San Francisco, CA')
list = []
list.append(ord1)
list.append(ord2)
list.append(ord3)

for updated_orders in list:
yield updated_orders

def generate_orders_for_processing():
ord1 = order_management_pb2.Order(
id='104', price=2332,
items=['Item - A', 'Item - B'],
description='Updated desc',
destination='San Jose, CA')
ord2 = order_management_pb2.Order(
id='105', price=3000,
description='Updated desc',
destination='San Francisco, CA')
ord3 = order_management_pb2.Order(
id='106', price=2560,
description='Updated desc',
destination='San Francisco, CA')
ord4 = order_management_pb2.Order(
id='107', price=2560,
description='Updated desc',
destination='Mountain View, CA')
list = []
list.append(ord1)
list.append(ord1)
list.append(ord3)
list.append(ord4)

for processing_orders in list:
yield processing_orders

run()
Loading

0 comments on commit bc838a1

Please sign in to comment.