Skip to content

epool/ip-api-klient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IpApiKlient

This is a utility written in Kotlin for checking ip's info by fetching the ip-api.com API.

Libraries

This project uses the following libraries internally to work.

Development libraries

Testing libraries

  • Spek Used to write some integration tests as specs.

Usage

Dependency

repositories {
    ...
    maven { url 'https://jitpack.io' }
    ...
}

dependencies {
    ...
    implementation "com.github.epool:ip-api-klient:1.0.0"
    ...
}

Blocking

Use it when blocking is safe to use like on web servers.

Java
IpCheckResult ipCheckResult = IpApiKlient.getIpInfo("8.8.8.8").blockingGet();
if (ipCheckResult.isSuccess()) {
    IpInfo ipInfo = ipCheckResult.getIpInfo();
} else {
    IpError ipError = ipCheckResult.getIpError();
}
Kotlin
val ipCheckResult = IpApiKlient.getIpInfo("8.8.8.8").blockingGet()
if (ipCheckResult.isSuccess()) {
    val ipInfo = ipCheckResult.ipInfo
} else {
    val ipError = ipCheckResult.ipError
}

Async

Use it when blocking is not safe to use like on Android main thread.

Java
IpApiKlient.getIpInfo("8.8.8.8")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread()) // In case you are using it on Android or use any other scheduler you need
        .subscribe(
                new Consumer<IpCheckResult>() {
                    @Override
                    public void accept(IpCheckResult ipCheckResult) throws Exception {
                        System.out.println(profile.isSuccess());
                    }
                },
                new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        System.out.println(throwable.getCause().toString());
                    }
                }
        );
Kotlin
IpApiKlient.getIpInfo("8.8.8.8")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                { println(it.isSuccess()) },
                { println(it.cause.toString()) }
        )

NOTE: If the ip check has an error IpCheckResult.isSuccess() will be false.