Google Cloud organizes resources into projects. This lets you collect all the related resources for a single application in one place.
If you don't have a Google Cloud project yet or you're not the owner of an existing one, you can create a new project.
After the project is created, set your PROJECT_ID to a project
variable.
-
Run the following command in Terminal:
gcloud config set project <YOUR_PROJECT_ID>
-
Check that the Retail API is enabled for your Project in the Admin Console.
To run a code sample from the Cloud Shell, you need to authenticate. To do this, use the Application Default Credentials.
-
Set your user credentials to authenticate your requests to the Retail API
gcloud auth application-default login
-
Type
Y
and press Enter. Click the link in Terminal. A browser window should appear asking you to log in using your Gmail account. -
Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal.
-
Run the code sample and check the Retail API in action.
Note: Click the copy button on the side of the code box to paste the command in the Cloud Shell terminal and run it.
Because you are going to run the code samples in your own Google Cloud project, you should specify the project_number as an environment variable. It will be used in every request to the Retail API.
-
You can find the
project_number
in the Project Info card displayed on Home/Dashboard. -
Set the environment variable with the following command:
export PROJECT_NUMBER=<YOUR_PROJECT_NUMBER>
To run Python code samples for the Retail API tutorial, you need to set up your virtual environment.
- Run the following commands in a Terminal to create an isolated Python environment:
pip install virtualenv virtualenv myenv source myenv/bin/activate
- Next, install Google packages:
pip install google pip install google-cloud-retail pip install google.cloud.storage pip install google.cloud.bigquery
This step is required if this is the first Retail API Tutorial you run. Otherwise, you can skip it.
Clone the Git repository with all the code samples to learn the Retail features and check them in action.
-
Run the following command in the Terminal:
git clone https://github.com/t-karasova/grs-samples-python.git
The code samples for each of the Retail services are stored in different directories.
-
Go to the
grs-samples-python
directory. It's our starting point to run more commands.cd grs-samples-python
This step is required if this is the first Retail API Tutorial you run. Otherwise, you can skip it.
There is a JSON file with valid products prepared in the product
directory:
product/resources/products.json
.
Another file, product/resources/products_some_invalid.json
, contains both valid and invalid products, and you will use it to check the error handling.
In your own project, create a Cloud Storage bucket and put the JSON file there.
The bucket name must be unique. For convenience, you can name it <YOUR_PROJECT_ID>_<TIMESTAMP>
.
-
To create the bucket and upload the JSON file, run the following command in the Terminal:
python product/setup/create_gcs_bucket.py
Now you can see the bucket is created in the Cloud Storage, and the files are uploaded.
-
The name of the created Retail Search bucket is printed in the Terminal. Copy the name and set it as the environment variable
BUCKET_NAME
:export BUCKET_NAME=<YOUR_BUCKET_NAME>
To import the prepared products to a catalog, run the following command in the Terminal:
python product/import_products_gcs.py
Run the sample in a terminal with the following command:
python search/search_with_boost_spec.py