@@ -2,14 +2,13 @@ package io.appwrite.starterkit.data.repository
2
2
3
3
import android.content.Context
4
4
import io.appwrite.Client
5
+ import io.appwrite.exceptions.AppwriteException
5
6
import io.appwrite.services.Account
6
7
import io.appwrite.services.Databases
8
+ import io.appwrite.starterkit.constants.AppwriteConfig
7
9
import io.appwrite.starterkit.data.models.Log
8
10
import kotlinx.coroutines.Dispatchers
9
11
import kotlinx.coroutines.withContext
10
- import java.io.BufferedReader
11
- import java.net.HttpURLConnection
12
- import java.net.URL
13
12
import java.text.SimpleDateFormat
14
13
import java.util.Date
15
14
import java.util.Locale
@@ -26,8 +25,8 @@ class AppwriteRepository private constructor(context: Context) {
26
25
27
26
// Appwrite Client and Services
28
27
private val client = Client (context.applicationContext)
29
- .setProject(APPWRITE_PROJECT_ID )
30
- .setEndpoint(APPWRITE_PUBLIC_ENDPOINT )
28
+ .setProject(AppwriteConfig . APPWRITE_PROJECT_ID )
29
+ .setEndpoint(AppwriteConfig . APPWRITE_PUBLIC_ENDPOINT )
31
30
32
31
private val account: Account = Account (client)
33
32
private val databases: Databases = Databases (client)
@@ -38,44 +37,27 @@ class AppwriteRepository private constructor(context: Context) {
38
37
*
39
38
* @return [Log] A log object containing details of the request and response.
40
39
*/
41
- suspend fun fetchPingLog (): Log = withContext(Dispatchers .IO ) {
42
- val url = URL (" ${client.endpoint}$PING_PATH " )
43
- val connection = (url.openConnection() as HttpURLConnection ).apply {
44
- requestMethod = " GET"
40
+ suspend fun fetchPingLog (): Log {
41
+ val date = getCurrentDate()
45
42
46
- readTimeout = DEFAULT_TIMEOUT
47
- connectTimeout = DEFAULT_TIMEOUT
48
-
49
- setRequestProperty(" Content-Type" , " application/json" )
50
- setRequestProperty(" x-appwrite-response-format" , APPWRITE_VERSION )
51
- setRequestProperty(" x-appwrite-project" , APPWRITE_PROJECT_ID )
52
- }
53
-
54
- try {
55
- val statusCode = connection.responseCode
56
- val response = if (statusCode == HttpURLConnection .HTTP_OK ) {
57
- connection.inputStream.bufferedReader().use(BufferedReader ::readText)
58
- } else {
59
- " Request failed with status code $statusCode "
60
- }
43
+ return try {
44
+ val response = withContext(Dispatchers .IO ) { client.ping() }
61
45
62
46
Log (
63
- date = getCurrentDate() ,
64
- status = statusCode.toString() ,
47
+ date = date ,
48
+ status = " 200 " ,
65
49
method = " GET" ,
66
- path = PING_PATH ,
50
+ path = " /ping " ,
67
51
response = response
68
52
)
69
- } catch (e : Exception ) {
53
+ } catch (exception : AppwriteException ) {
70
54
Log (
71
- date = getCurrentDate(),
72
- status = " Error" ,
55
+ date = date,
73
56
method = " GET" ,
74
- path = PING_PATH ,
75
- response = " Error occurred: ${e.message} "
57
+ path = " /ping" ,
58
+ status = " ${exception.code} " ,
59
+ response = " Error occurred: ${exception.message} "
76
60
)
77
- } finally {
78
- connection.disconnect()
79
61
}
80
62
}
81
63
@@ -93,36 +75,6 @@ class AppwriteRepository private constructor(context: Context) {
93
75
@Volatile
94
76
private var INSTANCE : AppwriteRepository ? = null
95
77
96
- /* *
97
- * Appwrite Server version.
98
- */
99
- const val APPWRITE_VERSION = " 1.6.0"
100
-
101
- /* *
102
- * Appwrite project id
103
- */
104
- const val APPWRITE_PROJECT_ID = " my-project-id"
105
-
106
- /* *
107
- * Appwrite project name
108
- */
109
- const val APPWRITE_PROJECT_NAME = " My project"
110
-
111
- /* *
112
- * Appwrite server endpoint url.
113
- */
114
- const val APPWRITE_PUBLIC_ENDPOINT = " https://cloud.appwrite.io/v1"
115
-
116
- /* *
117
- * The path to use for ping.
118
- */
119
- const val PING_PATH = " /ping"
120
-
121
- /* *
122
- * Default network timeout.
123
- */
124
- const val DEFAULT_TIMEOUT = 5_000
125
-
126
78
/* *
127
79
* Singleton factory method to get the instance of AppwriteRepository.
128
80
* Ensures thread safety
0 commit comments