-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add new "quickstart" samples. #174
Conversation
How do you want these
|
$projectId = 'YOUR_PROJECT_ID'; | ||
|
||
# Instantiates a client | ||
$bigqueryClient = new BigQueryClient([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The google-cloud-php can guess the projectId most of the cases.
I would omit the projectId, which makes the code more testable.
Same for the rest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The quickstarts should follow the same rules as the samples, and rely on the implicitly set project ID, rather than setting it explicitly, as this should be handled by the Client Libraries page.
$taskKey = $datastoreClient->key($kind, $id); | ||
|
||
# Retrieves the entity | ||
$dataset = $datastoreClient->lookup($taskKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$dataset
-> $entity
Considering it's the first example for users, I don't think lookup
is appropriate, because it will return null
for the first time users.
$projectId = 'YOUR_PROJECT_ID'; | ||
|
||
# Instantiates a client | ||
$datastoreClient = new DatastoreClient([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$datastoreClient
=> $datastore
for consistency with our other samples.
@tmatsuo PTAL |
// Make sure it looks correct | ||
$this->assertInstanceOf('Google\Cloud\BigQuery\Dataset', $dataset); | ||
$this->assertEquals($datasetId, $dataset->id()); | ||
$dataset->delete(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it more appropriate to call delete()
in tearDown
or something?
|
||
# Retrieves the entity | ||
$dataset = $datastoreClient->lookup($taskKey); | ||
$entity = $datastore->entity($taskKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't retrieve the entity, it just creates a local Entity
object. No API calls are made.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right. What do you think we should do for this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd create the key with named id and upsert
the entity.
But I think we should match with the other language samples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other language samples do what this sample does. But I agree this is not a good sample. @jmdobry thoughts on changing this to an upsert
?
$datastoreClient = new DatastoreClient([ | ||
'projectId' => $projectId | ||
]); | ||
$datastore = new DatastoreClient(); | ||
|
||
# The kind of the entity to retrieve | ||
$kind = 'Task'; | ||
# The id of the entity to retrieve | ||
$id = 1234567890; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit controversial to manually assign the numeric id (since you need to allocateId
to avoid the collision with auto assigned id).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we call "lookup" instead of "upsert" here, and maybe use a name instead of ID, would that be more appropriate?
$kind = 'Person';
$name = 'Bob';
$key = $datastore->key($kind, $name);
$entity = $datastore->lookup($key);
{ | ||
$file = sys_get_temp_dir() . '/datastore_quickstart.php'; | ||
$contents = file_get_contents(__DIR__ . '/../quickstart.php'); | ||
$contents = str_replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to replace anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__DIR__
needs replaced for the autoloading to work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the __DIR__
is the only thing you need to replace, you don't need to copy the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, good point
]); | ||
|
||
# List logging entries | ||
$entries = $logging->entries(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this will give you nothing, right? Is it appropriate for quickstart?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming there's at least something in the logs for their project, since this requests the logs without any filter. What do you suggest instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now fixed - the logging quickstart writes a logging entry
|
||
# Instantiates a client | ||
$logging = new LoggingClient([ | ||
'projectId' => $projectId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove $projectId here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding was Logging was the one API we wanted to pass Project ID in for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the CLI, we still need an explicit projectId, but for creating the LoggingClient and do some simple stuff, it can be omitted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to pass in a Project ID for the CLI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed Project ID from quickstart
$file = sys_get_temp_dir() . '/logging_quickstart.php'; | ||
$contents = file_get_contents(__DIR__ . '/../quickstart.php'); | ||
$contents = str_replace( | ||
['YOUR_PROJECT_ID', '__DIR__'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed!
$topicName = 'my-new-topic-' . time(); | ||
$file = sys_get_temp_dir() . '/pubsub_quickstart.php'; | ||
$contents = file_get_contents(__DIR__ . '/../quickstart.php'); | ||
$contents = str_replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one needs to copy the file so we can verify the new topic is created!
These samples are for the Client Libraries pages, therefore, can you bring back the explicit project IDs? These quickstart samples (which will only be shown on the Client Libraries pages) should be the only ones that use an explicit project ID. All other samples (as in, everything but the quickstart samples) should infer project ID. Thanks for writing the tests! Also, the various quickstart samples (Node.js, Ruby, Python) are nearly identical in terms of variable names, number of executable statement, comments in the code, etc., so if possible, it would be best to keep these PHP versions of those quickstart samples as close to the Node.js, Python, Ruby quickstart samples as possible. We have specific requests from UX/PM for these samples to look a certain way, use certain comments, etc. I think it's okay for the quickstart samples to be "quirky" (they're only for the Client Libraries pages), meaning they don't follow the exact same conventions as the rest of your samples. For example: Node.js // Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';
// Instantiates a client
const bigqueryClient = BigQuery({ projectId: projectId }); Python # Your Google Cloud Platform project ID
project_id = 'YOUR_PROJECT_ID'
# Instantiates a client
bigquery_client = BigQuery.Client(project=project_id) Ruby # Your Google Cloud Platform project ID
project_id = "YOUR_PROJECT_ID"
# Instantiates a client
gcloud = Google::Cloud new project_id
bigquery_client = gcloud.bigquery PHP # Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';
# Instantiates a client
$bigqueryClient = new BigQueryClient([
'projectId' => $projectId
]); Yes, they're harder to test with an explicit project ID, but there shouldn't be too many of these types of sample. |
Maybe "quickstart" was the wrong label for these samples? |
Yeah technically it's still testable with the replace hack... What's the rationale of having projectIds? |
|
My understanding is we would show both with and without the project ID being set, e.g. in the docs:
If we do it this way (which I prefer), we can create another quickstart sample for each one |
We, will just not on the same page. The quickstart samples, shown on Client Libraries pages, will use the explicit project ID. Everywhere else in the docs (the vast majority of samples) will use an implicit project ID. |
]); | ||
|
||
# List logging entries | ||
$entries = $logging->entries(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this sample match the other logging quickstart samples (here's the python one), i.e. log an entry as opposed to listing entries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, added
Hmm, seems strange to recommend doing it two different ways. The docs can cover explicitly setting the project ID, but they should explain when this would be done (when application default credentials span multiple projects) and why. |
As for using |
]); | ||
|
||
# The name of the image file to annotate | ||
$fileName = '/resources/wakeupcat.jpg'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add the __DIR__
here and not below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
]); | ||
|
||
# The name of the audio file to transcribe | ||
$fileName = '/resources/audio.raw'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add the __DIR__
here and not below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
# Detects speech in the audio file | ||
$results = $speech->recognize(fopen(__DIR__ . $fileName, 'r'), $options); | ||
|
||
echo 'Transcription: '.$results[0]['transcript']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need spaces between concatenating .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
$kind = 'Task'; | ||
|
||
# The name/ID of the entity to retrieve | ||
# The name/ID for the new entity | ||
$name = 'sampletask1'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"buymilktask"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, what would be the point of the description field in that case?
# Saves the entity | ||
$datastore->upsert($task); | ||
|
||
echo 'Saved ' . $task->key() . ': ' . $task['description']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This output will look like this:
Saved [ [Task: simpletask1] ]: Buy Milk
A little weird, but seems ok to me :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't for the life of me get it to just print simpletask1
(which is what I did in other languages).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha yeah, you would have to call $task->key()->path()[0]['name']
, which isn't worth it
I believe this guy is ready for a final look! |
LGTM except for one thing. The PHP Cloud Client library does not yet support auto-detecting the project ID, meaning that the user must set the |
Are Also, I'm now seeing a number of files in this PR with changes (or being renamed or something) that are unrelated to the quickstart samples, was that intentional? |
The renames had to happen for the organization of having |
@tmatsuo PTAL |
Can we please keep the branch around until I can update the docs? We don't want them to 404. |
@jmdobry should just take a second to update these. I have the change in flight. |
These are intended to be simple examples that explicitly show:
Product covered:
For reference, see the corresponding samples in other languages:
master
in variousquickstart.js
files)Thanks!