Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kamaravichow/Kotlin-rewrite
Kotlin rewrite
- Loading branch information
Showing
13 changed files
with
235 additions
and
286 deletions.
There are no files selected for viewing
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
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
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
This file was deleted.
Oops, something went wrong.
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,100 @@ | ||
package me.aravi.libraries.iplib | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import me.aravi.libraries.iplib.api.GeoLocatorAPI | ||
import me.aravi.libraries.iplib.listeners.OnIPResponse | ||
import me.aravi.libraries.iplib.model.IPInfo | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class IPLib private constructor() { | ||
/** | ||
* GeoLocator api call happens here | ||
* NOTE : THIS RUNS ON MAIN THREAD | ||
* | ||
* @param listener | ||
*/ | ||
fun ipLookup(listener: OnIPResponse) { | ||
val lookup: Call<IPInfo> = GeoLocatorAPI.apiService!!.lookup() | ||
lookup.enqueue(object : Callback<IPInfo?> { | ||
override fun onResponse(call: Call<IPInfo?>, response: Response<IPInfo?>) { | ||
if (response.isSuccessful) { | ||
listener.onSuccess(response.body()) | ||
} else { | ||
listener.onError(response.message(), response.code()) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<IPInfo?>, t: Throwable) { | ||
listener.onError(t.message, 1024) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Ip Lookup with Ip Address | ||
* | ||
* @param ipAddress | ||
* @param listener | ||
*/ | ||
fun ipLookupWith(ipAddress: String?, listener: OnIPResponse) { | ||
val lookup: Call<IPInfo> = GeoLocatorAPI.apiService!!.lookupFor(ipAddress) | ||
lookup.enqueue(object : Callback<IPInfo?> { | ||
override fun onResponse(call: Call<IPInfo?>, response: Response<IPInfo?>) { | ||
if (response.isSuccessful) { | ||
listener.onSuccess(response.body()) | ||
} else { | ||
listener.onError(response.message(), response.code()) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<IPInfo?>, t: Throwable) { | ||
listener.onError(t.message, 1024) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* This is same as the other method | ||
* this happens in IO CoroutineScope | ||
* | ||
* @param listener | ||
*/ | ||
fun ipLookupAsync(listener: OnIPResponse) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
ipLookup(listener) | ||
} | ||
} | ||
|
||
/** | ||
* Gets ip info | ||
* this happens in IO CoroutineScope | ||
* @param ipAddress | ||
* @param listener | ||
*/ | ||
fun ipLookupWithIpAsync(ipAddress: String?, listener: OnIPResponse) { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
ipLookupWith(ipAddress, listener) | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* Creates a new instance of iplib | ||
* | ||
* @return | ||
*/ | ||
@JvmStatic | ||
var instance: IPLib? = null | ||
get() { | ||
if (field == null) { | ||
field = IPLib() | ||
} | ||
return field | ||
} | ||
private set | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.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 me.aravi.libraries.iplib.api | ||
|
||
import me.aravi.libraries.iplib.model.IPInfo | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
|
||
interface Endpoints { | ||
@GET("json/") | ||
fun lookup(): Call<IPInfo> | ||
|
||
@GET("json/{ip}") | ||
fun lookupFor(@Path("ip") ip: String?): Call<IPInfo> | ||
} |
45 changes: 0 additions & 45 deletions
45
lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.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,46 @@ | ||
package me.aravi.libraries.iplib.api | ||
|
||
import retrofit2.http.GET | ||
import me.aravi.libraries.iplib.model.IPInfo | ||
import me.aravi.libraries.iplib.api.Endpoints | ||
import me.aravi.libraries.iplib.api.GeoLocatorAPI | ||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import androidx.annotation.Keep | ||
import me.aravi.libraries.iplib.listeners.OnIPResponse | ||
import android.os.AsyncTask | ||
import me.aravi.libraries.iplib.IPLib | ||
|
||
class GeoLocatorAPI private constructor() { | ||
val service: Endpoints? | ||
get() { | ||
if (apiService == null) { | ||
val gson = GsonBuilder() | ||
.setLenient() | ||
.create() | ||
val retrofit = Retrofit.Builder() | ||
.baseUrl(url) | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.build() | ||
apiService = retrofit.create(Endpoints::class.java) | ||
} | ||
return apiService | ||
} | ||
|
||
companion object { | ||
private val TAG = GeoLocatorAPI::class.java.simpleName | ||
const val url = "https://geolocation-db.com/" | ||
var apiService: Endpoints? = null | ||
var instance: GeoLocatorAPI? = null | ||
get() { | ||
if (field == null) { | ||
field = GeoLocatorAPI() | ||
} | ||
field!!.service | ||
return field | ||
} | ||
private set | ||
} | ||
} |
Oops, something went wrong.