embulk-input-marketo 0.5.6 → 0.5.7.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +37 -0
- data/.gitignore +11 -2
- data/.travis.yml +5 -45
- data/LICENSE.txt +21 -0
- data/README.md +14 -65
- data/build.gradle +102 -0
- data/config/checkstyle/checkstyle.xml +128 -0
- data/config/checkstyle/default.xml +108 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +169 -0
- data/gradlew.bat +84 -0
- data/lib/embulk/input/marketo.rb +3 -0
- data/settings.gradle +1 -0
- data/src/main/java/org/embulk/input/marketo/CsvTokenizer.java +677 -0
- data/src/main/java/org/embulk/input/marketo/MarketoInputPlugin.java +15 -0
- data/src/main/java/org/embulk/input/marketo/MarketoInputPluginDelegate.java +77 -0
- data/src/main/java/org/embulk/input/marketo/MarketoService.java +30 -0
- data/src/main/java/org/embulk/input/marketo/MarketoServiceImpl.java +176 -0
- data/src/main/java/org/embulk/input/marketo/MarketoUtils.java +172 -0
- data/src/main/java/org/embulk/input/marketo/delegate/ActivityBulkExtractInputPlugin.java +63 -0
- data/src/main/java/org/embulk/input/marketo/delegate/CampaignInputPlugin.java +67 -0
- data/src/main/java/org/embulk/input/marketo/delegate/LeadBulkExtractInputPlugin.java +61 -0
- data/src/main/java/org/embulk/input/marketo/delegate/LeadWithListInputPlugin.java +58 -0
- data/src/main/java/org/embulk/input/marketo/delegate/LeadWithProgramInputPlugin.java +56 -0
- data/src/main/java/org/embulk/input/marketo/delegate/MarketoBaseBulkExtractInputPlugin.java +260 -0
- data/src/main/java/org/embulk/input/marketo/delegate/MarketoBaseInputPluginDelegate.java +69 -0
- data/src/main/java/org/embulk/input/marketo/exception/MarketoAPIException.java +30 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoAccessTokenResponse.java +92 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoBulkExtractRequest.java +59 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoError.java +40 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoField.java +91 -0
- data/src/main/java/org/embulk/input/marketo/model/MarketoResponse.java +81 -0
- data/src/main/java/org/embulk/input/marketo/model/filter/DateRangeFilter.java +31 -0
- data/src/main/java/org/embulk/input/marketo/model/filter/ListFilter.java +10 -0
- data/src/main/java/org/embulk/input/marketo/model/filter/MarketoFilter.java +8 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoBaseRestClient.java +226 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoFileResponseEntityReader.java +69 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoRESTEndpoint.java +44 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoResponseJetty92EntityReader.java +88 -0
- data/src/main/java/org/embulk/input/marketo/rest/MarketoRestClient.java +332 -0
- data/src/main/java/org/embulk/input/marketo/rest/RecordPagingIterable.java +130 -0
- data/src/test/java/org/embulk/input/marketo/TestMarketoInputPlugin.java +5 -0
- data/src/test/java/org/embulk/input/marketo/rest/MarketoBaseRestClientTest.java +220 -0
- metadata +65 -222
- data/.ruby-version +0 -1
- data/.travis.yml.erb +0 -42
- data/Gemfile +0 -3
- data/LICENSE +0 -13
- data/Rakefile +0 -20
- data/embulk-input-marketo.gemspec +0 -28
- data/gemfiles/embulk-latest +0 -4
- data/gemfiles/template.erb +0 -4
- data/lib/embulk/input/marketo/activity_log.rb +0 -103
- data/lib/embulk/input/marketo/base.rb +0 -139
- data/lib/embulk/input/marketo/lead.rb +0 -143
- data/lib/embulk/input/marketo_api.rb +0 -22
- data/lib/embulk/input/marketo_api/soap/activity_log.rb +0 -103
- data/lib/embulk/input/marketo_api/soap/base.rb +0 -135
- data/lib/embulk/input/marketo_api/soap/lead.rb +0 -91
- data/test/activity_log_fixtures.rb +0 -216
- data/test/embulk/input/marketo/test_activity_log.rb +0 -444
- data/test/embulk/input/marketo/test_base.rb +0 -76
- data/test/embulk/input/marketo/test_lead.rb +0 -605
- data/test/embulk/input/marketo_api/soap/test_activity_log.rb +0 -154
- data/test/embulk/input/marketo_api/soap/test_base.rb +0 -96
- data/test/embulk/input/marketo_api/soap/test_lead.rb +0 -139
- data/test/embulk/input/test_marketo_api.rb +0 -28
- data/test/lead_fixtures.rb +0 -111
- data/test/mute_logger.rb +0 -7
- data/test/override_assert_raise.rb +0 -18
- data/test/prepare_embulk.rb +0 -15
- data/test/run-test.rb +0 -26
- data/test/savon_helper.rb +0 -17
@@ -0,0 +1,31 @@
|
|
1
|
+
package org.embulk.input.marketo.model.filter;
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Created by tai.khuu on 8/27/17.
|
5
|
+
*/
|
6
|
+
public class DateRangeFilter implements MarketoFilter
|
7
|
+
{
|
8
|
+
private String startAt;
|
9
|
+
|
10
|
+
private String endAt;
|
11
|
+
|
12
|
+
public String getStartAt()
|
13
|
+
{
|
14
|
+
return startAt;
|
15
|
+
}
|
16
|
+
|
17
|
+
public void setStartAt(String startAt)
|
18
|
+
{
|
19
|
+
this.startAt = startAt;
|
20
|
+
}
|
21
|
+
|
22
|
+
public String getEndAt()
|
23
|
+
{
|
24
|
+
return endAt;
|
25
|
+
}
|
26
|
+
|
27
|
+
public void setEndAt(String endAt)
|
28
|
+
{
|
29
|
+
this.endAt = endAt;
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1,226 @@
|
|
1
|
+
package org.embulk.input.marketo.rest;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
+
import com.google.common.annotations.VisibleForTesting;
|
5
|
+
import com.google.common.base.Joiner;
|
6
|
+
import com.google.common.collect.ArrayListMultimap;
|
7
|
+
import com.google.common.collect.ImmutableListMultimap;
|
8
|
+
import com.google.common.collect.Maps;
|
9
|
+
import com.google.common.collect.Multimap;
|
10
|
+
import org.eclipse.jetty.client.HttpClient;
|
11
|
+
import org.eclipse.jetty.client.api.ContentProvider;
|
12
|
+
import org.eclipse.jetty.client.api.Request;
|
13
|
+
import org.eclipse.jetty.client.api.Response;
|
14
|
+
import org.eclipse.jetty.client.util.StringContentProvider;
|
15
|
+
import org.eclipse.jetty.http.HttpMethod;
|
16
|
+
import org.embulk.input.marketo.exception.MarketoAPIException;
|
17
|
+
import org.embulk.input.marketo.model.MarketoAccessTokenResponse;
|
18
|
+
import org.embulk.input.marketo.model.MarketoError;
|
19
|
+
import org.embulk.spi.DataException;
|
20
|
+
import org.embulk.spi.Exec;
|
21
|
+
import org.embulk.util.retryhelper.jetty92.Jetty92ResponseReader;
|
22
|
+
import org.embulk.util.retryhelper.jetty92.Jetty92RetryHelper;
|
23
|
+
import org.embulk.util.retryhelper.jetty92.Jetty92SingleRequester;
|
24
|
+
import org.embulk.util.retryhelper.jetty92.StringJetty92ResponseEntityReader;
|
25
|
+
import org.slf4j.Logger;
|
26
|
+
|
27
|
+
import java.io.IOException;
|
28
|
+
import java.net.SocketTimeoutException;
|
29
|
+
import java.nio.charset.StandardCharsets;
|
30
|
+
import java.util.Map;
|
31
|
+
import java.util.concurrent.ExecutionException;
|
32
|
+
import java.util.concurrent.TimeoutException;
|
33
|
+
|
34
|
+
import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS;
|
35
|
+
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Marketo base rest client that provide
|
39
|
+
* Created by tai.khuu on 9/7/17.
|
40
|
+
*/
|
41
|
+
public class MarketoBaseRestClient implements AutoCloseable
|
42
|
+
{
|
43
|
+
private static final Logger LOGGER = Exec.getLogger(MarketoBaseRestClient.class);
|
44
|
+
|
45
|
+
private static final String APPLICATION_JSON = "application/json";
|
46
|
+
|
47
|
+
private static final String AUTHORIZATION_HEADER = "Authorization";
|
48
|
+
|
49
|
+
protected static final long READ_TIMEOUT_MILLIS = 30000;
|
50
|
+
|
51
|
+
private String identityEndPoint;
|
52
|
+
|
53
|
+
private String clientId;
|
54
|
+
|
55
|
+
private String clientSecret;
|
56
|
+
|
57
|
+
private String accessToken;
|
58
|
+
|
59
|
+
private int marketoLimitIntervalMilis;
|
60
|
+
|
61
|
+
private Jetty92RetryHelper retryHelper;
|
62
|
+
|
63
|
+
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false).configure(ALLOW_UNQUOTED_CONTROL_CHARS, false);
|
64
|
+
|
65
|
+
MarketoBaseRestClient(String identityEndPoint, String clientId, String clientSecret, int marketoLimitIntervalMilis, Jetty92RetryHelper retryHelper)
|
66
|
+
{
|
67
|
+
this.identityEndPoint = identityEndPoint;
|
68
|
+
this.clientId = clientId;
|
69
|
+
this.clientSecret = clientSecret;
|
70
|
+
this.retryHelper = retryHelper;
|
71
|
+
this.marketoLimitIntervalMilis = marketoLimitIntervalMilis;
|
72
|
+
}
|
73
|
+
|
74
|
+
private void renewAccessToken()
|
75
|
+
{
|
76
|
+
accessToken = requestAccessToken();
|
77
|
+
}
|
78
|
+
|
79
|
+
@VisibleForTesting
|
80
|
+
public String getAccessToken()
|
81
|
+
{
|
82
|
+
if (accessToken == null) {
|
83
|
+
synchronized (this) {
|
84
|
+
if (accessToken == null) {
|
85
|
+
accessToken = requestAccessToken();
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
return accessToken;
|
90
|
+
}
|
91
|
+
|
92
|
+
private String requestAccessToken()
|
93
|
+
{
|
94
|
+
final Multimap<String, String> params = ArrayListMultimap.create();
|
95
|
+
params.put("client_id", clientId);
|
96
|
+
params.put("client_secret", clientSecret);
|
97
|
+
params.put("grant_type", "client_credentials");
|
98
|
+
String response = retryHelper.requestWithRetry(new StringJetty92ResponseEntityReader(READ_TIMEOUT_MILLIS), new Jetty92SingleRequester()
|
99
|
+
{
|
100
|
+
@Override
|
101
|
+
public void requestOnce(HttpClient client, Response.Listener responseListener)
|
102
|
+
{
|
103
|
+
Request request = client.newRequest(identityEndPoint + MarketoRESTEndpoint.ACCESS_TOKEN.getEndpoint()).method(HttpMethod.GET);
|
104
|
+
for (String key : params.keySet()) {
|
105
|
+
for (String value : params.get(key)) {
|
106
|
+
request.param(key, value);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
request.send(responseListener);
|
110
|
+
}
|
111
|
+
|
112
|
+
@Override
|
113
|
+
protected boolean isResponseStatusToRetry(Response response)
|
114
|
+
{
|
115
|
+
return response.getStatus() == 502;
|
116
|
+
}
|
117
|
+
});
|
118
|
+
|
119
|
+
MarketoAccessTokenResponse accessTokenResponse;
|
120
|
+
|
121
|
+
try {
|
122
|
+
accessTokenResponse = OBJECT_MAPPER.readValue(response, MarketoAccessTokenResponse.class);
|
123
|
+
}
|
124
|
+
catch (IOException e) {
|
125
|
+
LOGGER.error("Exception when parse access token response", e);
|
126
|
+
throw new DataException("Can't parse access token response");
|
127
|
+
}
|
128
|
+
if (accessTokenResponse.hasError()) {
|
129
|
+
throw new DataException(accessTokenResponse.getErrorDescription());
|
130
|
+
}
|
131
|
+
return accessTokenResponse.getAccessToken();
|
132
|
+
}
|
133
|
+
|
134
|
+
protected <T> T doGet(final String target, final Map<String, String> headers, final Multimap<String, String> params, Jetty92ResponseReader<T> responseReader)
|
135
|
+
{
|
136
|
+
return doRequest(target, HttpMethod.GET, headers, params, null, responseReader);
|
137
|
+
}
|
138
|
+
|
139
|
+
protected <T> T doPost(final String target, final Map<String, String> headers, final Multimap<String, String> params, final String content, Jetty92ResponseReader<T> responseReader)
|
140
|
+
{
|
141
|
+
StringContentProvider contentProvider = null;
|
142
|
+
if (content != null) {
|
143
|
+
contentProvider = new StringContentProvider(APPLICATION_JSON, content, StandardCharsets.UTF_8);
|
144
|
+
}
|
145
|
+
return doRequest(target, HttpMethod.POST, headers, params, contentProvider, responseReader);
|
146
|
+
}
|
147
|
+
|
148
|
+
protected <T> T doRequest(final String target, final HttpMethod method, final Map<String, String> headers, final Multimap<String, String> params, final ContentProvider contentProvider, Jetty92ResponseReader<T> responseReader)
|
149
|
+
{
|
150
|
+
return retryHelper.requestWithRetry(responseReader, new Jetty92SingleRequester()
|
151
|
+
{
|
152
|
+
@Override
|
153
|
+
public void requestOnce(HttpClient client, Response.Listener responseListener)
|
154
|
+
{
|
155
|
+
Request request = client.newRequest(target).method(method);
|
156
|
+
|
157
|
+
if (headers != null) {
|
158
|
+
for (String key : headers.keySet()) {
|
159
|
+
request.header(key, headers.get(key));
|
160
|
+
}
|
161
|
+
}
|
162
|
+
request.header(AUTHORIZATION_HEADER, "Bearer " + getAccessToken());
|
163
|
+
if (params != null) {
|
164
|
+
for (String key : params.keySet()) {
|
165
|
+
for (String value : params.get(key)) {
|
166
|
+
request.param(key, value);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
if (contentProvider != null) {
|
171
|
+
request.content(contentProvider, APPLICATION_JSON);
|
172
|
+
}
|
173
|
+
request.send(responseListener);
|
174
|
+
}
|
175
|
+
|
176
|
+
@Override
|
177
|
+
protected boolean isResponseStatusToRetry(Response response)
|
178
|
+
{
|
179
|
+
//413 failed job
|
180
|
+
//414 failed job
|
181
|
+
//502 retry
|
182
|
+
return response.getStatus() / 4 != 100;
|
183
|
+
}
|
184
|
+
|
185
|
+
@Override
|
186
|
+
protected boolean isExceptionToRetry(Exception exception)
|
187
|
+
{
|
188
|
+
if (exception instanceof ExecutionException) {
|
189
|
+
this.toRetry((Exception) exception.getCause());
|
190
|
+
}
|
191
|
+
if (exception instanceof MarketoAPIException) {
|
192
|
+
//Retry Authenticate Exception
|
193
|
+
MarketoError error = ((MarketoAPIException) exception).getMarketoErrors().get(0);
|
194
|
+
String code = error.getCode();
|
195
|
+
switch (code) {
|
196
|
+
case "602":
|
197
|
+
renewAccessToken();
|
198
|
+
return true;
|
199
|
+
case "606":
|
200
|
+
try {
|
201
|
+
Thread.sleep(marketoLimitIntervalMilis);
|
202
|
+
}
|
203
|
+
catch (InterruptedException e) {
|
204
|
+
LOGGER.error("Encounter exception when waiting for interval limit", e);
|
205
|
+
throw new DataException("Exception when wait for interval limit");
|
206
|
+
}
|
207
|
+
return true;
|
208
|
+
case "615":
|
209
|
+
return true;
|
210
|
+
default:
|
211
|
+
return false;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
return exception instanceof TimeoutException || exception instanceof SocketTimeoutException || super.isExceptionToRetry(exception);
|
215
|
+
}
|
216
|
+
});
|
217
|
+
}
|
218
|
+
|
219
|
+
@Override
|
220
|
+
public void close()
|
221
|
+
{
|
222
|
+
if (retryHelper != null) {
|
223
|
+
retryHelper.close();
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
@@ -0,0 +1,69 @@
|
|
1
|
+
package org.embulk.input.marketo.rest;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.core.type.TypeReference;
|
4
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
+
import com.fasterxml.jackson.databind.ObjectReader;
|
6
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
7
|
+
import com.google.common.io.CharStreams;
|
8
|
+
import org.eclipse.jetty.client.api.Response;
|
9
|
+
import org.eclipse.jetty.client.util.InputStreamResponseListener;
|
10
|
+
import org.eclipse.jetty.http.HttpHeader;
|
11
|
+
import org.embulk.input.marketo.exception.MarketoAPIException;
|
12
|
+
import org.embulk.input.marketo.model.MarketoResponse;
|
13
|
+
import org.embulk.util.retryhelper.jetty92.Jetty92ResponseReader;
|
14
|
+
|
15
|
+
import java.io.InputStream;
|
16
|
+
import java.io.InputStreamReader;
|
17
|
+
import java.util.concurrent.TimeUnit;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Created by tai.khuu on 9/5/17.
|
21
|
+
*/
|
22
|
+
public class MarketoFileResponseEntityReader implements Jetty92ResponseReader<InputStream>
|
23
|
+
{
|
24
|
+
private static final ObjectReader OBJECT_READER = new ObjectMapper().readerFor(new TypeReference<MarketoResponse<ObjectNode>>(){ });
|
25
|
+
|
26
|
+
private InputStreamResponseListener listener;
|
27
|
+
|
28
|
+
private long timeout;
|
29
|
+
|
30
|
+
public MarketoFileResponseEntityReader(long timeout)
|
31
|
+
{
|
32
|
+
this.listener = new InputStreamResponseListener();
|
33
|
+
this.timeout = timeout;
|
34
|
+
}
|
35
|
+
|
36
|
+
@Override
|
37
|
+
public Response.Listener getListener()
|
38
|
+
{
|
39
|
+
return this.listener;
|
40
|
+
}
|
41
|
+
|
42
|
+
@Override
|
43
|
+
public Response getResponse() throws Exception
|
44
|
+
{
|
45
|
+
return this.listener.get(timeout, TimeUnit.MILLISECONDS);
|
46
|
+
}
|
47
|
+
|
48
|
+
@Override
|
49
|
+
public InputStream readResponseContent() throws Exception
|
50
|
+
{
|
51
|
+
if (!getResponse().getHeaders().getField(HttpHeader.CONTENT_TYPE).getValue().equals("text/csv")) {
|
52
|
+
String errorString = readResponseContentInString();
|
53
|
+
|
54
|
+
MarketoResponse<ObjectNode> errorResponse = OBJECT_READER.readValue(errorString);
|
55
|
+
if (!errorResponse.isSuccess()) {
|
56
|
+
throw new MarketoAPIException(errorResponse.getErrors());
|
57
|
+
}
|
58
|
+
}
|
59
|
+
return this.listener.getInputStream();
|
60
|
+
}
|
61
|
+
|
62
|
+
@Override
|
63
|
+
public String readResponseContentInString() throws Exception
|
64
|
+
{
|
65
|
+
try (InputStreamReader inputStreamReader = new InputStreamReader(this.listener.getInputStream())) {
|
66
|
+
return CharStreams.toString(inputStreamReader);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
package org.embulk.input.marketo.rest;
|
2
|
+
|
3
|
+
import org.apache.commons.lang3.text.StrSubstitutor;
|
4
|
+
|
5
|
+
import java.util.Map;
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Created by tai.khuu on 9/5/17.
|
9
|
+
*/
|
10
|
+
public enum MarketoRESTEndpoint
|
11
|
+
{
|
12
|
+
ACCESS_TOKEN("/oauth/token"),
|
13
|
+
CREATE_LEAD_EXTRACT("/bulk/v1/leads/export/create.json"),
|
14
|
+
CREATE_ACTIVITY_EXTRACT("/bulk/v1/activities/export/create.json"),
|
15
|
+
DESCRIBE_LEAD("/rest/v1/leads/describe.json"),
|
16
|
+
START_LEAD_EXPORT_JOB("/bulk/v1/leads/export/${export_id}/enqueue.json"),
|
17
|
+
START_ACTIVITY_EXPORT_JOB("/bulk/v1/activities/export/${export_id}/enqueue.json"),
|
18
|
+
GET_ACTIVITY_EXPORT_STATUS("/bulk/v1/activities/export/${export_id}/status.json"),
|
19
|
+
GET_LEAD_EXPORT_STATUS("/bulk/v1/leads/export/${export_id}/status.json"),
|
20
|
+
GET_LEAD_EXPORT_RESULT("/bulk/v1/leads/export/${export_id}/file.json"),
|
21
|
+
GET_ACTIVITY_EXPORT_RESULT("/bulk/v1/activities/export/${export_id}/file.json"),
|
22
|
+
GET_LISTS("/rest/v1/lists.json"),
|
23
|
+
GET_LEADS_BY_LIST("/rest/v1/lists/${list_id}/leads.json"),
|
24
|
+
GET_PROGRAMS("/rest/asset/v1/programs.json"),
|
25
|
+
GET_LEADS_BY_PROGRAM("/rest/v1/leads/programs/${program_id}.json"),
|
26
|
+
GET_CAMPAIGN("/rest/v1/campaigns.json");
|
27
|
+
|
28
|
+
private String endpoint;
|
29
|
+
|
30
|
+
MarketoRESTEndpoint(String endpoint)
|
31
|
+
{
|
32
|
+
this.endpoint = endpoint;
|
33
|
+
}
|
34
|
+
|
35
|
+
public String getEndpoint()
|
36
|
+
{
|
37
|
+
return endpoint;
|
38
|
+
}
|
39
|
+
|
40
|
+
public String getEndpoint(Map<String, String> pathParams)
|
41
|
+
{
|
42
|
+
return StrSubstitutor.replace(endpoint, pathParams);
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
package org.embulk.input.marketo.rest;
|
2
|
+
|
3
|
+
import com.fasterxml.jackson.databind.DeserializationFeature;
|
4
|
+
import com.fasterxml.jackson.databind.JavaType;
|
5
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
6
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
7
|
+
import com.google.common.io.CharStreams;
|
8
|
+
import org.eclipse.jetty.client.api.Response;
|
9
|
+
import org.eclipse.jetty.client.util.InputStreamResponseListener;
|
10
|
+
import org.embulk.input.marketo.exception.MarketoAPIException;
|
11
|
+
import org.embulk.input.marketo.model.MarketoResponse;
|
12
|
+
import org.embulk.spi.DataException;
|
13
|
+
import org.embulk.spi.Exec;
|
14
|
+
import org.embulk.util.retryhelper.jetty92.Jetty92ResponseReader;
|
15
|
+
import org.slf4j.Logger;
|
16
|
+
|
17
|
+
import java.io.IOException;
|
18
|
+
import java.io.InputStream;
|
19
|
+
import java.io.InputStreamReader;
|
20
|
+
import java.nio.charset.StandardCharsets;
|
21
|
+
import java.util.concurrent.TimeUnit;
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Created by tai.khuu on 9/1/17.
|
25
|
+
*/
|
26
|
+
public class MarketoResponseJetty92EntityReader<T> implements Jetty92ResponseReader<MarketoResponse<T>>
|
27
|
+
{
|
28
|
+
private InputStreamResponseListener listener;
|
29
|
+
|
30
|
+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
31
|
+
|
32
|
+
private static final Logger LOGGER = Exec.getLogger(MarketoResponseJetty92EntityReader.class);
|
33
|
+
private Long timeout;
|
34
|
+
|
35
|
+
private JavaType javaType;
|
36
|
+
|
37
|
+
public MarketoResponseJetty92EntityReader(long timeout)
|
38
|
+
{
|
39
|
+
this.listener = new InputStreamResponseListener();
|
40
|
+
this.timeout = timeout;
|
41
|
+
javaType = OBJECT_MAPPER.getTypeFactory().constructParametrizedType(MarketoResponse.class, MarketoResponse.class, ObjectNode.class);
|
42
|
+
}
|
43
|
+
|
44
|
+
public MarketoResponseJetty92EntityReader(long timeout, Class<T> resultClass)
|
45
|
+
{
|
46
|
+
this.listener = new InputStreamResponseListener();
|
47
|
+
this.timeout = timeout;
|
48
|
+
this.javaType = OBJECT_MAPPER.getTypeFactory().constructParametrizedType(MarketoResponse.class, MarketoResponse.class, resultClass);
|
49
|
+
}
|
50
|
+
|
51
|
+
@Override
|
52
|
+
public Response.Listener getListener()
|
53
|
+
{
|
54
|
+
return listener;
|
55
|
+
}
|
56
|
+
|
57
|
+
@Override
|
58
|
+
public Response getResponse() throws Exception
|
59
|
+
{
|
60
|
+
return this.listener.get(timeout, TimeUnit.MILLISECONDS);
|
61
|
+
}
|
62
|
+
|
63
|
+
@Override
|
64
|
+
public MarketoResponse<T> readResponseContent() throws Exception
|
65
|
+
{
|
66
|
+
String response = readResponseContentInString();
|
67
|
+
try {
|
68
|
+
MarketoResponse<T> marketoResponse = OBJECT_MAPPER.readValue(response, javaType);
|
69
|
+
if (!marketoResponse.isSuccess()) {
|
70
|
+
throw new MarketoAPIException(marketoResponse.getErrors());
|
71
|
+
}
|
72
|
+
return marketoResponse;
|
73
|
+
}
|
74
|
+
catch (IOException ex) {
|
75
|
+
LOGGER.error("Can't parse json content", ex);
|
76
|
+
throw new DataException("Exception when parse json content");
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
@Override
|
81
|
+
public String readResponseContentInString() throws Exception
|
82
|
+
{
|
83
|
+
InputStream inputStream = this.listener.getInputStream();
|
84
|
+
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
|
85
|
+
return CharStreams.toString(inputStreamReader);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|