Skip to content

Commit

Permalink
Add an example with remote methods
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Mar 5, 2024
1 parent f9e4492 commit 4c0a6b5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import ballerina/grpc;

configurable int port = 9090;

type Book record {
string id;
string title;
string author;
float price;
};

Book[] books = [
{id: "1", title: "Book One", author: "Author One", price: 29.99},
{id: "2", title: "Book Two", author: "Author Two", price: 19.99},
{id: "3", title: "Book Three", author: "Author Three", price: 39.99}
];

service "Books" on new grpc:Listener(port) {
remote function addBook(Book book) returns Book|error {
books.push(book);
return book;
}

remote function getBook(string id) returns Book|error {
Book[] book = books.filter(b => b.id == id);
if (book.length() == 0) {
return error grpc:NotFoundError(string `Cannot find the album for ID ${id}`);
}
return book[0];
}

remote function listBooks() returns stream<Book, error?>|error {
return books.toStream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ballerina/grpc;


configurable int port = 9090;

type Book record {
string id;
string title;
string author;
float price;
};

Book[] books = [
{id: "1", title: "Book One", author: "Author One", price: 29.99},
{id: "2", title: "Book Two", author: "Author Two", price: 19.99},
{id: "3", title: "Book Three", author: "Author Three", price: 39.99}
];

service "Books" on new grpc:Listener(port) {
remote function addBook(Book book) returns Book | error {
books.push(book);
return book;
}
remote function getBook(string id) returns Book | error {
Book[] book = books.filter(b => b.id == id);
if (book.length() == 0) {
return error grpc:NotFoundError(string `Cannot find the album for ID ${id}`);
}
return book[0];
}



remote function listBooks( ) returns stream<Book, error?> | error {
return books.toStream();
}
}

0 comments on commit 4c0a6b5

Please sign in to comment.