RetrofitUploadKit

Maven Central GitHub release (latest by date)

RetrofitUploadKit is a Kotlin-based library that simplifies the file upload process using Retrofit. This library provides a flexible DSL for uploading files as multipart or byte arrays and is designed to support both single and multiple file uploads with minimal configuration.

Features

Installation

To include RetrofitUploadKit in your project, add the following dependency to your build.gradle file:

dependencies {
    implementation 'io.github.dakshsemwal:retrofitfileuploadkit:1.0.2'
}

Usage

Ensure you have Hilt initialized in your application. Then integrate RetrofitFileUploadKit as follows:

Providing Base URL

    @Provides
    @Singleton
    fun provideBaseUrlProvider(): BaseUrlProvider = object : BaseUrlProvider { 
        override val baseUrl: String get() = "https://your.api.com" // Replace with your base URL
    }

Setting Up Dependencies

    @Singleton
    @Provides
    fun provideFileUploadSource(fileUploadManager: FileUploadManager): FileUploadDataSource =
        FileUploadDataSourceImpl(fileUploadManager)

Injecting the Manager

Single File Upload

class FileUploadDataSourceImpl @Inject constructor(
    private val fileUploadManager: FileUploadManager
) : FileUploadDataSource {

    override suspend fun uploadFile(
        url: String,
        file: File,
        name: String,
        isMultipart: Boolean
    ): Resource<YOUR_DATA_CLASS> {
        val type = object : TypeToken<YOUR_DATA_CLASS>() {}.type
        return fileUploadManager.uploadFile(
            type,
            url,
            file,
            name,
            isMultipart
        )
    }
}

Multiple File Upload

// List of files to be uploaded
val files: List<File> = listOf(file1, file2, file3)

// Iterate and upload each file
files.forEach { file ->
    fileUploadManager.uploadFile(
        url = "/upload",
        file = file,
        name = "file",
        isMultipart = true // Set false for byte array uploads
    )
}

Customization

For advanced customization, modify the DSL configuration or extend the library classes with your implementations.

Contributing

Contributions are welcome! Feel free to submit pull requests or open issues for bugs, feature requests, or enhancements.

License

RetrofitUploadKit is made available under the MIT License. For more details, see the LICENSE file in the repository.

Remember to replace placeholder text (like YOUR_DATA_CLASS) with the actual content or provide clear instructions on what users should replace it with based on their use case.

SERVER

if You don’t have a server for testing you can try cloning my other repo which simulates the Server https://github.com/DAKSHSEMWAL/file-upload-server FILE_UPLOAD_SERVER