File tree Expand file tree Collapse file tree 3 files changed +72
-0
lines changed
main/kotlin/com/baeldung/retrofit
test/kotlin/com/baeldung/retrofit Expand file tree Collapse file tree 3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 59
59
<artifactId >gson</artifactId >
60
60
<version >${gson.version} </version >
61
61
</dependency >
62
+ <dependency >
63
+ <groupId >com.squareup.retrofit2</groupId >
64
+ <artifactId >retrofit</artifactId >
65
+ <version >${retrofit.version} </version >
66
+ </dependency >
62
67
</dependencies >
63
68
64
69
<properties >
65
70
<khttp .version>1.3.1</khttp .version>
66
71
<fuel .version>2.3.1</fuel .version>
67
72
<http4k .version>5.25.0.0</http4k .version>
73
+ <retrofit .version>2.9.0</retrofit .version>
68
74
</properties >
69
75
70
76
</project >
Original file line number Diff line number Diff line change
1
+ package com.baeldung.retrofit
2
+
3
+ import kotlinx.coroutines.Dispatchers
4
+ import kotlinx.coroutines.withContext
5
+ import okhttp3.ResponseBody
6
+ import retrofit2.Retrofit
7
+ import retrofit2.http.GET
8
+ import retrofit2.http.Url
9
+ import java.io.File
10
+ import java.io.FileOutputStream
11
+
12
+ // Retrofit service interface
13
+ interface FileDownloadService {
14
+ @GET
15
+ suspend fun downloadFile (@Url fileUrl : String ): ResponseBody
16
+ }
17
+
18
+ // Function to download a PDF using Retrofit
19
+ suspend fun downloadPdfWithRetrofit (url : String , outputFile : File ) {
20
+ val retrofit = Retrofit .Builder ()
21
+ .baseUrl(" https://example.com/" ) // Base URL is required but won't be used with dynamic @Url
22
+ .build()
23
+
24
+ val service = retrofit.create(FileDownloadService ::class .java)
25
+
26
+ withContext(Dispatchers .IO ) {
27
+ val responseBody = service.downloadFile(url)
28
+
29
+ // Save the file to disk
30
+ responseBody.byteStream().use { inputStream ->
31
+ FileOutputStream (outputFile).use { outputStream ->
32
+ inputStream.copyTo(outputStream)
33
+ }
34
+ }
35
+
36
+ println (" File downloaded successfully to ${outputFile.absolutePath} " )
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ package com.baeldung.retrofit
2
+
3
+ import kotlinx.coroutines.runBlocking
4
+ import org.junit.Test
5
+ import java.io.File
6
+ import kotlin.test.assertTrue
7
+
8
+ class FileDownloadServiceUnitTest {
9
+
10
+ @Test
11
+ fun testSuccessfulFileDownload (): Unit = runBlocking {
12
+
13
+ // File to store the downloaded content
14
+ val outputFile = File (" test_sample.pdf" )
15
+ if (outputFile.exists()) {
16
+ outputFile.delete()
17
+ }
18
+
19
+ // Call the download function
20
+ downloadPdfWithRetrofit(" https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" , outputFile)
21
+
22
+ // Verify the file was created
23
+ assertTrue(outputFile.exists())
24
+
25
+ // Clean up the test file
26
+ outputFile.delete()
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments