Skip to content

Explore going the route of using Clients ? #861

Description

@jgeewax

After our last talk, there have been quite a few different ideas tossed around to make it clear and obvious which credentials and project IDs are in use during a particular API call, some of those have been....

  1. Changing the default values globally
  2. Making "Connections" a context manager (with connection: # do something)
  3. Creating the concept of a client

We do (1), are talking about doing (2), while the others tend to do (3) -- and comparing the code, I think (3) is the nicest.

gcloud-node:

vargcloud=require('gcloud');varproject1_storage=gcloud.storage({projectId: 'project1',keyFilename: '/path/to/key'});varproject2_storage_auto=gcloud.storage();
# Dothingswiththetwo"clients"...bucket1=project1_storage.get_bucket('bucket-1')bucket2=project2_storage_auto.get_bucket('bucket2')

gcloud-ruby

require'gcloud/storage'project1_storage=Gcloud.storage"project-id-1""/path/to/key"project2_storage_auto=Gcloud.storage# Magically figure out the project ID and credentials# Do things with the two "clients"...bucket1=project1_storage.find_bucket"bucket-1"bucket2=project2_storage_auto.find_bucket"bucket-2"

gcloud-python

fromgcloudimportstoragefromgcloud.credentialsimportget_for_service_account_json# Create two different credentials.credentials1=get_for_service_account_json('key1.json')
credentials2=get_for_service_account_json('key2.json')
# Create two different connections.connection1=storage.Connection(credentials=credentials1)
connection2=storage.Connection(credentials=credentials2)
# Get two different bucketsbucket1=storage.get_bucket('bucket-1', project='project1', connection=connection1)
bucket2=storage.get_bucket('bucket-2', project='project2', connection=connection2)

gcloud-python if we followed the client pattern:

fromgcloudimportstorageproject1_storage=storage.Client('project1', '/path/to/key')
project2_storage_auto=storage.Client()
# Do things with the two "clients"...bucket1=project1_storage.get_bucket('bucket-1')
bucket2=project2_storage_auto.get_bucket('bucket-2')

Another option for gcloud-python using the client pattern:

importgcloudproject1_storage=gcloud.storage('project1', '/path/to/key')
project2_storage_auto=gcloud.storage()
# Do things with the two "clients"bucket1=project1_storage.get_bucket('bucket-1')
bucket2=project2_storage_auto.get_bucket('bucket-2')

/cc @dhermes@tseaver

Metadata

Metadata

Assignees

Labels

api: coreauthtype: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions