Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 367075750
  • Loading branch information
Shopping Developer Relations authored and Brothman committed Feb 10, 2023
1 parent 70d0d51 commit b3733c6
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b3733c6

Please sign in to comment.