From b3733c6a39066107c2d1f97bdf286f037bde8a49 Mon Sep 17 00:00:00 2001 From: Shopping Developer Relations Date: Tue, 6 Apr 2021 20:38:08 +0000 Subject: [PATCH] Internal change PiperOrigin-RevId: 367075750 --- .../samples/accounts/AccountInsertSample.java | 2 +- .../v2_1/samples/accounts/AccountUtils.java | 20 ++++ .../samples/accounts/AccountWorkflow.java | 3 +- .../samples/accounts/AccountsListSample.java | 2 +- .../liasettings/LiaAccountWorkflow.java | 104 ++++++++++++++++++ .../v2_1/samples/liasettings/LiaConfig.java | 20 ++++ 6 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaAccountWorkflow.java diff --git a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountInsertSample.java b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountInsertSample.java index 9359bf1..fbc98e1 100644 --- a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountInsertSample.java +++ b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountInsertSample.java @@ -19,7 +19,7 @@ public void execute() throws IOException { checkMCA(); try { - Account account = new Account().setName(AccountUtils.SAMPLE_ACCOUNT_NAME); + Account account = AccountUtils.getDefaultAccount(); System.out.println("Inserting new account."); diff --git a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountUtils.java b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountUtils.java index 7238607..2c212de 100644 --- a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountUtils.java +++ b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountUtils.java @@ -1,13 +1,33 @@ package shopping.content.v2_1.samples.accounts; +import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.content.model.Account; import com.google.api.services.content.model.AccountAdsLink; import com.google.api.services.content.model.AccountUser; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; /** Utility class for storing actions performed on Accounts. */ public class AccountUtils { public static final String SAMPLE_ACCOUNT_NAME = "sampleAccount123"; + /** + * Returns a defaultly constructed {@code Account} object with {@code SAMPLE_ACCOUNT_NAME} as + * account name + */ + public static Account getDefaultAccount() { + return new Account().setName(SAMPLE_ACCOUNT_NAME); + } + + public static Account loadAccountFromJson(String path) throws IOException { + try (InputStream inputStream = new FileInputStream(path)) { + return new JacksonFactory().fromInputStream(inputStream, Account.class); + } catch (IOException e) { + throw new IOException("Could not find or read the account config file at " + path + "."); + } + } + public static void printAccount(Account account) { System.out.printf("Information for account %d:%n", account.getId()); if (account.getName() == null) { diff --git a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountWorkflow.java b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountWorkflow.java index b37ed4a..3b9e2bb 100644 --- a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountWorkflow.java +++ b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountWorkflow.java @@ -42,8 +42,7 @@ public void execute() throws IOException { AccountsListSample.listAccountsForMerchant(config.getMerchantId(), content); - Account subaccount = new Account(); - subaccount.setName(AccountUtils.SAMPLE_ACCOUNT_NAME); + Account subaccount = AccountUtils.getDefaultAccount(); System.out.println("Creating new sub-account."); Account result = content.accounts().insert(config.getMerchantId(), subaccount).execute(); diff --git a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountsListSample.java b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountsListSample.java index 22416e5..2406459 100644 --- a/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountsListSample.java +++ b/java/src/main/java/shopping/content/v2_1/samples/accounts/AccountsListSample.java @@ -15,7 +15,7 @@ public AccountsListSample(String[] args) throws IOException { super(args); } - static void listAccountsForMerchant(BigInteger merchantId, ShoppingContent content) + public static void listAccountsForMerchant(BigInteger merchantId, ShoppingContent content) throws IOException { System.out.printf("Listing sub-accounts for Merchant Center %s:%n", merchantId); ShoppingContent.Accounts.List listAccounts = content.accounts().list(merchantId); diff --git a/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaAccountWorkflow.java b/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaAccountWorkflow.java new file mode 100644 index 0000000..29b46e1 --- /dev/null +++ b/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaAccountWorkflow.java @@ -0,0 +1,104 @@ +package shopping.content.v2_1.samples.liasettings; + +import static shopping.common.BaseOption.NO_CONFIG; + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.util.ExponentialBackOff; +import com.google.api.services.content.ShoppingContent; +import com.google.api.services.content.model.Account; +import java.io.File; +import java.io.IOException; +import java.math.BigInteger; +import org.apache.commons.cli.CommandLine; +import shopping.common.BaseOption; +import shopping.content.v2_1.samples.ContentWorkflowSample; +import shopping.content.v2_1.samples.accounts.AccountUtils; +import shopping.content.v2_1.samples.accounts.AccountsListSample; + +/** Sample that runs through an entire example workflow using the Accounts service. */ +public class LiaAccountWorkflow extends ContentWorkflowSample { + private LiaAccountWorkflow(ShoppingContent content, ShoppingContent sandbox, LiaConfig config) { + super(content, sandbox, config); + } + + public static void run(ShoppingContent content, ShoppingContent sandbox, LiaConfig config) + throws IOException { + new LiaAccountWorkflow(content, sandbox, config).execute(); + } + + public Account getOrCreateSubAccount() throws IOException { + LiaConfig liaConfig = (LiaConfig) config; + + System.out.println("Retrieving account information."); + Account account = + content.accounts().get(liaConfig.getMerchantId(), liaConfig.getMerchantId()).execute(); + AccountUtils.printAccount(account); + + if (!liaConfig.getIsMCA()) { + if (liaConfig.getCreateSubAccount()) { + throw new IllegalArgumentException( + "createSubAccount is true but given merchantId is not a MCA"); + } + return account; + } + + AccountsListSample.listAccountsForMerchant(liaConfig.getMerchantId(), content); + + String subAccountConfigPath = liaConfig.getSubAccountConfigPath(); + Account subAccount; + if (subAccountConfigPath == null || subAccountConfigPath.isEmpty()) { + subAccount = AccountUtils.getDefaultAccount(); + } else { + subAccount = AccountUtils.loadAccountFromJson(subAccountConfigPath); + } + + System.out.println("Creating new sub-account."); + subAccount = content.accounts().insert(liaConfig.getMerchantId(), subAccount).execute(); + AccountUtils.printAccount(subAccount); + + BigInteger accountId = subAccount.getId(); + System.out.printf("Retrieving new sub-account %s.%n", accountId); + // Newly created accounts may not be immediately accessible, so retry until available + // or until our back off strategy runs out. + ExponentialBackOff backOff = + new ExponentialBackOff.Builder() + .setInitialIntervalMillis(5000) + .setMaxIntervalMillis(30000) + .build(); + subAccount = + retryFailures(content.accounts().get(liaConfig.getMerchantId(), accountId), backOff); + AccountUtils.printAccount(subAccount); + + return subAccount; + } + + @Override + public void execute() throws IOException { + System.out.println("---------------------------------"); + System.out.println("Running LIA Account workflow:"); + System.out.println(); + + getOrCreateSubAccount(); + } + + public static void main(String[] args) throws IOException { + CommandLine parsedArgs = BaseOption.parseOptions(args); + File configPath = null; + if (!NO_CONFIG.isSet(parsedArgs)) { + configPath = BaseOption.checkedConfigPath(parsedArgs); + } + configPath = BaseOption.checkedConfigPath(parsedArgs); + LiaConfig config = LiaConfig.load(configPath); + + ShoppingContent.Builder builder = createStandardBuilder(parsedArgs, config); + ShoppingContent content = createService(builder); + ShoppingContent sandbox = createSandboxContentService(builder); + retrieveConfiguration(content, config); + + try { + new LiaAccountWorkflow(content, sandbox, config).execute(); + } catch (GoogleJsonResponseException e) { + checkGoogleJsonResponseException(e); + } + } +} diff --git a/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaConfig.java b/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaConfig.java index 7f0cdf4..8940986 100644 --- a/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaConfig.java +++ b/java/src/main/java/shopping/content/v2_1/samples/liasettings/LiaConfig.java @@ -16,6 +16,10 @@ public class LiaConfig extends ContentConfig { private static final String CONTENT_DIR = "content"; private static final String FILE_NAME = "merchant-info.json"; + @Key private boolean createSubAccount; + + @Key private String subAccountConfigPath; + @Key private String gmbEmail; public static LiaConfig load(File basePath) throws IOException { @@ -45,6 +49,22 @@ public static LiaConfig load(File basePath) throws IOException { } } + public boolean getCreateSubAccount() { + return createSubAccount; + } + + public void setCreateSubAccount(boolean createSubAccount) { + this.createSubAccount = createSubAccount; + } + + public String getSubAccountConfigPath() { + return subAccountConfigPath; + } + + public void setSubAccountConfigPath(String subAccountConfigPath) { + this.subAccountConfigPath = subAccountConfigPath; + } + public String getGmbEmail() { return gmbEmail; }