Create a Data Access Object for Webex 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 Webex integrates connectivity to live Webex data in Java applications. By pairing these technologies, you gain simple, programmatic access to Webex data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Webex data.
Create a DAO for the Webex AdminAuditEvents 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 MyAdminAuditEventsDAO {
//request specific data from Webex (String type is used for simplicity)
@SqlQuery("SELECT FROM AdminAuditEvents WHERE OrgId = :orgId")
String findByOrgId(@Bind("orgId") String orgId);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Webex
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Webex.
To authenticate to Webex, and connect to your own data or to allow other users to connect to their data, you can use the OAuth standard.
Using OAuth Authentication
First, you will need to register an OAuth application with Webex. To do so, navigate to the Webex Developer Portal and create a new integration. Your OAuth application will be assigned a client id and a client secret.
After setting the following connection properties, you are ready to connect:
- AuthScheme: Set this to OAuth.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage the process to obtain the OAuthAccessToken.
- OAuthClientId: Set this to the client_id that is specified in your app settings.
- OAuthClientSecret: Set this to the client_secret that is specified in your app settings.
- CallbackURL: Set this to the Redirect URI that is specified in your app settings.
- Scope: Set this in ProfileSettings to specify the OAuth scopes requested during authorization. Multiple scopes can be space-separated (e.g. ProfileSettings='Scope=spark:all spark:kms').
Example connection string:
Profile=C:\profiles\Webex.apip;Authscheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackUrl=your_callback_url;ProfileSettings='Scope=your_scopes';
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Webex JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.api.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Webex will typically look like the following:
jdbc:api:Profile=C:\profiles\Webex.apip;Authscheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackUrl=your_callback_url;ProfileSettings='Scope=your_scopes';
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:api:Profile=C:\profiles\Webex.apip;Authscheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackUrl=your_callback_url;ProfileSettings='Scope=your_scopes';");
MyAdminAuditEventsDAO dao = dbi.open(MyAdminAuditEventsDAO.class);
//do stuff with the DAO
dao.close();
Read Webex Data
With the connection open to Webex, simply call the previously defined method to retrieve data from the AdminAuditEvents entity in Webex.
//disply the result of our 'find' method
String = dao.findByOrgId("org123");
System.out.println();
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Webex by integrating with the CData JDBC Driver for Webex. Download a free trial and work with live Webex data in custom Java applications today.