Skip to main content

Automatic protorpc message types for ndb.Model subclasses (Google App Engine only)

Project description

automessage is a library that helps you quickly create protorpc-based web services that interact with ndb models (Google Cloud Datastore) by automatically generating Message classes for your ndb.Model subclasses, along with easy serialization/deserialization.

Caveats

  • This is pretty rough, alpha-level code. There are lots of TODOs. Use at your own risk.
  • This only works for Google App Engine standard environment + Python with the protorpc and ndb libraries.

Installation

Follow this guide to install automessage as a third-party library for your App Engine Python app; the pip command you want is:

pip install -t lib/ automessage

Usage

First, use a @automessage.attach decorator on your ndb.Model subclass:

from google.appengine.ext import ndb
import automessage

@automessage.attach()
class Book(ndb.Model):
  title = ndb.StringProperty()
  author = ndb.StringProperty()
  publish_date = ndb.DateTimeProperty(indexed=True)

This generates a class BookMessage (a subclass of protorpc.messages.Message) in the same module as the Book class, that you can then use in your protorpc-based services, like so:

class BooksService(remote.Service):
  class FindRequest(messages.Message):
    title = messages.StringField(1, required=True)

  @remote.method(FindRequest, BookMessage)
  def find(self, request):
    return (Book
        .query(Book.title == request.title)
        .to_message()) # to_message() added by automessage

  @remote.method(BookMessage, BookMessage)
  def create(self, request):
    book = Book.from_message(request) # from_message() added by automessage
    book.put()
    return book.to_message()

attach takes several parameters (see the code for details) that lets you customize the name of the generated message class, convert to camel case, add an ID field, blacklist/whitelist properties, etc. You can decorate models with multiple attach calls (with different parameters) to create multiple message types for a given model. When doing so, you'll need to provide the message type in to_message calls, e.g. book.to_message(CustomBookMessage).

Related work

  • Protopigeon is an almost identical, older approach with a slightly different API and better testing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

automessage-0.1.5.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file automessage-0.1.5.tar.gz.

File metadata

  • Download URL: automessage-0.1.5.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/2.7.16

File hashes

Hashes for automessage-0.1.5.tar.gz
Algorithm Hash digest
SHA256 209b250315c7f2b8a3a91c16860e074e14c2cf253bb4666308c43a0bcb70b9ad
MD5 b8fd6f60c7381fbd705086a8d7e8a7c9
BLAKE2b-256 ed3a0b8e7e8aee8e1de0efddc42df3779d680ba9489a8e74661ca7da6d02e999

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page