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

Add new "quickstart" samples. #174

Merged
merged 22 commits into from
Oct 10, 2016
Merged

Add new "quickstart" samples. #174

merged 22 commits into from
Oct 10, 2016

Conversation

jmdobry
Copy link
Member

@jmdobry jmdobry commented Sep 29, 2016

These are intended to be simple examples that explicitly show:

  • Importing the client library
  • Instantiating a client
  • Making a single API call
  • Printing the result

Product covered:

  • BigQuery
    • - sample
    • - test
  • Datastore
    • - sample
    • - test
  • Language
    • - sample
    • - test
  • Logging
    • - sample
    • - test
  • Pub/Sub
    • - sample
    • - test
  • Speech
    • - sample
    • - test
  • Storage
    • - sample
    • - test
  • Translate
    • - sample
    • - test
  • Vision
    • - sample
    • - test

For reference, see the corresponding samples in other languages:

Thanks!

@jmdobry jmdobry self-assigned this Sep 29, 2016
@jmdobry
Copy link
Member Author

jmdobry commented Sep 29, 2016

How do you want these quickstart.php files organized? I've just been putting them into their own quickstart folder within each product folder, e.g.:

bigquery/
  quickstart/
    composer.json
    composer.lock
    quickstart.php

$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$bigqueryClient = new BigQueryClient([
Copy link
Contributor

@tmatsuo tmatsuo Sep 29, 2016

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

Copy link
Contributor

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);
Copy link
Contributor

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([
Copy link
Contributor

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.

@bshaffer
Copy link
Contributor

@tmatsuo PTAL

@bshaffer bshaffer changed the title [DO NOT MERGE] Add "quickstart" samples. Add "quickstart" samples. Sep 30, 2016
// Make sure it looks correct
$this->assertInstanceOf('Google\Cloud\BigQuery\Dataset', $dataset);
$this->assertEquals($datasetId, $dataset->id());
$dataset->delete();
Copy link
Contributor

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);
Copy link
Contributor

@tmatsuo tmatsuo Sep 30, 2016

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.

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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;
Copy link
Contributor

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

Copy link
Contributor

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(
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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();
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

@bshaffer bshaffer Sep 30, 2016

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
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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__'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor

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!

@jmdobry
Copy link
Member Author

jmdobry commented Sep 30, 2016

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.

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.

@jmdobry
Copy link
Member Author

jmdobry commented Sep 30, 2016

Maybe "quickstart" was the wrong label for these samples?

@tmatsuo
Copy link
Contributor

tmatsuo commented Sep 30, 2016

Yeah technically it's still testable with the replace hack...

What's the rationale of having projectIds?

@jmdobry
Copy link
Member Author

jmdobry commented Sep 30, 2016

What's the rationale of having projectIds?

@omaray?

@bshaffer
Copy link
Contributor

My understanding is we would show both with and without the project ID being set, e.g. in the docs:

To instantiate the [PRODUCT] client:

<code>

If you need to explicitly set the project ID, you can pass it in like this:

<code>

If we do it this way (which I prefer), we can create another quickstart sample for each one create_client_with_project_id.php or something.

@jmdobry
Copy link
Member Author

jmdobry commented Sep 30, 2016

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();
Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added

@bshaffer
Copy link
Contributor

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.

@bshaffer
Copy link
Contributor

As for using $bigqueryClient vs $bigquery, this is something we also want to be consistent with our other samples. I can't speak to the other languages, but I think it's more important to be consistent within the language docs than across languages.

@jmdobry jmdobry changed the title Add "quickstart" samples. Add new "quickstart" samples. Oct 5, 2016
]);

# The name of the image file to annotate
$fileName = '/resources/wakeupcat.jpg';
Copy link
Contributor

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.

Copy link
Member Author

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';
Copy link
Contributor

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.

Copy link
Member Author

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'];
Copy link
Contributor

@bshaffer bshaffer Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need spaces between concatenating .

Copy link
Member Author

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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"buymilktask"?

Copy link
Member Author

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'];
Copy link
Contributor

@bshaffer bshaffer Oct 6, 2016

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

Copy link
Member Author

@jmdobry jmdobry Oct 6, 2016

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

Copy link
Contributor

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

@bshaffer
Copy link
Contributor

bshaffer commented Oct 7, 2016

I believe this guy is ready for a final look!

@jmdobry
Copy link
Member Author

jmdobry commented Oct 7, 2016

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 GCLOUD_PROJECT environment variable for these samples to work. The docs will expect that all the user needs to do is gcloud beta auth application-default login. Is much work to make them all use an explicit project ID until the library supports auto-detecting the project ID?

@jmdobry
Copy link
Member Author

jmdobry commented Oct 8, 2016

Are composer.lock supposed to be committed to version control?

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?

@bshaffer
Copy link
Contributor

@jmdobry

composer.lock is committed to projects but not to dependencies. So this library should contain composer.lock (so the dependencies don't change w/o an explicit update) whereas a dependent library like google-cloud-php should never contain composer.lock

The renames had to happen for the organization of having quickstart not clutter up another sample application. I.e. logging/* became logging/api/* so that the logging sample code is clearly separated.

@bshaffer
Copy link
Contributor

@tmatsuo PTAL

@bshaffer bshaffer merged commit 627f249 into master Oct 10, 2016
@bshaffer bshaffer deleted the quickstarts branch October 10, 2016 19:52
@jmdobry jmdobry restored the quickstarts branch October 10, 2016 20:38
@jmdobry
Copy link
Member Author

jmdobry commented Oct 10, 2016

Can we please keep the branch around until I can update the docs? We don't want them to 404.

@bshaffer
Copy link
Contributor

@jmdobry should just take a second to update these. I have the change in flight.

@bshaffer bshaffer deleted the quickstarts branch December 13, 2016 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants