Skip to content

Commit 9bcdfce

Browse files
authored
Merge pull request #2 from Square-Developers/mikek/upgrade
upgrade quickstart to work with new sdk
2 parents 0b8d73b + 7ac190e commit 9bcdfce

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

quickstart/src/main/java/com/square/examples/Quickstart.java

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
import java.io.InputStream;
55
import java.util.Properties;
66

7-
import com.squareup.square.*;
8-
import com.squareup.square.api.*;
9-
import com.squareup.square.authentication.BearerAuthModel;
10-
import com.squareup.square.models.*;
11-
import com.squareup.square.models.Error;
12-
import com.squareup.square.exceptions.*;
7+
import com.squareup.square.AsyncSquareClient;
8+
import com.squareup.square.core.Environment;
9+
import com.squareup.square.types.Location;
10+
import com.squareup.square.types.Error;
11+
import com.squareup.square.core.SquareApiException;
12+
import com.squareup.square.types.Address;
1313

1414
public class Quickstart {
1515
public static void main(String[] args) {
16-
1716
InputStream inputStream =
1817
Quickstart.class.getResourceAsStream("/config.properties");
1918
Properties prop = new Properties();
@@ -25,37 +24,59 @@ public static void main(String[] args) {
2524
e.printStackTrace();
2625
}
2726

28-
SquareClient client = new SquareClient.Builder()
29-
.bearerAuthCredentials(new BearerAuthModel.Builder(prop.getProperty("SQUARE_ACCESS_TOKEN")).build())
30-
.environment(Environment.SANDBOX)
31-
.build();
32-
33-
LocationsApi locationsApi = client.getLocationsApi();
27+
AsyncSquareClient client = AsyncSquareClient.builder()
28+
.token(prop.getProperty("SQUARE_ACCESS_TOKEN"))
29+
.environment(Environment.SANDBOX)
30+
.build();
3431

35-
locationsApi.listLocationsAsync().thenAccept(result -> {
36-
System.out.println("Location(s) for this account:");
37-
for (Location l : result.getLocations()) {
38-
System.out.printf("%s: %s, %s, %s\n",
39-
l.getId(), l.getName(),
40-
l.getAddress().getAddressLine1(),
41-
l.getAddress().getLocality());
42-
}
32+
client.locations().list()
33+
.thenAccept(result -> {
34+
System.out.println("Location(s) for this account:");
35+
result.getLocations().ifPresent(locations -> {
36+
for (Location l : locations) {
37+
// Build location details safely
38+
StringBuilder locationInfo = new StringBuilder();
39+
40+
// Add ID if present
41+
l.getId().ifPresent(id -> locationInfo.append("ID: ").append(id));
42+
43+
// Add name if present
44+
l.getName().ifPresent(name ->
45+
locationInfo.append(locationInfo.length() > 0 ? ", " : "")
46+
.append("Name: ").append(name));
47+
48+
// Handle address details if present
49+
l.getAddress().ifPresent(address -> {
50+
// Add address line 1 if present
51+
address.getAddressLine1().ifPresent(line1 ->
52+
locationInfo.append(locationInfo.length() > 0 ? ", " : "")
53+
.append("Address: ").append(line1));
54+
55+
// Add locality (city) if present
56+
address.getLocality().ifPresent(locality ->
57+
locationInfo.append(locationInfo.length() > 0 ? ", " : "")
58+
.append("City: ").append(locality));
59+
});
4360

44-
}).exceptionally(exception -> {
45-
try {
46-
throw exception.getCause();
47-
} catch (ApiException ae) {
48-
for (Error err : ae.getErrors()) {
49-
System.out.println(err.getCategory());
50-
System.out.println(err.getCode());
51-
System.out.println(err.getDetail());
61+
// Print the complete location information
62+
System.out.println(locationInfo.toString());
63+
}
64+
});
65+
})
66+
.exceptionally(exception -> {
67+
if (exception.getCause() instanceof SquareApiException) {
68+
SquareApiException apiException = (SquareApiException) exception.getCause();
69+
apiException.errors().forEach(error -> {
70+
System.out.println("Category: " + error.getCategory());
71+
System.out.println("Code: " + error.getCode());
72+
System.out.println("Detail: " + error.getDetail());
73+
});
74+
} else {
75+
System.out.println("An unexpected error occurred:");
76+
exception.printStackTrace();
5277
}
53-
} catch (Throwable t) {
54-
t.printStackTrace();
55-
}
56-
return null;
57-
}).join();
58-
59-
SquareClient.shutdown();
78+
return null;
79+
})
80+
.join();
6081
}
61-
}
82+
}

0 commit comments

Comments
 (0)