Skip to content

Commit

Permalink
Refactor Upload interface to clarify upload type
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Nov 2, 2023
1 parent 612531a commit 2a1d077
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
33 changes: 27 additions & 6 deletions src/main/kotlin/de/cyface/uploader/DefaultUploader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -198,7 +219,7 @@ class DefaultUploader(private val apiEndpoint: String) : Uploader {
}*/
}

override fun endpoint(): URL {
override fun measurementsEndpoint(): URL {
return URL(returnUrlWithTrailingSlash(apiEndpoint) + "measurements")
}

Expand Down
46 changes: 33 additions & 13 deletions src/main/kotlin/de/cyface/uploader/Uploader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a1d077

Please sign in to comment.