Skip to content

Commit 05cd742

Browse files
committed
added validation
1 parent 4250a33 commit 05cd742

21 files changed

+415
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
2424

25+
*.xml
2526
# project files
2627
*.iml
2728
.idea

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,28 @@
7676
<groupId>org.slf4j</groupId>
7777
<artifactId>slf4j-log4j12</artifactId>
7878
</exclusion>
79+
<exclusion>
80+
<groupId>org.apache.httpcomponents</groupId>
81+
<artifactId>httpclient</artifactId>
82+
</exclusion>
83+
<exclusion>
84+
<groupId>org.apache.httpcomponents</groupId>
85+
<artifactId>httpcore</artifactId>
86+
</exclusion>
7987
</exclusions>
8088
</dependency>
89+
<dependency>
90+
<groupId>org.apache.httpcomponents</groupId>
91+
<artifactId>httpclient</artifactId>
92+
<version>4.5.2</version>
93+
<scope>compile</scope>
94+
</dependency>
95+
<dependency>
96+
<groupId>org.apache.httpcomponents</groupId>
97+
<artifactId>httpcore</artifactId>
98+
<version>4.4.4</version>
99+
<scope>compile</scope>
100+
</dependency>
81101
<!-- should match the gson version used by cdap-->
82102
<dependency>
83103
<groupId>com.google.code.gson</groupId>

src/main/java/io/cdap/plugin/sendgrid/common/SendGridClient.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.base.Strings;
1919
import com.google.gson.Gson;
2020
import com.google.gson.GsonBuilder;
21+
import com.google.gson.reflect.TypeToken;
2122
import com.sendgrid.Method;
2223
import com.sendgrid.Request;
2324
import com.sendgrid.Response;
@@ -26,13 +27,14 @@
2627
import io.cdap.plugin.sendgrid.common.helpers.ObjectInfo;
2728
import io.cdap.plugin.sendgrid.common.objects.BasicResult;
2829
import io.cdap.plugin.sendgrid.common.objects.SendGridAuthType;
29-
import io.cdap.plugin.sendgrid.source.batch.SendGridBatchSourceConfig;
30+
import io.cdap.plugin.sendgrid.source.batch.SendGridBatchConfig;
3031

3132
import java.io.IOException;
3233
import java.lang.reflect.ParameterizedType;
3334
import java.lang.reflect.Type;
3435
import java.util.ArrayList;
3536
import java.util.Base64;
37+
import java.util.HashMap;
3638
import java.util.List;
3739
import java.util.Map;
3840
import java.util.stream.Collectors;
@@ -43,6 +45,7 @@
4345
* SendGrid Client
4446
*/
4547
public class SendGridClient {
48+
private static final String CONNECTION_CHECK_ENDPOINT = "alerts";
4649

4750
/**
4851
* Extended version of the original SendGrid API wrapper with added support of basic auth
@@ -77,7 +80,7 @@ private SendGridClient() {
7780
gson = new GsonBuilder().create();
7881
}
7982

80-
public SendGridClient(SendGridBatchSourceConfig config) {
83+
public SendGridClient(SendGridBatchConfig config) {
8184
this();
8285
if (config.getAuthType() == SendGridAuthType.API) {
8386
sendGrid = new SendGridAPIClient(config.getSendGridApiKey());
@@ -123,12 +126,37 @@ private String makeApiRequest(Method method, String endpoint, @Nullable Map<Stri
123126
try {
124127
response = sendGrid.api(request);
125128
} catch (IOException e) {
126-
throw new IOException(String.format("Request to SendGrid API \"%s\"", endpoint), e);
129+
String serverMessage = String.format("Request to SendGrid API \"%s\"", endpoint);
130+
// Parse error response from the server
131+
if (e.getMessage().contains("Body:")) {
132+
String[] messages = e.getMessage().split("Body:");
133+
HashMap<String, List<HashMap<String, String>>> errors = gson.fromJson(
134+
messages[1],
135+
new TypeToken<HashMap<String, List<HashMap<String, String>>>>() { }.getType()
136+
);
137+
if (errors.containsKey("errors")) {
138+
String description = errors.get("errors").stream()
139+
.filter(x -> x.containsKey("message"))
140+
.map(x -> x.get("message"))
141+
.collect(Collectors.joining(";"));
142+
143+
serverMessage = String.format("%s, API response: %s", messages[0], description);
144+
}
145+
}
146+
throw new IOException(serverMessage, e);
127147
}
128148

129149
return response.getBody();
130150
}
131151

152+
/**
153+
* Checks connection to the service by testing API endpoint, in case
154+
* of exception would be generated {@link IOException}
155+
*/
156+
public void checkConnection() throws IOException {
157+
makeApiRequest(Method.GET, CONNECTION_CHECK_ENDPOINT, null);
158+
}
159+
132160
/**
133161
* Verify all incoming arguments for the query object
134162
*

src/main/java/io/cdap/plugin/sendgrid/common/config/BaseSourceConfig.java renamed to src/main/java/io/cdap/plugin/sendgrid/common/config/BaseConfig.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* Provides all required configuration for reading SendGrid information
4141
*/
42-
public class BaseSourceConfig extends ReferencePluginConfig {
42+
public class BaseConfig extends ReferencePluginConfig {
4343
public static final String PROPERTY_AUTH_TYPE = "authType";
4444
public static final String PROPERTY_SENDGRID_API_KEY = "sendGridApiKey";
4545
public static final String PROPERTY_AUTH_USERNAME = "username";
@@ -52,8 +52,8 @@ public class BaseSourceConfig extends ReferencePluginConfig {
5252

5353
public static final String PROPERTY_DATA_SOURCE_FIELDS = "dataSourceFields";
5454
public static final String PROPERTY_STAT_CATEGORIES = "statCategories";
55-
public static final String PROPERTY_START_DATE = "startDate";
56-
public static final String PROPERTY_END_DATE = "endDate";
55+
public static final String PROPERTY_START_DATE = "start_date";
56+
public static final String PROPERTY_END_DATE = "end_date";
5757

5858
@Name(PROPERTY_AUTH_TYPE)
5959
@Description("The way, how user would like to be authenticated to the SendGrid account")
@@ -134,10 +134,17 @@ public class BaseSourceConfig extends ReferencePluginConfig {
134134
*
135135
* @param referenceName uniquely identify source/sink for lineage, annotating metadata, etc.
136136
*/
137-
public BaseSourceConfig(String referenceName) {
137+
public BaseConfig(String referenceName) {
138138
super(referenceName);
139139
}
140140

141+
/**
142+
* Validate configuration for the issues
143+
*/
144+
protected void validate(FailureCollector failureCollector) {
145+
new BaseConfigValidator(failureCollector, this).validate();
146+
}
147+
141148
/**
142149
* Fetches all fields selected by the user
143150
*/
@@ -223,10 +230,6 @@ public Map<String, String> getRequestArguments() {
223230
return builder.build();
224231
}
225232

226-
public void validate(FailureCollector failureCollector) {
227-
228-
}
229-
230233
/**
231234
* Client authentication way
232235
*/
@@ -271,4 +274,11 @@ public String getStartDate() {
271274
public String getEndDate() {
272275
return endDate;
273276
}
277+
278+
public List<String> getDataSourceTypes() {
279+
if (!Strings.isNullOrEmpty(dataSourceTypes)) {
280+
return Arrays.asList(dataSourceTypes.split(","));
281+
}
282+
return Collections.emptyList();
283+
}
274284
}

0 commit comments

Comments
 (0)