From 2a1d077a56cff00d5d26d1dcd3076640c5afe939 Mon Sep 17 00:00:00 2001 From: Armin Date: Thu, 2 Nov 2023 08:52:37 +0100 Subject: [PATCH] Refactor Upload interface to clarify upload type --- .../de/cyface/uploader/DefaultUploader.kt | 33 ++++++++++--- .../kotlin/de/cyface/uploader/Uploader.kt | 46 +++++++++++++------ 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt b/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt index 97666fb..db22f07 100644 --- a/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt +++ b/src/main/kotlin/de/cyface/uploader/DefaultUploader.kt @@ -63,18 +63,39 @@ import javax.net.ssl.SSLException /** * Implementation of the [Uploader]. * - * To use this interface just call [DefaultUploader.upload] with an authentication token. + * To use this interface just call [DefaultUploader.uploadMeasurement] or [DefaultUploader.uploadAttachment]. * * @author Armin Schnabel - * @version 1.1.0 + * @version 2.0.0 * @since 1.0.0 * @property apiEndpoint An API endpoint running a Cyface data collector service, like `https://some.url/api/v3` */ class DefaultUploader(private val apiEndpoint: String) : Uploader { - @Suppress("unused", "CyclomaticComplexMethod", "LongMethod") - override // Part of the API - fun upload( + @Suppress("unused", "CyclomaticComplexMethod", "LongMethod") // Part of the API + override fun uploadMeasurement( + jwtToken: String, + metaData: RequestMetaData, + file: File, + progressListener: UploadProgressListener + ): Result { + val endpoint = measurementsEndpoint() + return uploadFile(jwtToken, metaData, file, endpoint, progressListener) + } + + override fun uploadAttachment( + jwtToken: String, + metaData: RequestMetaData, + measurementId: Long, + file: File, + progressListener: UploadProgressListener + ): Result { + val endpoint = filesEndpoint(measurementId) + return uploadFile(jwtToken, metaData, file, endpoint, progressListener) + } + + @Throws(UploadFailed::class) + private fun uploadFile( jwtToken: String, metaData: RequestMetaData, file: File, @@ -198,7 +219,7 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader { }*/ } - override fun endpoint(): URL { + override fun measurementsEndpoint(): URL { return URL(returnUrlWithTrailingSlash(apiEndpoint) + "measurements") } diff --git a/src/main/kotlin/de/cyface/uploader/Uploader.kt b/src/main/kotlin/de/cyface/uploader/Uploader.kt index d4d7f01..9bc94bb 100644 --- a/src/main/kotlin/de/cyface/uploader/Uploader.kt +++ b/src/main/kotlin/de/cyface/uploader/Uploader.kt @@ -28,44 +28,64 @@ import java.net.URL * Interface for uploading files to a Cyface Data Collector. * * @author Armin Schnabel - * @version 1.1.0 + * @version 2.0.0 * @since 1.0.0 */ interface Uploader { /** - * Uploads the provided file to the server available at the [endpoint]. + * Uploads the provided measurement file to the server. * * @param jwtToken A String in the format "eyXyz123***". - * @param metaData The [RequestMetaData] required for the Multipart request. + * @param metaData The [RequestMetaData] required for the upload request. * @param file The data file to upload via this post request. - * @param endpoint The endpoint to upload the data to. * @param progressListener The [UploadProgressListener] to be informed about the upload progress. * @throws UploadFailed when an error occurred. * @return [Result.UPLOAD_SUCCESSFUL] when successful and [Result.UPLOAD_SKIPPED] when the server is * not interested in the data. */ @Throws(UploadFailed::class) - @Suppress("unused") // Part of the API - fun upload( + fun uploadMeasurement( jwtToken: String, metaData: RequestMetaData, file: File, - endpoint: URL, progressListener: UploadProgressListener ): Result /** - * @return the endpoint which will be used to upload the core measurement file. - * @throws MalformedURLException if the endpoint address provided is malformed. + * Uploads the provided attachment file to the server, associated with a specific measurement. + * + * @param jwtToken A String in the format "eyXyz123***". + * @param metaData The [RequestMetaData] required for the upload request. + * @param measurementId The id of the measurement the file is attached to. + * @param file The attachment file to upload via this post request. + * @param progressListener The [UploadProgressListener] to be informed about the upload progress. + * @throws UploadFailed when an error occurred. + * @return [Result.UPLOAD_SUCCESSFUL] when successful and [Result.UPLOAD_SKIPPED] when the server is + * not interested in the data. + */ + @Throws(UploadFailed::class) + fun uploadAttachment( + jwtToken: String, + metaData: RequestMetaData, + measurementId: Long, + file: File, + progressListener: UploadProgressListener + ): Result + + /** + * @return The URL endpoint used for uploading measurement files. + * @throws MalformedURLException if the endpoint address is malformed. */ @Throws(MalformedURLException::class) - fun endpoint(): URL + fun measurementsEndpoint(): URL /** - * @param measurementId The id of the measurement the files are captured for. - * @return the endpoint which will be used to upload the attached files of a measurement. - * @throws MalformedURLException if the endpoint address provided is malformed. + * Determines the URL endpoint for uploading attachment files associated with a specific measurement. + * + * @param measurementId The ID of the measurement the files are attached to. + * @return The URL endpoint used for uploading attachment files. + * @throws MalformedURLException if the endpoint address is malformed. */ @Throws(MalformedURLException::class) fun filesEndpoint(measurementId: Long): URL