|
| 1 | +package com.emrepbu.smsgateway.data.remote.api.service |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import com.emrepbu.smsgateway.data.remote.api.model.ApiResponse |
| 5 | +import com.emrepbu.smsgateway.data.remote.api.model.ApiResult |
| 6 | +import com.emrepbu.smsgateway.data.remote.api.model.SmsRequest |
| 7 | +import io.ktor.client.* |
| 8 | +import io.ktor.client.call.* |
| 9 | +import io.ktor.client.engine.cio.* |
| 10 | +import io.ktor.client.plugins.* |
| 11 | +import io.ktor.client.plugins.contentnegotiation.* |
| 12 | +import io.ktor.client.request.* |
| 13 | +import io.ktor.client.statement.* |
| 14 | +import io.ktor.http.* |
| 15 | +import io.ktor.serialization.kotlinx.json.* |
| 16 | +import kotlinx.coroutines.Dispatchers |
| 17 | +import kotlinx.coroutines.withContext |
| 18 | +import kotlinx.serialization.json.Json |
| 19 | +import javax.inject.Inject |
| 20 | +import javax.inject.Singleton |
| 21 | + |
| 22 | +private const val TAG = "ApiService" |
| 23 | + |
| 24 | +@Singleton |
| 25 | +class ApiService @Inject constructor() { |
| 26 | + private val client = HttpClient(CIO) { |
| 27 | + install(ContentNegotiation) { |
| 28 | + json(Json { |
| 29 | + prettyPrint = true |
| 30 | + isLenient = true |
| 31 | + ignoreUnknownKeys = true |
| 32 | + }) |
| 33 | + } |
| 34 | + install(HttpTimeout) { |
| 35 | + requestTimeoutMillis = 15000 // 15 saniye |
| 36 | + connectTimeoutMillis = 15000 |
| 37 | + socketTimeoutMillis = 15000 |
| 38 | + } |
| 39 | + defaultRequest { |
| 40 | + contentType(ContentType.Application.Json) |
| 41 | + accept(ContentType.Application.Json) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + suspend fun sendSms(apiUrl: String, phoneNumber: String, message: String, senderName: String? = null, authToken: String? = null): ApiResult<ApiResponse> { |
| 46 | + return try { |
| 47 | + withContext(Dispatchers.IO) { |
| 48 | + val response = client.post(apiUrl) { |
| 49 | + setBody(SmsRequest(phoneNumber, message, senderName)) |
| 50 | + if (!authToken.isNullOrBlank()) { |
| 51 | + header("Authorization", "Bearer $authToken") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + when (response.status) { |
| 56 | + HttpStatusCode.OK -> { |
| 57 | + val responseData: ApiResponse = response.body() |
| 58 | + ApiResult.Success(responseData) |
| 59 | + } |
| 60 | + else -> { |
| 61 | + ApiResult.Error("API isteği başarısız: ${response.status.description}", response.status.value) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (e: ClientRequestException) { |
| 66 | + Log.e(TAG, "HTTP Hatası: ${e.response.status.value}") |
| 67 | + ApiResult.Error("HTTP Hatası: ${e.response.status.description}", e.response.status.value) |
| 68 | + } catch (e: ServerResponseException) { |
| 69 | + Log.e(TAG, "Sunucu Hatası: ${e.response.status.value}") |
| 70 | + ApiResult.Error("Sunucu Hatası: ${e.response.status.description}", e.response.status.value) |
| 71 | + } catch (e: HttpRequestTimeoutException) { |
| 72 | + Log.e(TAG, "Bağlantı zaman aşımına uğradı", e) |
| 73 | + ApiResult.Error("Bağlantı zaman aşımına uğradı. İnternet bağlantınızı kontrol edin.") |
| 74 | + } catch (e: Exception) { |
| 75 | + Log.e(TAG, "API isteği sırasında hata oluştu", e) |
| 76 | + ApiResult.Error("İstek gönderilirken bir hata oluştu: ${e.localizedMessage}") |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fun close() { |
| 81 | + client.close() |
| 82 | + } |
| 83 | +} |
0 commit comments