Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add namespace property to context.Context #388

Merged
merged 1 commit into from
Apr 15, 2020

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.
@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