This guide illustrates differences between Lob’s the legacy versions of this SDK and the new current version.
In this guide we compare how v12.*.*
and >=v13.0.0
implement the following method pattern.
- CREATE
- LIST
- GET
- DELETE
- VERIFY (BANK ACCOUNTS)
- UPDATE (TEMPLATES)
Similar to the legacy version, simply update your POM file dependency as follows:
<dependency>
<groupId>com.lob</groupId>
<artifactId>lob-java</artifactId>
<version>{{ CURRENT_VERSION_OF_JAVA }}</version>
</dependency>
If you are using the Spring framework, you will also need to add the following two blocks:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
</dependencies>
</dependencyManagement>
and
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
import com.lob.api.ApiClient;
import com.lob.api.ApiException;
import com.lob.api.Configuration;
import com.lob.api.auth.HttpBasicAuth;
import com.lob.api.client.*;
public class Controller {
private final ApiClient lobClient;
Controller() {
ApiClient client = Configuration.getDefaultApiClient();
HttpBasicAuth basicAuth = (HttpBasicAuth) client.getAuthentication("basicAuth");
basicAuth.setUsername("<<YOUR API KEY HERE>>");
this.lobClient = client;
}
}
You then instantiate the specific resource API that you need access to as follows:
AddressesApi apiInstance = new AddressesApi(this.lobClient);
The new SDK version abstracts the request/response interaction out of the calling code such that you create the resource and pass it to the corresponding API in a try/catch such that any non-success is an ApiException
that must be handled.
Here is a sample of a legacy lob-java CREATE function:
LobResponse<Address> response = new Address.RequestBuilder()
.setDescription("Harry - Office")
.setName("Harry Zhang")
.setCompany("Lob")
.setLine1("2261 Market Street")
.setLine2("Ste 5668")
.setCity("San Francisco")
.setState("CA")
.setZip("94114")
.setCountry("US")
.setPhone("555-555-5555")
.setEmail("harry@lob.com")
.create();
Address address = response.getResponseBody();
Here is a sample of the updated CREATE method
try {
AddressEditable address = new AddressEditable();
address.setName("Harry Zhang");
address.setCompany("Lob");
address.setEmail("harry@lob.com");
address.setPhone("5555555555");
address.setAddressLine1("2261 Market Street");
address.setAddressLine2("Ste 5668");
address.setAddressCity("San Francisco");
address.setAddressState("CA");
address.setAddressZip("94114");
address.setAddressCountry(CountryExtended.US);
apiInstance.addressCreate(address);
} catch (ApiException e) {
e.printStackTrace();
}
In these create operations, the required Address records are created alongside the record they are associated with.
LobResponse<Letter> response = new Letter.RequestBuilder()
.setDescription("Demo Letter")
.setFile("https://s3-us-west-2.amazonaws.com/public.lob.com/assets/us_letter_1pg.pdf")
.setColor(true)
.setTo(
new Address.RequestBuilder()
.setName("Harry Zhang")
.setLine1("2261 Market Street Ste 5668")
.setCity("San Francisco")
.setState("CA")
.setZip("94114")
)
.setFrom("adr_210a8d4b0b76d77b")
.create();
Letter letter = response.getResponseBody();
The same letter can be created as follows:
try {
AddressEditable addressTo = new AddressEditable();
addressTo.setName("Harry Zhang");
addressTo.setAddressLine1("2261 Market Street");
addressTo.setAddressLine2("Ste 5668");
addressTo.setAddressCity("San Francisco");
addressTo.setAddressState("CA");
addressTo.setAddressZip("94114");
LetterEditable letterRaw = new LetterEditable();
letterRaw.setDescription("Demo Letter");
letterRaw.setTo(addressTo);
letterRaw.setFrom("adr_210a8d4b0b76d77b");
letterRaw.setFile("https://s3-us-west-2.amazonaws.com/public.lob.com/assets/us_letter_1pg.pdf");
letterRaw.setColor(true);
Letter letter = apiInstance.create(letterRaw, null);
} catch (ApiException e) {
e.printStackTrace();
}
Here is a sample of a legacy lob-java LIST method:
Map<String, Object> params = new HashMap<>();
params.put("limit", 2);
LobResponse<AddressCollection> response = Address.list(params);
AddressCollection addresses = response.getResponseBody();
Here is a sample of the updated LIST method:
try {
AddressList addresses = apiInstance.list(2, null, null, null, null, null);
} catch (ApiException e) {
e.printStackTrace();
}
Here is a sample of a legacy GET method:
LobResponse<Address> response = Address.retrieve("adr_fa85158b26c3eb7c");
Address address = response.getResponseBody();
Here is a sample of the updated SDK GET by ID method:
try {
Address address = apiInstance.get("adr_fa85158b26c3eb7c");
} catch (ApiException e) {
e.printStackTrace();
}
Here is a sample of the legacy DELETE method:
LobResponse<Address> response = Address.delete("adr_43769b47aed248c2");
Address address = response.getResponseBody();
Here is a sample of the updated SDK DELETE method:
try {
AddressDeletion address = apiInstance.delete("adr_fa85158b26c3eb7c");
} catch (ApiException e) {
e.printStackTrace();
}
Here is a sample of the legacy Bank Account Verify method
LobResponse<BankAccount> response = BankAccount.verify(newBankAccount.getId(), Arrays.asList(25, 63));
BankAccount bankAccount = response.getResponseBody();
Here is a sample of the updated SDK Bank Account Verify method:
try {
BankAccountVerify verification = new BankAccountVerify();
verification.addAmountsItem(11);
verification.addAmountsItem(35);
BankAccount account = apiInstance.verify("bank_8cad8df5354d33f", verification);
} catch (ApiException e) {
e.printStackTrace();
}
The Template Update endpoint updates the description and/or published version of the template with a given id. This is how it is done:
try {
TemplateUpdate templateUpdate = new TemplateUpdate();
templateUpdate.setDescription("Updated Example");
templateUpdate.setPublishedVersion("vrsn_a");
Template template = apiInstance.update("tmpl_c94e83ca2cd5121", templateUpdate);
} catch (ApiException e) {
e.printStackTrace();
}