Skip to content

Commit

Permalink
Square Client cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Dec 8, 2023
1 parent 7a35543 commit bac1f7f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,53 @@ public abstract class AbstractFunction<T> implements Cloneable {
@Setter(AccessLevel.PRIVATE)
private ChannelPlatform channelPlatform;


/**
* The Lex Session ID.
*/
@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PRIVATE)
private String sessionId;

/**
* Define here since used by most the of the functions
*/
protected final static SquareClient client = new SquareClient.Builder()
.accessToken(System.getenv("SQUARE_API_KEY"))
.environment(Environment.valueOf(System.getenv("SQUARE_ENVIRONMENT")))
.build();

private final static boolean squareEnabled;
private final static SquareClient squareClient;

static {
final var key = System.getenv("SQUARE_API_KEY");
final var loc = System.getenv("SQUARE_LOCATION_ID");

squareEnabled = !((loc == null || loc.isBlank() || loc.equalsIgnoreCase("DISABLED")) || (key == null || key.isBlank() || loc.equalsIgnoreCase("DISABLED")));
squareEnabled = !((loc == null || loc.isBlank() || loc.equalsIgnoreCase("DISABLED")) || (key == null || key.isBlank() || key.equalsIgnoreCase("DISABLED")));
log.debug("Square Enabled = " + squareEnabled);

// If square enabled, then configure the client
if (squareEnabled) {
squareClient = new SquareClient.Builder()
.accessToken(key)
.environment(Environment.valueOf(System.getenv("SQUARE_ENVIRONMENT")))
.build();
} else {
squareClient = null;
}
}

/**
* Is Square enabled (API Key and Location ID set to something).
* Is Square enabled (API Key and Location ID set to something that looks
* valid).
*
* @return
*/
public final static boolean isSquareEnabled() {
return squareEnabled;
}

/**
* Square client to make API Calls.
*
* @return client or null if not enabled
*/
protected final static SquareClient getSquareClient() {
return squareClient;
}

/**
* Register all the functions in this package. This should be called by a
* top level object that is being initialized like a lambda, so during
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Class getRequestClass() {
public Function<Request, Object> getExecutor() {
return (var r) -> {
try {
final var objects = client.getCatalogApi()
final var objects = getSquareClient().getCatalogApi()
// Only retrieve Category objects
.searchCatalogObjects(new SearchCatalogObjectsRequest.Builder()
.includeDeletedObjects(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Class getRequestClass() {
public Function<Request, Object> getExecutor() {
return (var r) -> {
try {
final var loc = client.getLocationsApi().retrieveLocation(System.getenv("SQUARE_LOCATION_ID")).getLocation();
final var loc = getSquareClient().getLocationsApi().retrieveLocation(System.getenv("SQUARE_LOCATION_ID")).getLocation();
final var bh = new BusinessHours(loc);

final var tz = ZoneId.of(loc.getTimezone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Class getRequestClass() {
public Function<Request, Object> getExecutor() {
return (var r) -> {
try {
var items = client.getCatalogApi()
var items = getSquareClient().getCatalogApi()
.searchCatalogItems(new SearchCatalogItemsRequest.Builder().textFilter(r.search_text).limit(5).build())
.getItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Class getRequestClass() {
public Function<Request, Object> getExecutor() {
return (var r) -> {
try {
return client.getTeamApi()
return getSquareClient().getTeamApi()
.searchTeamMembers(new SearchTeamMembersRequest.Builder().query(new SearchTeamMembersQuery.Builder()
// Only return active employees at the defined location
.filter(new SearchTeamMembersFilter.Builder().status("ACTIVE").locationIds(List.of(System.getenv("SQUARE_LOCATION_ID"))).build())
Expand Down

0 comments on commit bac1f7f

Please sign in to comment.