|
| 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