-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
places/src/commonMain/kotlin/com/ngallazzi/places/data/PlacesRemoteDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.ngallazzi.places.data | ||
|
||
import com.ngallazzi.places.data.dto.AutocompleteDTO | ||
import com.ngallazzi.places.domain.PlacesDataSource | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.get | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.contentType | ||
|
||
internal class PlacesRemoteDataSource( | ||
private val httpClient: HttpClient | ||
) : | ||
PlacesDataSource { | ||
override suspend fun searchCity(name: String, languageCode: String): Result<AutocompleteDTO> { | ||
return handleNetworkCall("maps/api/place/autocomplete/json?input=$name&types=(cities)&language=$languageCode") | ||
} | ||
|
||
override suspend fun searchCountry( | ||
name: String, | ||
languageCode: String | ||
): Result<AutocompleteDTO> { | ||
return handleNetworkCall("maps/api/place/autocomplete/json?input=$name&type=country&language=$languageCode") | ||
} | ||
|
||
override suspend fun searchAddress( | ||
address: String, | ||
languageCode: String | ||
): Result<AutocompleteDTO> { | ||
return handleNetworkCall("maps/api/place/autocomplete/json?input=$address&type=geocode&language=$languageCode") | ||
} | ||
|
||
private suspend fun handleNetworkCall(url: String): Result<AutocompleteDTO> { | ||
return try { | ||
val result = httpClient.get(url) { | ||
contentType(ContentType.Application.Json) | ||
} | ||
return when (result.status) { | ||
HttpStatusCode.OK -> { | ||
val response = result.body<AutocompleteDTO>() | ||
when (response.status) { | ||
"OK" -> Result.success(response) | ||
"REQUEST_DENIED" -> Result.failure(Exception("Invalid api key")) | ||
else -> Result.failure(Exception(response.status)) | ||
} | ||
} | ||
|
||
else -> Result.failure(Exception(Exception(result.toString()))) | ||
} | ||
|
||
} catch (e: Exception) { | ||
Result.failure(Throwable(e)) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
places/src/commonMain/kotlin/com/ngallazzi/places/domain/PlacesDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ngallazzi.places.domain | ||
|
||
import com.ngallazzi.places.data.dto.AutocompleteDTO | ||
|
||
internal interface PlacesDataSource { | ||
suspend fun searchCity(name: String, languageCode: String): Result<AutocompleteDTO> | ||
|
||
suspend fun searchCountry(name: String, languageCode: String): Result<AutocompleteDTO> | ||
|
||
suspend fun searchAddress( | ||
address: String, | ||
languageCode: String | ||
): Result<AutocompleteDTO> | ||
} |