Skip to content

Instantly share code, notes, and snippets.

@gordonturner
Created May 13, 2021 11:37
Show Gist options
  • Save gordonturner/d0b0333403aaa1daaa128294e827005f to your computer and use it in GitHub Desktop.
Save gordonturner/d0b0333403aaa1daaa128294e827005f to your computer and use it in GitHub Desktop.
package com.gordonturner.dangerdigital.service;
import com.notronix.etsy.api.EtsyAPI;
import com.notronix.etsy.api.EtsyAPIException;
import com.notronix.etsy.api.authentication.Credentials;
import com.notronix.etsy.api.authentication.EtsyScope;
import com.notronix.etsy.api.model.ShopAssociations;
import com.notronix.etsy.api.model.UserAssociations;
import com.notronix.etsy.impl.EtsyDataService;
import com.notronix.etsy.impl.model.EtsyShop;
import com.notronix.etsy.impl.model.EtsyUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DefaultPropertiesPersister;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
@Service
public class EtsyService {
private static final Logger logger = LoggerFactory.getLogger( EtsyService.class );
private EtsyDataService etsyDataService;
private Credentials temporaryCredentials;
private Credentials accessCredentials;
private EtsyUser authorizedEtsyAccountUser;
@Value("${app.workingDirectory}")
private String workingDirectory;
private static final String accessCredentialsFileName = "access-credentials.properties";
public EtsyService(@Value("${etsy.api_key}") String apiKey,
@Value("${etsy.shared_secret}") String sharedSecret) {
etsyDataService = new EtsyDataService(apiKey, sharedSecret);
}
/**
* @return
*/
public boolean loginWithPreviousAccessCredentials() {
logger.info("Called loginWithPreviousAccessCredentials");
List<String> userIdsOrNames = Collections.singletonList(EtsyAPI.__SELF__);
// Attempt to load previous access credentials
accessCredentials = loadAccessCredentials();
// If there are no previous access credentials, return false
if(accessCredentials == null) {
return false;
}
try {
List<EtsyUser> users = etsyDataService.getUser(accessCredentials,
userIdsOrNames,
UserAssociations.values());
authorizedEtsyAccountUser = users.get(0);
return true;
} catch (EtsyAPIException e) {
e.printStackTrace();
return false;
}
}
/**
* This should return a list of all open orders.
*
* We want:
* - Date
* - Number
* - Buyer name
*
* @return
*/
public String loginOob() {
logger.info("Called loginOob");
Set<String> scopes = Arrays.stream(EtsyScope.values()).map(Enum::name).collect(toSet());
try {
temporaryCredentials = etsyDataService.getTemporaryCredentials(scopes, "oob");
} catch (EtsyAPIException e) {
e.printStackTrace();
}
String loginUrl = temporaryCredentials.getLoginUrl();
logger.info(loginUrl);
return loginUrl;
}
/**
* @param verifier
*/
public void loginOobValidate(String verifier) {
try {
List<String> userIdsOrNames = Collections.singletonList(EtsyAPI.__SELF__);
accessCredentials = etsyDataService.getAccessCredentials(temporaryCredentials, verifier);
saveAccessCredentials( accessCredentials );
List<EtsyUser> users = etsyDataService.getUser(accessCredentials,
userIdsOrNames,
UserAssociations.values());
authorizedEtsyAccountUser = users.get(0);
} catch (EtsyAPIException e) {
e.printStackTrace();
}
}
/**
* @return
*/
public EtsyUser getAuthorizedEtsyAccountUser() {
return authorizedEtsyAccountUser;
}
/**
* Search for one shop, based on Etsy Shop Id.
*
* @return
*/
public EtsyShop getEtsyShopReceiptsAndTransactions(String etsyShopId) {
List<String> shopIdsOrNames = new ArrayList<String>();
shopIdsOrNames.add(etsyShopId);
try {
List<EtsyShop> etsyShops = etsyDataService.getShop(accessCredentials, shopIdsOrNames,
ShopAssociations.Receipts,
ShopAssociations.Transactions,
ShopAssociations.Listings);
return etsyShops.get(0);
} catch (EtsyAPIException e) {
e.printStackTrace();
return null;
}
}
/**
* @return
*/
private Credentials loadAccessCredentials() {
logger.info("Attempting to load: '" + workingDirectory + accessCredentialsFileName + "'");
try {
InputStream inputStream = new FileInputStream(workingDirectory + accessCredentialsFileName);
Properties properties = new Properties();
properties.load(inputStream);
String token = (String) properties.get("token");
String tokenSecret = (String) properties.get("tokenSecret");
return Credentials.forKeyPair(token, tokenSecret);
} catch (FileNotFoundException e) {
logger.error("Error, could not load from file.");
} catch (IOException e) {
logger.error("Error, could not load from file.");
}
return null;
}
/**
* @param accessCredentials
*/
private void saveAccessCredentials(Credentials accessCredentials) {
logger.info("Attempting to save: '" + workingDirectory + accessCredentialsFileName + "'");
Properties properties = new Properties();
properties.setProperty("token", accessCredentials.getToken());
properties.setProperty("tokenSecret", accessCredentials.getTokenSecret());
try {
OutputStream outputStream = new FileOutputStream(workingDirectory + accessCredentialsFileName);
properties.store(outputStream," DO NOT SHARE, Etsy access credentials");
} catch (FileNotFoundException e) {
logger.error("Error, could not save to file.");
e.printStackTrace();
} catch (IOException e) {
logger.error("Error, could not save to file.");
e.printStackTrace();
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment