Ejemplo n.º 1
0
def protobuf_loads(raw):
    books = []
    len_idx = 0
    while len_idx < len(raw):
        msg_idx = len_idx + 4
        msg_len = struct.unpack('i', raw[len_idx: msg_idx])[0]
        book = PBBook()
        book.ParseFromString(raw[msg_idx: msg_idx + msg_len])
        books.append(book)
        len_idx = msg_idx + msg_len
    return books
Ejemplo n.º 2
0
async def mmm(channel, name, year, author_id):
    book = BookServiceStub(channel)
    loop = asyncio.new_event_loop()
    reply = loop.create_task(
        book.BookCreate(Book(name=name, year=year, author_id=author_id)))
    loop.run_until_complete(asyncio.wait(asyncio.get_event_loop()))
    return reply
Ejemplo n.º 3
0
 async def BookCreate(self, stream):
     request = await stream.recv_message()
     name = request.name
     author_id = request.author_id
     year = request.year
     pk = await create_book(name, author_id, year)
     response = Book(id=pk, name=name, author_id=author_id, year=year)
     await stream.send_message(response)
Ejemplo n.º 4
0
 async def BookUpdate(self, stream):
     request = await stream.recv_message()
     book_id = request.id
     name = request.name
     author_id = request.author_id
     year = request.year
     book_id = await update_book(book_id, name, author_id, year)
     await stream.send_message(
         Book(id=book_id, name=name, author_id=author_id, year=year))
Ejemplo n.º 5
0
 async def BookDetail(self, stream):
     request = await stream.recv_message()
     book_id = request.id
     get_book = await detail_book(book_id)
     name = get_book.name
     year = get_book.year
     author_id = await Author.get(get_book.author_id)
     pk = author_id.id
     response = Book(id=book_id, name=name, author_id=pk, year=year)
     await stream.send_message(response)
Ejemplo n.º 6
0
 async def post(self, request):
     data = await request.json()
     channel = request.scope['channel']
     book = BookServiceStub(channel)
     name = data["name"]
     author_id = data["author_id"]
     year = data["year"]
     reply = await book.BookCreate(
         Book(name=name, year=year, author_id=author_id))
     json_data = convert_to_json(reply)
     return JSONResponse(json_data)
Ejemplo n.º 7
0
    def to_raw_book(tup):
        book = PBBook()
        book.title = tup[0]
        book.author = tup[1]
        book.sales = tup[2]
        book.is_published = tup[3]

        book.languages.extend(tup[4])

        for review_data in tup[5]:
            review = book.reviews.add()
            review.author = review_data[0]
            review.comment = review_data[1]

        book.price = tup[6]
        return book.SerializeToString()