Created
February 29, 2020 11:57
-
-
Save Vivek205/6fc940dce633cf93e57768dca135fb19 to your computer and use it in GitHub Desktop.
Executing BookService gRPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { grpc } from "@improbable-eng/grpc-web"; | |
import { BookService } from "./Book_pb_service"; | |
parseGetBookMessage = message => { | |
// parse the binary response back to appropriate type described in the proto | |
const book = { | |
isbn: message.getIsbn(), | |
title: message.getTitle(), | |
author: message.getAuthor() | |
}; | |
return book; | |
}; | |
export const getBookDetails = () => { | |
// domain where the service is hosted | |
const host = "http://localhost:5489"; | |
// selecting the method from the service | |
const methodDescriptor = BookService.GetBook; | |
// constructing the request class | |
const request = new methodDescriptor.requestType(); | |
// setting the body for request | |
request.setIsbn(325); | |
// options for the grpc call | |
const options = { | |
host, | |
request, | |
// callback to be executed when the API call is completed | |
onEnd: response => { | |
const { status, statusMessage, headers, message, trailers } = response; | |
if (status === grpc.Code.OK && message) { | |
const book = parseGetBookMessage(message); | |
console.log("all ok. got book: ", book); | |
} | |
console.log("error.code", status, "error.message", statusMessage); | |
} | |
}; | |
return grpc.unary(methodDescriptor, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment