Skip to content

Commit 1ce782a

Browse files
ObservationsAPI implementation
1 parent f9252e9 commit 1ce782a

12 files changed

Lines changed: 641 additions & 18 deletions

‎src/main/java/org/connectedsystems/ConnectedSystemsAPI.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ConnectedSystemsAPI {
1111
protected final String authorizationToken;
1212
protected final SystemsAPI systemsAPI;
1313
protected final DataStreamsAPI dataStreamsAPI;
14+
protected final ObservationsAPI observationsAPI;
1415

1516
/**
1617
* Constructs a ConnectedSystemsAPI object with the given API root and authentication token.
@@ -23,6 +24,7 @@ public ConnectedSystemsAPI(String apiRoot, String authenticationToken) {
2324
this.authorizationToken = authenticationToken;
2425
this.systemsAPI = new SystemsAPI(this);
2526
this.dataStreamsAPI = new DataStreamsAPI(this);
27+
this.observationsAPI = new ObservationsAPI(this);
2628
}
2729

2830
/**
@@ -58,4 +60,8 @@ public SystemsAPI getSystemsAPI() {
5860
public DataStreamsAPI getDataStreamsAPI() {
5961
return dataStreamsAPI;
6062
}
63+
64+
public ObservationsAPI getObservationsAPI() {
65+
return observationsAPI;
66+
}
6167
}

‎src/main/java/org/connectedsystems/GsonFactory.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import net.opengis.swe.v20.DataComponent;
66
import net.opengis.swe.v20.DataRecord;
77
import org.connectedsystems.datamodels.FeatureType;
8-
import org.connectedsystems.serialization.DataComponentSerializer;
9-
import org.connectedsystems.serialization.DataRecordSerializer;
10-
import org.connectedsystems.serialization.FeatureTypeSerializer;
11-
import org.connectedsystems.serialization.TimeExtentSerializer;
8+
import org.connectedsystems.serialization.*;
129
import org.vast.util.TimeExtent;
1310

1411
public class GsonFactory {
@@ -26,4 +23,20 @@ public class GsonFactory {
2623
private GsonFactory() {
2724
// Private constructor to prevent instantiation
2825
}
26+
27+
/**
28+
* Used for serialization of DataBlock objects, which require a schema to be deserialized.
29+
*
30+
* @param resultSchema The schema to use for deserialization of DataBlock objects.
31+
* @return A Gson instance with the necessary TypeAdapters registered.
32+
*/
33+
public static Gson createGson(DataComponent resultSchema) {
34+
return new GsonBuilder()
35+
.registerTypeAdapter(FeatureType.class, new FeatureTypeSerializer())
36+
.registerTypeAdapter(TimeExtent.class, new TimeExtentSerializer())
37+
.registerTypeAdapter(DataComponent.class, new DataComponentSerializer())
38+
.registerTypeAdapter(DataRecord.class, new DataRecordSerializer())
39+
.registerTypeAdapterFactory(new DataBlockTypeAdapterFactory(resultSchema))
40+
.create();
41+
}
2942
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package org.connectedsystems;
2+
3+
import net.opengis.swe.v20.DataComponent;
4+
import org.connectedsystems.datamodels.ObservationResource;
5+
import org.connectedsystems.datamodels.ObservationSchema;
6+
import org.connectedsystems.net.APIRequest;
7+
import org.connectedsystems.net.APIResponse;
8+
import org.connectedsystems.net.Endpoint;
9+
import org.connectedsystems.net.HttpRequestMethod;
10+
import org.connectedsystems.util.ObservationsOfDataStreamQueryBuilder;
11+
import org.connectedsystems.util.ObservationsQueryBuilder;
12+
13+
import java.io.IOException;
14+
15+
import static org.connectedsystems.GsonFactory.gson;
16+
17+
public class ObservationsAPI {
18+
private final ConnectedSystemsAPI connectedSystemsAPI;
19+
20+
public ObservationsAPI(ConnectedSystemsAPI connectedSystemsAPI) {
21+
this.connectedSystemsAPI = connectedSystemsAPI;
22+
}
23+
24+
/**
25+
* List all observations available from this server endpoint.
26+
* <p>
27+
* Note: This method does not deserialize the observations; it only returns the raw JSON response.
28+
* Use {@link #getObservationsOfDataStream(String, DataComponent)} to deserialize the observations.
29+
*
30+
* @return {@link APIResponse} containing the list of observations.
31+
* @throws IOException if an error occurs while making the API request.
32+
*/
33+
public APIResponse<Void> getObservations() throws IOException {
34+
return getObservations(new ObservationsQueryBuilder());
35+
}
36+
37+
/**
38+
* List all observations available from this server endpoint.
39+
* <p>
40+
* Note: This method does not deserialize the observations; it only returns the raw JSON response.
41+
* Use {@link #getObservationsOfDataStream(String, DataComponent, ObservationsOfDataStreamQueryBuilder)} to deserialize the observations.
42+
*
43+
* @param queryParams {@link ObservationsQueryBuilder} containing the query parameters.
44+
* @return {@link APIResponse} containing the list of observations.
45+
* @throws IOException if an error occurs while making the API request.
46+
*/
47+
public APIResponse<Void> getObservations(ObservationsQueryBuilder queryParams) throws IOException {
48+
APIRequest apiRequest = new APIRequest.APIRequestBuilder()
49+
.setApiRoot(connectedSystemsAPI.apiRoot)
50+
.setResourcePath(Endpoint.OBSERVATIONS_COLLECTION)
51+
.setAuthorizationToken(connectedSystemsAPI.authorizationToken)
52+
.setRequestMethod(HttpRequestMethod.GET)
53+
.setQueryParams(queryParams.getParameters())
54+
.build();
55+
56+
return apiRequest.execute(Void.class);
57+
}
58+
59+
/**
60+
* List all {@link ObservationResource} available from a specific datastream.
61+
*
62+
* @param dataStreamId the ID of the datastream.
63+
* @param resultSchema the schema of the observation result.
64+
* This is used to deserialize the result field of the observation.
65+
* Typically, this comes from {@link ObservationSchema#getResultSchema()} of the datastream.
66+
* @return {@link APIResponse} containing the list of {@link ObservationResource}.
67+
* @throws IOException if an error occurs while making the API request.
68+
*/
69+
public APIResponse<ObservationResource> getObservationsOfDataStream(String dataStreamId, DataComponent resultSchema) throws IOException {
70+
return getObservationsOfDataStream(dataStreamId, resultSchema, new ObservationsOfDataStreamQueryBuilder());
71+
}
72+
73+
/**
74+
* List all {@link ObservationResource} available from a specific datastream.
75+
*
76+
* @param dataStreamId the ID of the datastream.
77+
* @param resultSchema the schema of the observation result.
78+
* This is used to deserialize the result field of the observation.
79+
* Typically, this comes from {@link ObservationSchema#getResultSchema()} of the datastream.
80+
* @param queryParams {@link ObservationsOfDataStreamQueryBuilder} containing the query parameters.
81+
* @return {@link APIResponse} containing the list of {@link ObservationResource}.
82+
* @throws IOException if an error occurs while making the API request.
83+
*/
84+
public APIResponse<ObservationResource> getObservationsOfDataStream(String dataStreamId, DataComponent resultSchema, ObservationsOfDataStreamQueryBuilder queryParams) throws IOException {
85+
var gson = GsonFactory.createGson(resultSchema);
86+
APIRequest apiRequest = new APIRequest.APIRequestBuilder()
87+
.setApiRoot(connectedSystemsAPI.apiRoot)
88+
.setResourcePath(Endpoint.DATA_STREAMS_COLLECTION)
89+
.setResourceId(dataStreamId)
90+
.setSubResourcePath(Endpoint.OBSERVATIONS_COLLECTION)
91+
.setAuthorizationToken(connectedSystemsAPI.authorizationToken)
92+
.setRequestMethod(HttpRequestMethod.GET)
93+
.setQueryParams(queryParams.getParameters())
94+
.build();
95+
96+
return apiRequest.execute(ObservationResource.class, gson);
97+
}
98+
99+
/**
100+
* Add a new observation to an existing datastream.
101+
*
102+
* @param dataStreamId the ID of the datastream.
103+
* @param observationResource the observation to add.
104+
* @return {@link APIResponse} containing the response from the server.
105+
* @throws IOException if an error occurs while making the API request.
106+
*/
107+
public APIResponse<Void> createObservation(String dataStreamId, ObservationResource observationResource) throws IOException {
108+
APIRequest apiRequest = new APIRequest.APIRequestBuilder()
109+
.setApiRoot(connectedSystemsAPI.apiRoot)
110+
.setResourcePath(Endpoint.DATA_STREAMS_COLLECTION)
111+
.setResourceId(dataStreamId)
112+
.setSubResourcePath(Endpoint.OBSERVATIONS_COLLECTION)
113+
.setAuthorizationToken(connectedSystemsAPI.authorizationToken)
114+
.setRequestMethod(HttpRequestMethod.POST)
115+
.setBody(gson.toJson(observationResource))
116+
.build();
117+
118+
return apiRequest.execute(Void.class);
119+
}
120+
121+
/**
122+
* Update an existing observation.
123+
*
124+
* @param observationId the ID of the observation.
125+
* @param observationResource the updated observation.
126+
* @return {@link APIResponse} containing the response from the server.
127+
* @throws IOException if an error occurs while making the API request.
128+
*/
129+
public APIResponse<Void> updateObservation(String observationId, ObservationResource observationResource) throws IOException {
130+
APIRequest apiRequest = new APIRequest.APIRequestBuilder()
131+
.setApiRoot(connectedSystemsAPI.apiRoot)
132+
.setResourcePath(Endpoint.OBSERVATIONS_COLLECTION)
133+
.setResourceId(observationId)
134+
.setAuthorizationToken(connectedSystemsAPI.authorizationToken)
135+
.setRequestMethod(HttpRequestMethod.PUT)
136+
.setBody(gson.toJson(observationResource))
137+
.build();
138+
139+
return apiRequest.execute(Void.class);
140+
}
141+
142+
/**
143+
* Delete an existing observation.
144+
*
145+
* @param observationId the ID of the observation.
146+
* @return {@link APIResponse} containing the response from the server.
147+
* @throws IOException if an error occurs while making the API request.
148+
*/
149+
public APIResponse<Void> deleteObservation(String observationId) throws IOException {
150+
APIRequest apiRequest = new APIRequest.APIRequestBuilder()
151+
.setApiRoot(connectedSystemsAPI.apiRoot)
152+
.setResourcePath(Endpoint.OBSERVATIONS_COLLECTION)
153+
.setResourceId(observationId)
154+
.setAuthorizationToken(connectedSystemsAPI.authorizationToken)
155+
.setRequestMethod(HttpRequestMethod.DELETE)
156+
.build();
157+
158+
return apiRequest.execute(Void.class);
159+
}
160+
}

0 commit comments

Comments
 (0)