Skip to content

feat: add namespace property to context.Context#388

Merged
chrisrossi merged 1 commit intogoogleapis:masterfrom
chrisrossi:feat-385
Apr 15, 2020
Merged

feat: add namespace property to context.Context#388
chrisrossi merged 1 commit intogoogleapis:masterfrom
chrisrossi:feat-385

Conversation

@chrisrossi
Copy link
Contributor

It is now possible to set the namespace on the context, overriding the
namespace set on the client. This makes it easier to write multi-homed
applications which might access different namespaces depending on which
user is logged in or other variable only known at request time.

client.Client.context has a new argument, namespace, which can be
used to set the namespace at request time. Here is an example of you
might do this in a WSGI middleware:

def ndb_wsgi_middleware(wsgi_app):
    client = ndb.Client()

    def middleware(environ, start_response):
        global_cache = ndb.RedisCache.from_environment()
        namespace = get_namespace_from_request(environ)
        with client.context(global_cache=global_cache, namespace=namespace):
            return wsgi_app(environ, start_response)

    return middleware

app = flask.Flask("NDB Example")
app.wsgi_app = ndb_wsgi_middleware(app.wsgi_app)  # Wrap the app in middleware.

Closes #385.

It is now possible to set the namespace on the context, overriding the
namespace set on the client. This makes it easier to write multi-homed
applications which might access different namespaces depending on which
user is logged in or other variable only known at request time.

`client.Client.context` has a new argument, `namespace`, which can be
used to set the namespace at request time. Here is an example of you
might do this in a WSGI middleware:

~~~~
def ndb_wsgi_middleware(wsgi_app):
    client = ndb.Client()

    def middleware(environ, start_response):
        global_cache = ndb.RedisCache.from_environment()
        namespace = get_namespace_from_request(environ)
        with client.context(global_cache=global_cache, namespace=namespace):
            return wsgi_app(environ, start_response)

    return middleware

app = flask.Flask("NDB Example")
app.wsgi_app = ndb_wsgi_middleware(app.wsgi_app)  # Wrap the app in middleware.
~~~~

Closes googleapis#385.
@chrisrossi chrisrossi requested a review from cguardia April 14, 2020 19:03
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 14, 2020
Copy link
Contributor

@cguardia cguardia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks!

@chrisrossi chrisrossi merged commit 34bac15 into googleapis:master Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

namespace property of context

3 participants