GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DISCUSSION: Reduce amount of implicit magic · Issue #847 · googleapis/google-cloud-python

DISCUSSION: Reduce amount of implicit magic #847

New issue
Copy link
New issue
Copy link
Closed
Closed
DISCUSSION: Reduce amount of implicit magic#847
Copy link
Assignees
dhermes
Labels
api: core

Description

@dhermes
dhermes
opened on Apr 29, 2015
Issue body actions

See https://github.com/GoogleCloudPlatform/gcloud-python/pull/830/files#r29357515

Activity

  1. dhermes
    added
    api: core
    on Apr 29, 2015
  2. dhermes
    added this to the This milestone has been deleted milestone on Apr 29, 2015
  3. jgeewax commented on Apr 29, 2015

    @jgeewax
    jgeewax
    on Apr 29, 2015
    Contributor
    More actions

    IMO, the goal here should be to support the implicit magic if users want it (and hell, for a lot of people it's what they have come to expect from App Engine), however support the explicit side of things as well. Ultimately it should be easy to swap out credentials for the cases where we want them.

    Disclaimer: This conversation may very well bring us in a full circle... and I apologize if that happens.


    Some thoughts

    1. <module>.<method>() calls (ie, datastore.get(...)) all would have a connection or client parameter.
    2. If connection/client is None, we try to resolve it -- potentially using magic.
    3. Setting a default client should have global effect (in other words, no per-module default client).
    4. If you don't want a global default client, you should be able to easily create a client and then use it like you would use the module itself (ie pubsub_client.create_topic('topic_name')) (I'd expect this to be 'the recommended way' of doing things...)
    5. A Client object would represent a combination of Credentials, a Project ID, and a transport, so a globally shared default client implies a globally shared default project ID as well as credentials.
    6. Clients should have classmethod factories for loading credentials easily (ie, from_environment_variable, from_path, from_p12)

    Using an explicitly built client object (not the module)

    You'd use this whenever you use more than one set of credentials and one project ID (whether they're on the same service or not). The client object would just pass itself on as the argument and act as an intermediary:

    class PubsubClient(Client):
        def create_topic(self, *args, **kwargs):
            pubsub.create_topic(connection=self.connection, *args, **kwargs)

    Auto detect project ID and credentials (aka, don't think about it)

    from gcloud import pubsub
    
    pubsub_client = pubsub.Client()
    pubsub_client.create_topic('topic_name')

    Auto detect the project ID but specify where to get credentials

    from gcloud import pubsub
    
    pubsub_client = pubsub.Client(
        credentials=gcloud.Credentials.from_environment_variable('...'))
    pubsub_client.create_topic('topic_name')

    Auto detect the credentials but specify the project ID

    from gcloud import pubsub
    
    pubsub_client = pubsub.Client(project='project_id')
    pubsub_client.create_topic('topic_name')

    Specify both the project ID and the credentials

    import gcloud
    from gcloud import pubsub
    
    pubsub_client = pubsub.Client(
        credentials=gcloud.Credentials.from_environment_variable('...'),
        project='project_id')
    pubsub_client.create_topic('topic_name')

    Using the module itself

    You'd use this only when you have a single set of credentials and a single project.

    Auto detect project ID and credentials (aka, don't think about it)

    from gcloud import pubsub
    pubsub.create_topic('topic_name')

    Auto detect the project ID but specify where to get credentials

    import gcloud
    from gcloud import pubsub
    
    gcloud.set_default_client(gcloud.Client(
        credentials=gcloud.Credentials.from_environment_variable('....')))
    pubsub.create_topic('topic_name')

    Auto detect the credentials but specify the project ID

    import gcloud
    from gcloud import pubsub
    
    gcloud.set_default_client(gcloud.Client(project='project_id'))
    pubsub.create_topic('topic_name')

    Specify both the project ID and the credentials

    import gcloud
    from gcloud import pubsub
    
    gcloud.set_default_client(gcloud.Client(
        credentials=gcloud.Credentials.from_environment_variable('....'),
        project='project_id'))
    pubsub.create_topic('topic_name')
  4. jgeewax commented on Apr 30, 2015

    @jgeewax
    jgeewax
    on Apr 30, 2015
    Contributor
    More actions

    After our conversation yesterday, it seems like if our goal is to simplify the use of different "connection" objects we might be able to do this cleanly using the same type of pattern as we do with Batches by making the Connection object a context manager. This would make code look like:

    connection = ...  # Get a Connection object somehow.
    connection2 = ...  # Get a Connection object somehow.
    
    with connection:
        pubsub.create_topic('topic_name')
    
    with connection 2:
        pubsub.create_topic('topic_name')

    We still need to address the problem of "If I want to swap out credentials, it should be easy and simple" -- in other words "Getting a connection object somehow." should be straight forward assuming the two things I want to tweak are the credentials and the project ID.

    More discussion on this soon...

  5. dhermes commented on Jun 18, 2015

    @dhermes
    dhermes
    on Jun 18, 2015
    ContributorAuthor
    More actions

    This is kind of a redundant issue at this point, given #816, etc.

  6. dhermes
    closed this as completedon Jun 18, 2015
  7. JustinBeckwith
    assigned
    dhermes
    on Feb 1, 2021
  8. parthea
    added a commit that references this issue on Aug 21, 2025

    chore(deps): update all dependencies (#847)

    1c27e0f
  9. parthea
    added 2 commits that reference this issue on Sep 16, 2025

    chore(deps): update all dependencies (#847)

    7d934f4

    chore: add bigframes team as co-owners for pandas-gbq (#847)

    dd833ba
  10. parthea
    added 7 commits that reference this issue on Nov 22, 2025

    chore: [autoapprove] Pin flake8 version (#847)

    22f0577

    chore: add SECURITY.md (#847)

    dcb0d7e

    chore(main): release 2.28.0 (#847)

    c95725b

    feat: Remove pyarrow and bqstorage as dependencies (#847)

    74d2f30

    build(deps): bump cryptography from 42.0.0 to 42.0.2 in .kokoro (#847)

    8f6fe00

    build(nox): Use Python 3.9 for docs and doctest sessions (#847)

    a62b387

    chore: release 2.0.2 (#847)

    3fe5fc1
  11. parthea
    added 2 commits that reference this issue on Mar 2, 2026

    chore(deps): update dependency pytest to v7.2.1 (#847)

    bec116b

    chore: bump cryptography from 41.0.5 to 41.0.6 in /synthtool/gcp/temp…

    50ddc89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

Labels

api: core

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.