Title: How do I write code that talks to two different datasets with two different sets of credentials? · Issue #659 · googleapis/google-cloud-python · GitHub
Open Graph Title: How do I write code that talks to two different datasets with two different sets of credentials? · Issue #659 · googleapis/google-cloud-python
X Title: How do I write code that talks to two different datasets with two different sets of credentials? · Issue #659 · googleapis/google-cloud-python
Description: Let's say I have: /creds1.json /creds2.json dataset1 dataset2 gcloud.datastore How do I pull down Person:1 from dataset1, retrieve the 'name' property, and write it back to Log:2 in dataset2? Here's my best guess so far: from gcloud impo...
Open Graph Description: Let's say I have: /creds1.json /creds2.json dataset1 dataset2 gcloud.datastore How do I pull down Person:1 from dataset1, retrieve the 'name' property, and write it back to Log:2 in dataset2? Here'...
X Description: Let's say I have: /creds1.json /creds2.json dataset1 dataset2 gcloud.datastore How do I pull down Person:1 from dataset1, retrieve the 'name' property, and write it back to Log:2 in dat...
Opengraph URL: https://github.com/googleapis/google-cloud-python/issues/659
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How do I write code that talks to two different datasets with two different sets of credentials?","articleBody":"Let's say I have:\n- `/creds1.json`\n- `/creds2.json`\n- `dataset1`\n- `dataset2`\n- `gcloud.datastore`\n\nHow do I pull down `Person:1` from `dataset1`, retrieve the `'name'` property, and write it back to `Log:2` in `dataset2`?\n\nHere's my best guess so far:\n\n``` python\nfrom gcloud import credentials\nfrom gcloud import datastore\n\ncreds1 = credentials.get_for_service_account_json('/creds1.json')\ncreds2 = credentials.get_for_service_account_json('/creds2.json')\n\nconnection1 = datastore.Connection(credentials=creds1)\nconnection2 = datastore.Connection(credentials=creds1)\n\nperson1_key = datastore.Key('Person', 1, dataset_id='dataset1')\nlog2_key = datastore.Key('Log', 2, dataset_id='dataset2')\n\nperson1 = datastore.get(datastore.Key(person1_key), connection=connection1, dataset_id='dataset1')\nlog2 = datastore.get(datastore.Key(log2_key), connection=connection2, dataset_id='dataset2')\n\nlog2['data'] = person1['name']\ndatastore.put(log2, connection=connection2, dataset_id='dataset2')\n```\n\nI only got that by digging through tons of code. It made me sad.\n\nWhat I want to write:\n\n``` python\nfrom gcloud.datastore import get_connection\n\ndataset1 = get_connection(credentials_json='/creds1.json').get_dataset('dataset1')\ndataset2 = get_connection(credentials_json='/creds2.json').get_dataset('dataset2')\n\nperson1 = dataset1.get('Person', 1)\nlog2 = dataset2.get('Log', 2)\n\nlog2['data'] = person1['name']\nlog2.put()\n```\n\nIt seems that somewhere along the way, we lost the \"hierarchy\" of high-level concepts (Datastore -\u003e Connection -\u003e Dataset -\u003e Entity) so that things don't seem to know who their \"parent\" is in the tree on the way up.\n\nThis means things like `dataset.get()` because it needs to be provided it's connection and credentials. It seems we've tried to overcome this by storing defaults, but that blows up when you have more than one set of credentials...\n\nMaybe I'm totally misunderstanding?\n\n---\n\nI'm thinking that it'd be cool if we could allow three things:\n1. datastore.\u003cmethod\u003e that accepts _all_ the parameters to be absurdly specific (here is the connection, here is the dataset_it, etc)\n2. datastore.\u003cmethod\u003e that has lots of `None`s as default parameters, and we \"go get the default\" if you left it as `None` (ie, `connection=None -\u003e get_default_connection()`)\n3. Datastore -\u003e Connection -\u003e Dataset -\u003e Entity drill down that \"pre-fills\" these things going up the chain. That is, I can say:\n\n``` python\nconnection = datastore.get_connection(...)\ndataset = connection.get_dataset(...)\nentity = dataset.get(...)\n```\n\nThis means that the `datastore` module, `Connection`, and `Dataset` all would likely have the same methods, just with fewer things you can specify because some of those fieldsare specified when you \"ask for\" the next level down (ie, a Dataset knows it's `dataset_id`, so you don't have an option to provide that) .\n\n---\n### Example\n#### datastore.py:\n\n``` python\ndef get(key, connection=None, dataset_id=None):\n connection = connection or get_default_connection()\n dataset_id = dataset_id or get_default_dataset_id()\n\n# Means I can do: datastore.get(Key('Person', 1), dataset_id='dataset1')\n```\n#### dataset.py:\n\n``` python\nclass Dataset(object):\n\n def __init__(self, dataset_id, connection=None):\n self.dataset_id = dataset_id\n self.connection = connection\n\n def get(self, key):\n return datastore.get(key, dataset_id=self.dataset_id, connection=self.connection)\n```\n\n/cc @pcostell @dhermes @tseaver \n","author":{"url":"https://github.com/jgeewax","@type":"Person","name":"jgeewax"},"datePublished":"2015-02-17T16:52:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/659/google-cloud-python/issues/659"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:251a4260-acff-8664-6791-824b4833bc0d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 932C:2C9E7F:332B8D:450B9F:6A4D2D53 |
| html-safe-nonce | d0748f17d449395c4a25297781995f3d008106fdee9baa756635e0840735e252 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzJDOjJDOUU3RjozMzJCOEQ6NDUwQjlGOjZBNEQyRDUzIiwidmlzaXRvcl9pZCI6Ijc4MTIwNjQ4MTE4Nzc3Mzk4NjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2712c9800956281aed359a60d1e4496dbadff4ecc6176d77893eb509aa401803 |
| hovercard-subject-tag | issue:57949452 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-cloud-python/659/issue_layout |
| twitter:image | https://opengraph.githubassets.com/89b9d667e6d5a762103cbd451bc245135725173f435664104fec203be262aeba/googleapis/google-cloud-python/issues/659 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/89b9d667e6d5a762103cbd451bc245135725173f435664104fec203be262aeba/googleapis/google-cloud-python/issues/659 |
| og:image:alt | Let's say I have: /creds1.json /creds2.json dataset1 dataset2 gcloud.datastore How do I pull down Person:1 from dataset1, retrieve the 'name' property, and write it back to Log:2 in dataset2? Here'... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jgeewax |
| hostname | github.com |
| expected-hostname | github.com |
| None | 92571a8944142227b7e19cd10918b1ddd06e5066c1ad5bc7e4769cf6140a87e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/google-cloud-python git https://github.com/googleapis/google-cloud-python.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 16316451 |
| octolytics-dimension-repository_nwo | googleapis/google-cloud-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 16316451 |
| octolytics-dimension-repository_network_root_nwo | googleapis/google-cloud-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 93f17a978ee60bc4668e1d7b90e6bd2d622261fd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width