Retail API - Python Client Lib Sample - anyone

89 views
Skip to first unread message

C G

unread,
Mar 29, 2021, 11:47:43 AM3/29/21
to cloud-recommendations-users
Hi all,
I am struggling with the Python client library: is anyone able to share a short sample snippet, e.g. create a product via Retail AI? 
Thanks in advance,
christian

elarson

unread,
Apr 2, 2021, 9:04:51 PM4/2/21
to cloud-recommendations-users
There are some basic examples if you view the pydocs, for example:
pydoc3 google.cloud.retail.ProductServiceClient

Here's a very barebones example using the import method.  create_product would be similar.  Ping me if you have any trouble.


from google.cloud import retail
from google.oauth2 import service_account

SERVICE_ACCOUNT_FILE = 'yourkeyfile.json'
PROJECT_NUM = "####"

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)

client = retail.ProductServiceClient(credentials=credentials)

products = {
  "products": [
    {
      "id": "123",
      "title": "Steamed Hams",
      "description": "Bulk Lot of Steamed Hams",
      "categories": ['Food','Steamed & Canned'],
    }
  ]
}

inputConfig = {"product_inline_source": products}
parent = 'projects/' + PROJECT_NUM + '/locations/global/catalogs/default_catalog/branches/0'

response = client.import_products(parent,inputConfig)


C G

unread,
Apr 7, 2021, 10:23:46 AM4/7/21
to cloud-recommendations-users
Hi, thanks. That was helpful, product import works.

I tried to import user events in a similar manner, but I am having problems - again - in creating the user event import request. I know this is not directly related to Retail API, but I cannot find any documentation or samples on how to construct the request objects in the Python client libs accordingly:

Here's my (erroneous) snippet:

user_events = {
    "user_events": [
        {
            "event_type": "home-page-view",
            "visitor_id": "visitor-1",
            "event_time": "XXX"  # problem 
        }
    ]
}
import_user_events_request = {
    "parent": 'projects/' + PROJECT_ID + '/locations/global/catalogs/default_catalog/userEvents:import',
    "input_config": {"user_event_inline_source": user_events}
}
response = user_event_service.import_user_events(import_user_events_request)


Problem seems to be the 'event_time' field - supposed to be a (protobuf) timestamp. Can't find any docs on how to create such a timestamp. 

Again, thanks in advance
christian


elarson

unread,
Apr 7, 2021, 11:34:52 PM4/7/21
to cloud-recommendations-users
You can use  google.protobuf.timestamp_pb2.Timestamp:
 
import datetime
from google.protobuf.timestamp_pb2 import Timestamp
...
timestamp = Timestamp()
timestamp.FromDatetime(dt=datetime.datetime.now())
...

Also, your parent string should be something like:
parent = 'projects/' + PROJECT_ID + '/locations/global/catalogs/default_catalog'

C G

unread,
Apr 8, 2021, 7:13:19 AM4/8/21
to cloud-recommendations-users
Thanks again. I applied you suggestions, I still receive an 'invalid argument' error.

Here is my current snippet:

timestamp = Timestamp()
timestamp.FromDatetime(dt=datetime.datetime.now())
user_events = {
    "user_events": [
        {
            "event_type": "home-page-view",
            "visitor_id": "visitor-1",
            "event_time": timestamp
        }
    ]
}
import_user_events_request = {

    "parent": 'projects/' + PROJECT_ID + '/locations/global/catalogs/default_catalog/branches/0',
    "input_config": {"user_event_inline_source": user_events}
}
response = user_event_service.import_user_events(import_user_events_request)

What am I missing? Is there a way to get more verbose error messages (ie which argument is invalid, which fields are missing)?
Sorry for bothering again, thanks in advance. Any hint appreciated!
christian


Eric Larson

unread,
Apr 8, 2021, 11:05:32 AM4/8/21
to C G, cloud-recommendations-users
Your parent path is still incorrect, the catalog does not have a branch only products have branches specified.


--
You received this message because you are subscribed to a topic in the Google Groups "cloud-recommendations-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cloud-recommendations-users/H_7dzID2HkY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cloud-recommendatio...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-recommendations-users/f92dc08d-c9ff-4b85-ae4c-e228134b9f97n%40googlegroups.com.

elarson

unread,
Apr 8, 2021, 11:49:22 AM4/8/21
to cloud-recommendations-users
I meant *events* do not have branches, so for event import I believe you should have this as the parent:
parent = 'projects/' + PROJECT_ID + '/locations/global/catalogs/default_catalog'

Reply all
Reply to author
Forward
0 new messages