|
1 | 1 | package org.typesense.api; |
2 | 2 |
|
3 | | -import org.typesense.api.utils.URLEncoding; |
4 | | -import org.typesense.model.AnalyticsRuleSchema; |
5 | | -import org.typesense.model.AnalyticsRuleUpsertSchema; |
6 | | -import org.typesense.model.AnalyticsRulesRetrieveSchema; |
| 3 | +import org.typesense.model.AnalyticsRule; |
| 4 | +import org.typesense.model.AnalyticsRuleCreate; |
| 5 | +import java.util.List; |
7 | 6 |
|
8 | 7 | public class AnalyticsRules { |
9 | 8 |
|
10 | 9 | private final ApiCall apiCall; |
| 10 | + private final AnalyticsRuleSerializer serializer; |
11 | 11 | public final static String RESOURCE_PATH = "/analytics/rules"; |
12 | 12 |
|
13 | 13 | public AnalyticsRules(ApiCall apiCall) { |
14 | 14 | this.apiCall = apiCall; |
| 15 | + this.serializer = new AnalyticsRuleSerializer(); |
| 16 | + } |
| 17 | + |
| 18 | + public AnalyticsRules(ApiCall apiCall, AnalyticsRuleSerializer serializer) { |
| 19 | + this.apiCall = apiCall; |
| 20 | + this.serializer = serializer; |
15 | 21 | } |
16 | 22 |
|
17 | | - public AnalyticsRuleSchema create(AnalyticsRuleSchema rule) throws Exception { |
18 | | - return this.apiCall.post(RESOURCE_PATH, rule, null, AnalyticsRuleSchema.class); |
| 23 | + public AnalyticsRulesResponse create(List<AnalyticsRuleCreate> rules) throws Exception { |
| 24 | + String response = this.apiCall.post(RESOURCE_PATH, rules, null, String.class); |
| 25 | + return parseCreateResponse(response); |
19 | 26 | } |
20 | 27 |
|
21 | | - public AnalyticsRuleSchema upsert(String name, AnalyticsRuleUpsertSchema rule) throws Exception { |
22 | | - return this.apiCall.put(RESOURCE_PATH + "/" + URLEncoding.encodeURIComponent(name), rule, null, |
23 | | - AnalyticsRuleSchema.class); |
| 28 | + public List<AnalyticsRule> retrieve() throws Exception { |
| 29 | + String response = this.apiCall.get(RESOURCE_PATH, null, String.class); |
| 30 | + return parseRetrieveResponse(response); |
24 | 31 | } |
25 | 32 |
|
26 | | - public AnalyticsRulesRetrieveSchema retrieve() throws Exception { |
27 | | - return this.apiCall.get(RESOURCE_PATH, null, AnalyticsRulesRetrieveSchema.class); |
| 33 | + /** |
| 34 | + * Parse the create response which can be either a single AnalyticsRule or an array |
| 35 | + */ |
| 36 | + private AnalyticsRulesResponse parseCreateResponse(String jsonResponse) throws Exception { |
| 37 | + List<AnalyticsRule> rules = serializer.parseListFromJson(jsonResponse); |
| 38 | + |
| 39 | + for (AnalyticsRule rule : rules) { |
| 40 | + if (rule.getName() == null) { |
| 41 | + throw new RuntimeException("Analytics rule creation failed: rule name is null"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return new AnalyticsRulesResponse(rules); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Parse the retrieve response which is always an array of AnalyticsRule objects |
| 50 | + */ |
| 51 | + private List<AnalyticsRule> parseRetrieveResponse(String jsonResponse) throws Exception { |
| 52 | + List<AnalyticsRule> rules = serializer.parseListFromJson(jsonResponse); |
| 53 | + |
| 54 | + return rules; |
28 | 55 | } |
29 | 56 |
|
| 57 | + /** |
| 58 | + * Response wrapper for analytics rules operations |
| 59 | + */ |
| 60 | + public static class AnalyticsRulesResponse { |
| 61 | + private final List<AnalyticsRule> rules; |
| 62 | + |
| 63 | + public AnalyticsRulesResponse(List<AnalyticsRule> rules) { |
| 64 | + this.rules = rules; |
| 65 | + } |
| 66 | + |
| 67 | + public List<AnalyticsRule> getRules() { |
| 68 | + return rules; |
| 69 | + } |
| 70 | + |
| 71 | + public AnalyticsRule getFirstRule() { |
| 72 | + return rules.isEmpty() ? null : rules.get(0); |
| 73 | + } |
| 74 | + |
| 75 | + public int getCount() { |
| 76 | + return rules.size(); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isEmpty() { |
| 80 | + return rules.isEmpty(); |
| 81 | + } |
| 82 | + } |
30 | 83 | } |
0 commit comments