|
1 | 1 | /* |
2 | | - * Copyright 2020-2024 Björn Kautler |
| 2 | + * Copyright 2020-2025 Björn Kautler |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -27,6 +27,7 @@ import io.ktor.client.request.forms.submitForm |
27 | 27 | import io.ktor.client.statement.bodyAsText |
28 | 28 | import io.ktor.http.HttpStatusCode.Companion.OK |
29 | 29 | import io.ktor.http.parameters |
| 30 | +import io.ktor.utils.io.core.use |
30 | 31 | import js.objects.recordOf |
31 | 32 | import kotlinx.coroutines.CoroutineStart.LAZY |
32 | 33 | import kotlinx.coroutines.DelicateCoroutinesApi |
@@ -62,58 +63,58 @@ sealed class Distribution( |
62 | 63 | return@async _downloadUrl |
63 | 64 | } |
64 | 65 |
|
65 | | - val client = HttpClient(Js) { |
66 | | - install(UserAgent) { |
67 | | - agent = "Setup WSL GitHub Action" |
68 | | - } |
69 | | - } |
70 | | - |
71 | 66 | val parameters = parameters { |
72 | 67 | append("type", "ProductId") |
73 | 68 | append("url", productId!!) |
74 | 69 | } |
75 | 70 |
|
76 | | - return@async retry(5) { |
77 | | - val response = client.submitForm( |
78 | | - url = "https://store.rg-adguard.net/api/GetFiles", |
79 | | - formParameters = parameters |
80 | | - ) |
| 71 | + return@async HttpClient(Js) { |
| 72 | + install(UserAgent) { |
| 73 | + agent = "Setup WSL GitHub Action" |
| 74 | + } |
| 75 | + }.use { client -> |
| 76 | + retry(5) { |
| 77 | + val response = client.submitForm( |
| 78 | + url = "https://store.rg-adguard.net/api/GetFiles", |
| 79 | + formParameters = parameters |
| 80 | + ) |
81 | 81 |
|
82 | | - if (response.status != OK) { |
83 | | - if (isDebug()) { |
84 | | - val echoResponse = client.submitForm( |
85 | | - url = "https://echo.free.beeceptor.com/api/GetFiles", |
86 | | - formParameters = parameters |
87 | | - ) |
88 | | - if (echoResponse.status == OK) { |
89 | | - debug("Request:\n${echoResponse.bodyAsText()}") |
90 | | - } else { |
91 | | - debug("Could not get echo response (statusCode: ${echoResponse.status.value} / statusMessage: ${echoResponse.status.description})") |
| 82 | + if (response.status != OK) { |
| 83 | + if (isDebug()) { |
| 84 | + val echoResponse = client.submitForm( |
| 85 | + url = "https://echo.free.beeceptor.com/api/GetFiles", |
| 86 | + formParameters = parameters |
| 87 | + ) |
| 88 | + if (echoResponse.status == OK) { |
| 89 | + debug("Request:\n${echoResponse.bodyAsText()}") |
| 90 | + } else { |
| 91 | + debug("Could not get echo response (statusCode: ${echoResponse.status.value} / statusMessage: ${echoResponse.status.description})") |
| 92 | + } |
| 93 | + |
| 94 | + val responseMessage = JSON.stringify( |
| 95 | + recordOf( |
| 96 | + "httpVersion" to "${response.version}", |
| 97 | + "headers" to "${response.headers}", |
| 98 | + "method" to "${response.call.request.method}", |
| 99 | + "url" to "${response.call.request.url}", |
| 100 | + "statusCode" to response.status.value, |
| 101 | + "statusMessage" to response.status.description, |
| 102 | + "body" to response.bodyAsText() |
| 103 | + ), |
| 104 | + space = 2 |
| 105 | + ) |
| 106 | + debug("Response:\n$responseMessage") |
92 | 107 | } |
93 | | - |
94 | | - val responseMessage = JSON.stringify( |
95 | | - recordOf( |
96 | | - "httpVersion" to "${response.version}", |
97 | | - "headers" to "${response.headers}", |
98 | | - "method" to "${response.call.request.method}", |
99 | | - "url" to "${response.call.request.url}", |
100 | | - "statusCode" to response.status.value, |
101 | | - "statusMessage" to response.status.description, |
102 | | - "body" to response.bodyAsText() |
103 | | - ), |
104 | | - space = 2 |
105 | | - ) |
106 | | - debug("Response:\n$responseMessage") |
| 108 | + error("Could not determine download URL (statusCode: ${response.status.value} / statusMessage: ${response.status.description})") |
107 | 109 | } |
108 | | - error("Could not determine download URL (statusCode: ${response.status.value} / statusMessage: ${response.status.description})") |
109 | | - } |
110 | 110 |
|
111 | | - val body = response.bodyAsText() |
112 | | - val downloadLinkAnchorMatch = |
113 | | - """<a [^>]*href="(?<url>[^"]+)"[^>]*>[^<]*\.appx(?:bundle)?</a>""".toRegex().find(body) |
114 | | - ?: error("Could not determine download URL from:\n$body") |
| 111 | + val body = response.bodyAsText() |
| 112 | + val downloadLinkAnchorMatch = |
| 113 | + """<a [^>]*href="(?<url>[^"]+)"[^>]*>[^<]*\.appx(?:bundle)?</a>""".toRegex().find(body) |
| 114 | + ?: error("Could not determine download URL from:\n$body") |
115 | 115 |
|
116 | | - return@retry URL(downloadLinkAnchorMatch.groups[1]!!.value) |
| 116 | + return@retry URL(downloadLinkAnchorMatch.groups[1]!!.value) |
| 117 | + } |
117 | 118 | } |
118 | 119 | } |
119 | 120 |
|
|
0 commit comments