Skip to content

Commit 7a17bfa

Browse files
author
stephen powis
committed
update readme, add example code
1 parent 0e64cea commit 7a17bfa

File tree

7 files changed

+173
-5
lines changed

7 files changed

+173
-5
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,70 @@ requesting it.
1515

1616
## How to use this library
1717

18-
More information soon.
18+
This client library is released on Maven Central. Add a new dependency to your project's POM file:
19+
20+
```xml
21+
<dependency>
22+
<groupId>com.darksci</groupId>
23+
<artifactId>pardot-api-client</artifactId>
24+
<version>0.1.0-SNAPSHOT</version>
25+
</dependency>
26+
```
27+
28+
Example Code:
29+
```java
30+
/*
31+
* Create a new configuration object with your Pardot credentials.
32+
*
33+
* This configuration also allows you to define some optional details on your connection,
34+
* such as using an outbound proxy (authenticated or not).
35+
*/
36+
final Configuration configuration = new Configuration("YourPardotUserNameHere", "PardotPassword", "UserKey");
37+
38+
/*
39+
* Create an instance of PardotClient, passing your configuration.
40+
*/
41+
final PardotClient client = new PardotClient(configuration);
42+
43+
/*
44+
* The client will automatically authenticate when you make your first request, no need to
45+
* explicitly login.
46+
*
47+
* Lets create a simple Account request, and execute it.
48+
*/
49+
final AccountReadRequest accountReadRequest = new AccountReadRequest();
50+
final Account account = client.accountRead(accountReadRequest);
51+
52+
53+
/*
54+
* Or lets do a more complex Campaign search.
55+
*/
56+
final CampaignQueryRequest campaignQueryRequest = new CampaignQueryRequest()
57+
.withUpdatedAfter(DateParameter.last7Days())
58+
.withIdLessThan(1234L)
59+
.withSortById()
60+
.withSortOrderDescending();
61+
final CampaignQueryResponse.Result campaignQueryResponse = client.campaignQuery(campaignQueryRequest);
62+
63+
/*
64+
* And when you're done, call close on PardotClient.
65+
*/
66+
client.close();
67+
```
68+
69+
Or Using the Try-With-Resources Pattern:
70+
```java
71+
/*
72+
* Since PardotClient implements Autoclosable, you can also use the try-with-resources pattern.
73+
*/
74+
final Configuration configuration = new Configuration("YourPardotUserNameHere", "PardotPassword", "UserKey");
75+
try (final PardotClient client = new PardotClient(configuration)) {
76+
// Use client instance as needed
77+
client.accountRead(new AccountReadRequest());
78+
79+
// client.close() is automatically called at the end of the try {} block.
80+
}
81+
```
1982

2083
## What Features are implemented?
2184

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.darksci.pardot.api;
2+
3+
import com.darksci.pardot.api.request.DateParameter;
4+
import com.darksci.pardot.api.request.account.AccountReadRequest;
5+
import com.darksci.pardot.api.request.campaign.CampaignQueryRequest;
6+
import com.darksci.pardot.api.response.account.Account;
7+
import com.darksci.pardot.api.response.campaign.CampaignQueryResponse;
8+
9+
import java.io.IOException;
10+
11+
/**
12+
* Just a simple example code showing basic usage.
13+
*/
14+
public class Example {
15+
16+
/**
17+
* Example code.
18+
*/
19+
public static void example() throws IOException {
20+
/*
21+
* Create a new configuration object with your Pardot credentials.
22+
*
23+
* This configuration also allows you to define some optional details on your connection,
24+
* such as using an outbound proxy (authenticated or not).
25+
*/
26+
final Configuration configuration = new Configuration("YourPardotUserNameHere", "PardotPassword", "UserKey");
27+
28+
/*
29+
* Create an instance of PardotClient, passing your configuration.
30+
*/
31+
final PardotClient client = new PardotClient(configuration);
32+
33+
/*
34+
* The client will automatically authenticate when you make your first request, no need to
35+
* explicitly login.
36+
*
37+
* Lets create a simple Account request, and execute it.
38+
*/
39+
final AccountReadRequest accountReadRequest = new AccountReadRequest();
40+
final Account account = client.accountRead(accountReadRequest);
41+
42+
43+
/*
44+
* Or lets do a more complex Campaign search.
45+
*/
46+
final CampaignQueryRequest campaignQueryRequest = new CampaignQueryRequest()
47+
.withUpdatedAfter(DateParameter.last7Days())
48+
.withIdLessThan(1234L)
49+
.withSortById()
50+
.withSortOrderDescending();
51+
final CampaignQueryResponse.Result campaignQueryResponse = client.campaignQuery(campaignQueryRequest);
52+
53+
/*
54+
* And when you're done, call close on PardotClient.
55+
*/
56+
client.close();
57+
}
58+
59+
/**
60+
* Example code showing off Auto-closable.
61+
*/
62+
public static void autoCloseableExample() throws IOException {
63+
/*
64+
* Since PardotClient implements Autoclosable, you can also use the try-with-resources pattern.
65+
*/
66+
final Configuration configuration = new Configuration("YourPardotUserNameHere", "PardotPassword", "UserKey");
67+
try (final PardotClient client = new PardotClient(configuration)) {
68+
// Use client instance as needed
69+
client.accountRead(new AccountReadRequest());
70+
71+
// client.close() is automatically called at the end of the try {} block.
72+
}
73+
}
74+
}

src/main/java/com/darksci/pardot/api/PardotClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
/**
4747
* Interface for Pardot's API.
4848
*/
49-
public class PardotClient {
49+
public class PardotClient implements AutoCloseable {
5050
private static final Logger logger = LoggerFactory.getLogger(PardotClient.class);
5151

5252
/**
@@ -288,4 +288,11 @@ public Email emailSendOneToOne(final EmailSendOneToOneRequest request) throws IO
288288
public Email emailSendList(final EmailSendListRequest request) throws IOException {
289289
return submitRequest(request, new EmailReadResponseParser());
290290
}
291+
292+
/**
293+
* Clean up instance, releasing any resources held internally.
294+
*/
295+
public void close() {
296+
getRestClient().close();
297+
}
291298
}

src/main/java/com/darksci/pardot/api/request/BaseQueryRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class BaseQueryRequest<T> extends BaseRequest<T> {
1313
* @param idGreaterThan Id constraint.
1414
* @return BaseQueryRequest
1515
*/
16-
public T withIdGreaterThan(final Integer idGreaterThan) {
16+
public T withIdGreaterThan(final Long idGreaterThan) {
1717
return setParam("id_greater_than", idGreaterThan);
1818
}
1919

@@ -22,7 +22,7 @@ public T withIdGreaterThan(final Integer idGreaterThan) {
2222
* @param idLessThan Id constraint.
2323
* @return BaseQueryRequest
2424
*/
25-
public T withIdLessThan(final Integer idLessThan) {
25+
public T withIdLessThan(final Long idLessThan) {
2626
return setParam("id_less_than", idLessThan);
2727
}
2828

src/main/java/com/darksci/pardot/api/rest/HttpClientRestClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ public RestResponse submitRequest(final Request request) throws IOException {
138138
return submitRequest(request, new RestResponseHandler());
139139
}
140140

141+
@Override
142+
public void close() {
143+
if (httpClient != null) {
144+
try {
145+
httpClient.close();
146+
} catch (IOException e) {
147+
logger.error("Error closing: {}", e.getMessage(), e);
148+
}
149+
}
150+
httpClient = null;
151+
}
152+
141153
/**
142154
* For issuing an API Request.
143155
* @param request The Request to perform.

src/main/java/com/darksci/pardot/api/rest/MockRestClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ public class MockRestClient implements RestClient {
1515

1616
@Override
1717
public void init(final Configuration configuration) {
18-
18+
// Noop.
1919
}
2020

2121
@Override
2222
public RestResponse submitRequest(final Request request) throws IOException {
23+
// Not implemented yet.
2324
return null;
2425
}
26+
27+
@Override
28+
public void close() {
29+
// Noop.
30+
}
2531
}

src/main/java/com/darksci/pardot/api/rest/RestClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ public interface RestClient {
2323
* @throws IOException if something goes wrong?
2424
*/
2525
RestResponse submitRequest(final Request request) throws IOException;
26+
27+
/**
28+
* Called to release any internally held resources.
29+
*/
30+
void close();
31+
2632
}

0 commit comments

Comments
 (0)