-
Notifications
You must be signed in to change notification settings - Fork 5
/
ApplyLabelFunction.java
101 lines (73 loc) · 2.8 KB
/
ApplyLabelFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.takipi.udf.label;
import java.net.HttpURLConnection;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.takipi.api.client.ApiClient;
import com.takipi.api.client.request.event.EventModifyLabelsRequest;
import com.takipi.api.client.request.label.CreateLabelRequest;
import com.takipi.api.client.result.EmptyResult;
import com.takipi.api.core.url.UrlClient.Response;
import com.takipi.udf.ContextArgs;
import com.takipi.udf.input.Input;
public class ApplyLabelFunction {
public static String validateInput(String rawInput) {
return getLabelInput(rawInput).toString();
}
private static LabelInput getLabelInput(String rawInput) {
System.out.println("validateInput rawInput:" + rawInput);
if (Strings.isNullOrEmpty(rawInput)) {
throw new IllegalArgumentException("Input is empty");
}
LabelInput input;
try {
input = LabelInput.of(rawInput);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
if (StringUtils.isEmpty(input.label)) {
throw new IllegalArgumentException("Label name can't be empty");
}
return input;
}
public static void execute(String rawContextArgs, String rawInput) {
LabelInput input = getLabelInput(rawInput);
ContextArgs args = (new Gson()).fromJson(rawContextArgs, ContextArgs.class);
System.out.println("execute context: " + rawContextArgs);
if (!args.validate()) {
throw new IllegalArgumentException("Bad context args - " + rawContextArgs);
}
if (!args.eventValidate()) {
return;
}
ApiClient apiClient = args.apiClient();
CreateLabelRequest createLabel = CreateLabelRequest.newBuilder().setServiceId(args.serviceId).setName(input.label)
.build();
Response<EmptyResult> createResult = apiClient.post(createLabel);
if ((createResult.isBadResponse()) && (createResult.responseCode != HttpURLConnection.HTTP_CONFLICT)) {
throw new IllegalStateException("Can't create label " + input);
}
EventModifyLabelsRequest addLabel = EventModifyLabelsRequest.newBuilder().setServiceId(args.serviceId)
.setEventId(args.eventId).addLabel(input.label).build();
Response<EmptyResult> addResult = apiClient.post(addLabel);
if (addResult.isBadResponse()) {
throw new IllegalStateException("Can't apply label " + input + " to event " + args.eventId);
}
}
static class LabelInput extends Input {
public String label;
private LabelInput(String raw) {
super(raw);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Label name - ");
builder.append(label);
return builder.toString();
}
static LabelInput of(String raw) {
return new LabelInput(raw);
}
}
}