Create a Data Access Object for Talkdesk Data using JDBI
JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Talkdesk integrates connectivity to live Talkdesk data in Java applications. By pairing these technologies, you gain simple, programmatic access to Talkdesk data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Talkdesk data.
Create a DAO for the Talkdesk Users Entity
The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.
public interface MyUsersDAO {
//request specific data from Talkdesk (String type is used for simplicity)
@SqlQuery("SELECT Name FROM Users WHERE Active = :active")
String findNameByActive(@Bind("active") String active);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Talkdesk
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Talkdesk.
Talkdesk uses the OAuth 2.0 Client Credentials grant. There is no browser-based authorization step and no callback URL.
Set the following connection properties:
- AccountName: The name of your Talkdesk account.
- Region: The region where your Talkdesk instance is deployed. Supported values are US (default), EU, CA, AU, UK, and FedRamp.
- OAuthClientId: The Client Id assigned when you registered your custom OAuth application.
- OAuthClientSecret: The Client Secret assigned to your custom OAuth application.
Creating a Custom OAuth Application
- Log in to your Talkdesk account and select OAuth Clients from the navigation menu.
- Click Create OAuth Client and give the client a descriptive name.
- Set Grant Type to Client Credentials.
- Click Add scopes and select the scopes for the data you want to access.
- Click Create and copy the Client Id and Client Secret.
When you connect, the driver automatically requests an access token from Talkdesk, caches it, and refreshes it when it expires. Make sure the scopes selected for the application match the views you plan to query, or the token request can fail.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Talkdesk JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.talkdesk.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Talkdesk will typically look like the following:
jdbc:talkdesk:AccountName=myAccount;Region=US;OAuthClientId=myClientId;OAuthClientSecret=myClientSecret;
Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.
DBI dbi = new DBI("jdbc:talkdesk:AccountName=myAccount;Region=US;OAuthClientId=myClientId;OAuthClientSecret=myClientSecret;");
MyUsersDAO dao = dbi.open(MyUsersDAO.class);
//do stuff with the DAO
dao.close();
Read Talkdesk Data
With the connection open to Talkdesk, simply call the previously defined method to retrieve data from the Users entity in Talkdesk.
//disply the result of our 'find' method
String name = dao.findNameByActive("true");
System.out.println(name);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Talkdesk by integrating with the CData JDBC Driver for Talkdesk. Download a free trial and work with live Talkdesk data in custom Java applications today.