|
1 | 1 | package com.automattic.encryptedlogging.network.rest.wpcom.encryptedlog |
2 | 2 |
|
3 | 3 | import android.util.Log |
4 | | -import com.android.volley.NoConnectionError |
5 | | -import com.android.volley.RequestQueue |
6 | | -import com.android.volley.VolleyError |
7 | | -import kotlinx.coroutines.suspendCancellableCoroutine |
| 4 | +import com.automattic.encryptedlogging.network.EncryptedLogHttpClient |
| 5 | +import com.automattic.encryptedlogging.store.UploadEncryptedLogError |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.withContext |
8 | 8 | import org.json.JSONException |
9 | 9 | import org.json.JSONObject |
10 | | -import com.automattic.encryptedlogging.network.EncryptedLogUploadRequest |
11 | | -import com.automattic.encryptedlogging.store.UploadEncryptedLogError |
12 | | -import kotlin.coroutines.resume |
| 10 | +import java.io.IOException |
13 | 11 |
|
14 | 12 | private const val INVALID_REQUEST = "invalid-request" |
15 | 13 | private const val TOO_MANY_REQUESTS = "too_many_requests" |
16 | 14 |
|
17 | 15 | internal class EncryptedLogRestClient( |
18 | | - private val requestQueue: RequestQueue, |
19 | | - private val clientSecret: String, |
| 16 | + private val httpClient: EncryptedLogHttpClient, |
20 | 17 | ) { |
21 | 18 | suspend fun uploadLog(logUuid: String, contents: String): UploadEncryptedLogResult { |
22 | | - return suspendCancellableCoroutine { cont -> |
23 | | - val request = EncryptedLogUploadRequest(logUuid, contents, clientSecret, { |
24 | | - cont.resume(UploadEncryptedLogResult.LogUploaded) |
25 | | - }, { error -> |
26 | | - cont.resume(UploadEncryptedLogResult.LogUploadFailed(mapError(error))) |
27 | | - }) |
28 | | - cont.invokeOnCancellation { request.cancel() } |
29 | | - requestQueue.add(request) |
| 19 | + return withContext(Dispatchers.IO) { |
| 20 | + try { |
| 21 | + val response = httpClient.uploadLog(logUuid, contents) |
| 22 | + if (response.statusCode in 200..299) { |
| 23 | + UploadEncryptedLogResult.LogUploaded |
| 24 | + } else { |
| 25 | + UploadEncryptedLogResult.LogUploadFailed(mapError(response.statusCode, response.body)) |
| 26 | + } |
| 27 | + } catch (e: IOException) { |
| 28 | + UploadEncryptedLogResult.LogUploadFailed(UploadEncryptedLogError.NoConnection) |
| 29 | + } |
30 | 30 | } |
31 | 31 | } |
32 | 32 |
|
33 | | - /** |
34 | | - * { |
35 | | - * "error":"too_many_requests", |
36 | | - * "message":"You're sending too many messages. Please slow down." |
37 | | - * } |
38 | | - * { |
39 | | - * "error":"invalid-request", |
40 | | - * "message":"Invalid UUID: uuids must only contain letters, numbers, dashes, and curly brackets" |
41 | | - * } |
42 | | - */ |
43 | 33 | @Suppress("ReturnCount") |
44 | | - private fun mapError(error: VolleyError): UploadEncryptedLogError { |
45 | | - if (error is NoConnectionError) { |
46 | | - return UploadEncryptedLogError.NoConnection |
| 34 | + private fun mapError(statusCode: Int, responseBody: String): UploadEncryptedLogError { |
| 35 | + val json = try { |
| 36 | + JSONObject(responseBody) |
| 37 | + } catch (jsonException: JSONException) { |
| 38 | + Log.e(TAG, "Received response not in JSON format: " + jsonException.message) |
| 39 | + return UploadEncryptedLogError.Unknown(statusCode = statusCode, message = responseBody) |
47 | 40 | } |
48 | | - error.networkResponse?.let { networkResponse -> |
49 | | - val statusCode = networkResponse.statusCode |
50 | | - val dataString = String(networkResponse.data) |
51 | | - val json = try { |
52 | | - JSONObject(dataString) |
53 | | - } catch (jsonException: JSONException) { |
54 | | - Log.e(TAG, "Received response not in JSON format: " + jsonException.message) |
55 | | - return UploadEncryptedLogError.Unknown(message = dataString) |
56 | | - } |
57 | | - val errorMessage = json.getString("message") |
58 | | - json.getString("error").let { errorType -> |
59 | | - if (errorType == INVALID_REQUEST) { |
60 | | - return UploadEncryptedLogError.InvalidRequest |
61 | | - } else if (errorType == TOO_MANY_REQUESTS) { |
62 | | - return UploadEncryptedLogError.TooManyRequests |
63 | | - } |
| 41 | + val errorMessage = json.getString("message") |
| 42 | + json.getString("error").let { errorType -> |
| 43 | + if (errorType == INVALID_REQUEST) { |
| 44 | + return UploadEncryptedLogError.InvalidRequest |
| 45 | + } else if (errorType == TOO_MANY_REQUESTS) { |
| 46 | + return UploadEncryptedLogError.TooManyRequests |
64 | 47 | } |
65 | | - return UploadEncryptedLogError.Unknown(statusCode, errorMessage) |
66 | 48 | } |
67 | | - return UploadEncryptedLogError.Unknown() |
| 49 | + return UploadEncryptedLogError.Unknown(statusCode, errorMessage) |
68 | 50 | } |
69 | 51 |
|
70 | 52 | companion object { |
|
0 commit comments