-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEventsApi.java
More file actions
executable file
·192 lines (169 loc) · 5.61 KB
/
EventsApi.java
File metadata and controls
executable file
·192 lines (169 loc) · 5.61 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.coscale.sdk.client.events;
import java.io.IOException;
import java.util.List;
import javax.annotation.Nullable;
import com.coscale.sdk.client.ApiClient;
import com.coscale.sdk.client.commons.Msg;
import com.coscale.sdk.client.commons.Options;
import com.fasterxml.jackson.core.type.TypeReference;
/**
* CoScale API client used to create Events and inserting event data.
*
* @author cristi
*
*/
public class EventsApi {
/** ApiClient used to make HTTP requests. */
private final ApiClient api;
/**
* EventsApi constructor.
*
* @param api ApiClient.
*/
public EventsApi(ApiClient api) {
this.api = api;
}
/** Events end point calls */
/**
* all is used to get a list of all events.
*
* @return List<Event>
* @throws IOException
*/
public List<Event> all() throws IOException {
return api.callWithAuth("GET", "/events/", null, new EventListTypeReference());
}
/**
* all is used to get a list of all events.
*
* @param options
* which contain query parameters
* @return List<Event>
* @throws IOException
*/
public List<Event> all(Options options) throws IOException {
String url = "/events/";
url += (options.hasQuery() ? "?" : "&") + options.query();
return api.callWithAuth("GET", url, null, new EventListTypeReference());
}
/**
* get a event by the id.
*
* @param id
* id of the event.
* @return Event
* @throws IOException
*/
public Event get(long id) throws IOException {
return api.callWithAuth("GET", "/events/" + id + '/', null, new EventTypeReference());
}
/**
* Insert a new event.
*
* @param event
* EventInsert containing data for the new event.
* @return EventInsert the event object.
* @throws IOException
*/
public Event insert(EventInsert event) throws IOException {
return api.callWithAuth("POST", "/events/", event, new EventTypeReference());
}
/**
* Delete a event by id.
*
* @param id
* id of the event.
* @return Msg the response message.
* @throws IOException
*/
public Msg delete(long id) throws IOException {
return api.callWithAuth("DELETE", "/events/" + id + '/', null, new MsgTypeReference());
}
/** Events data end point calls. */
/**
* Get data from start to stop for a given event. If timestamp = 0 : use the
* current timestamp. If the timestamp is negative: seconds since the
* current timestamp. The stop time is optional. Entering only a start time
* will give all events from that moment onwards.
*
* @param eventId
* the event id for which the data is requested.
* @param start
* start time.
* @param stop
* stop time.
* @return List<EventData> a list of events data.
* @throws IOException
*/
public List<EventData> allData(Long eventId, @Nullable Long start, @Nullable Long stop)
throws IOException {
String endpoint = "/events/" + eventId + "/data/";
boolean first = true;
if (start != null) {
endpoint += (first ? "?" : "&") + "start=" + start;
first = false;
}
if (stop != null) {
endpoint += (first ? "?" : "&") + "stop=" + stop;
first = false;
}
return api.callWithAuth("GET", endpoint, null, new EventDataListTypeReference());
}
/**
* Get a single Data point by its id.
*
* @param eventId
* the event id.
* @param dataId
* the event data id.
* @return EventData
* @throws IOException
*/
public EventData getData(Long eventId, Long dataId) throws IOException {
return api.callWithAuth("GET", "/events/" + eventId + "/data/get/" + dataId + '/', null,
new EventDataTypeReference());
}
/**
* Create new EventData for a given event.
*
* @param eventId
* the event for which the data will ne inserted.
* @param data
* EventDataInsert the data that will be inserted.
* @return EventData the inserted data.
* @throws IOException
*/
public EventData insertData(Long eventId, EventDataInsert data) throws IOException {
return api.callWithAuth("POST", "/events/" + eventId + "/data/", data,
new EventDataTypeReference());
}
/**
* Delete EventData.
*
* @param eventId
* the event id.
* @param dataId
* the event data id.
* @return Msg response message.
* @throws IOException
*/
public Msg deleteData(Long eventId, Long dataId) throws IOException {
return api.callWithAuth("DELETE", "/events/" + eventId + "/data/" + dataId + '/', null,
new MsgTypeReference());
}
/**
* Copied from the Intellij code analysis:
* A static inner class does not keep an implicit reference to its enclosing instance.
* This prevents a common cause of memory leaks and uses less memory per instance of the class.
*/
private static class EventListTypeReference extends TypeReference<List<Event>> {
}
private static class EventTypeReference extends TypeReference<Event> {
}
private static class MsgTypeReference extends TypeReference<Msg> {
}
private static class EventDataListTypeReference extends TypeReference<List<EventData>> {
}
private static class EventDataTypeReference extends TypeReference<EventData> {
}
}