@@ -10,6 +10,7 @@ import com.vk.admstorm.env.getByKey
10
10
import kotlinx.serialization.json.Json
11
11
import kotlinx.serialization.json.JsonObject
12
12
import kotlinx.serialization.json.jsonPrimitive
13
+ import java.net.HttpURLConnection
13
14
import java.net.URI
14
15
import java.net.http.HttpClient
15
16
import java.net.http.HttpRequest
@@ -39,27 +40,27 @@ class HastebinService {
39
40
val request = HttpRequest .newBuilder()
40
41
.uri(URI .create(" $hastLink /documents" ))
41
42
.POST (HttpRequest .BodyPublishers .ofString(data))
42
- .timeout(Duration .ofSeconds(10 )).build()
43
+ .timeout(Duration .ofSeconds(10 ))
44
+ .build()
43
45
44
46
val response = httpClient.send(request, HttpResponse .BodyHandlers .ofString())
45
- if (response.statusCode() != 200 ) {
46
- LOG .error(
47
- " Something wrong with Hastebin service:" +
48
- " status code ${response.statusCode()} , error: ${response.body()} "
49
- )
47
+ if (response.statusCode() != HttpURLConnection .HTTP_OK ) {
48
+ LOG .error(" Error sending the request (status code: ${response.statusCode()} , body: ${response.body()} )" )
50
49
return null
51
50
}
52
51
53
52
val jsonResponse = Json .parseToJsonElement(response.body())
54
53
if (jsonResponse !is JsonObject ){
55
- LOG .error(
56
- " Something wrong with Hastebin service:" +
57
- " status code ${response.statusCode()} , error: ${response.body()} "
58
- )
54
+ LOG .error(" Error parsing the request (body: ${response.body()} )" )
59
55
return null
60
56
}
61
57
62
58
val value = jsonResponse[" key" ]?.jsonPrimitive?.content
59
+ if (value == null ) {
60
+ LOG .error(" JsonResponse returned null value" )
61
+ return null
62
+ }
63
+
63
64
return " $hastLink /${value} "
64
65
}
65
66
}
0 commit comments