A Brief Walk of gRPC 

## What is gRPC

gRPC is a high performance, open-source universal RPC framework, developed by Google. In gRPC, a client application can directly call methods on a server application on a different machine as if it was a local object, making it easier to create distributed applications and services.
One of the primary benefits of using gRPC is for documentation, you can use your service configuration and API interface definition files to generate reference documentation for your API. 

The reason for this is that, gRPC can use protocol buffers as both its **Interface Definition Language (IDL)** and as it underlying message interchange format.
As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
On the server side, the server implements this interface and runs a gRPC server to handle client calls.
On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.

![https://grpc.io/img/landing-2.svg](https://grpc.io/img/landing-2.svg)

source: [Introduction to gRPC](https://www.grpc.io/docs/what-is-grpc/introduction/)

### Why gRPC?

The most important reason to use gRPC is that, it is **fast**.

- it utilize HTTP/2 by default
- protocol buffers usually has only 10% size of JSON message

Another important reason that may be not so obvious is that, it helps programmers writer **BETTER** code, because you have to follow its guidelines to generate API interfaces.



## Working with Protocol Buffers

By default, gRPC uses Protocol Buffers, Google's open source mechanism for serializing structured data. And it can also be used with other data formats such as JSON. You can check the language guide below.
[Language Guide | Protocol Buffers | Google Developers](https://developers.google.com/protocol-buffers/docs/overview)

A simple example of protocol buffers is as below,

```proto

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}
```

One great advantage of protocol buffers is that, it is language neutral. Protocol buffers currently support generated code in Java, Python, Objective-C, and C++. And with proto3 language version, you can also work with Dart, Go, Ruby, and C#.

## Protocol Buffers with Python

### Install

Install gRPC

```shell
$ python -m pip install grpcio
```

Install gRPC tools

```shell
$ python -m pip install grpcio-tools
```

### Define Protocol Format
To work with protocol buffers using Python, the first thing you need to do is to define your protocol format. You start with a `.proto` file, the following is an official example.

```proto

syntax = "proto2";

package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}
```

### Compiling Your Protocol Buffers

Once you have the `.proto` file, you need compile it. [Download](https://developers.google.com/protocol-buffers/docs/downloads) the compiler here.
Then compile it using the following command

```shell

protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/sample.proto
```

This will generate a `sample_pb2.py` in the given destination. Then you can easily import it into your python code as follows.

```python

import sample_pb2

Address_Book = sample_pb2.AddressBook()
```