Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisxmlapi committed Mar 29, 2021
0 parents commit 476538b
Show file tree
Hide file tree
Showing 35 changed files with 1,757 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea/*
.gradle/*
gradlew*
**/build/
!gradle-wrapper.jar
**/*.jar
163 changes: 163 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Whois XML API client library
The java library for the [Whois XML API service](https://whois.whoisxmlapi.com/).

## Requirements
* JRE version 1.8 or newer.
* WhoisXMLAPI account and the API key. The API key could be obtained [here](https://user.whoisxmlapi.com/products).

## Installation
Jar files are available in the latest release.

## Usage
API documentation available [here](https://whois.whoisxmlapi.com/documentation/making-requests).

The minimal usage sample:
```java
//Creating a client object with an API key and default settings
ApiClient client = new ApiClient(System.getenv("API_KEY"));

try {
WhoisRecord record = client.getWhois("whoisxmlapi.com");

// Model's getters return an Option<T> (Option<String>)
if (record.getDomainName().isPresent()) {
System.out.println(record.getDomainName().get());
}
// Or
record.getContactEmail().ifPresent((email) -> System.out.println(email));
} catch (BaseException e) {
System.err.println(e.getMessage());
}
```

Passing an invalid domain name
```java
try {
// Sometimes, when your request is invalid you will receive an error response
WhoisRecord record2 = client.getWhois("incorrectdomain");
} catch (ApiErrorException e) {
// In that case the ApiErrorException will be thrown
// Use e.getErrorMessageEntity() to get ErrorMessage object (Could be a null!)
System.err.println(e.getApiErrorMessage());
} catch (BaseException e) {
System.err.println(e.getMessage());
}
```

If the API key is missed the IllegalArgumentException will be thrown.
```java
try {
// The IllegalArgumentException will be thrown if the empty API key is specified
// If the API key is null the NullPointerException will be thrown
ApiClient client1 = new ApiClient("");
} catch (IllegalArgumentException exception) {
System.err.println(exception.getMessage());
}
```

Handling various exceptions
```java
try {
// The ApiAuthorizationException will be thrown if you are not permitted to perform queries
// This could be caused by invalid API key value or empty account balance
ApiClient client1 = new ApiClient(System.getenv("API_KEY").replace('0', '9'));
WhoisRecord record = client1.getWhois("whoisxmlapi.net");
} catch (ApiAuthorizationException exception) {
System.err.println(exception.getMessage());
} catch (NetworkException exception) {
// NetworkException means that there was something wrong with connection
System.err.println(exception.getMessage());
} catch (ApiEndpointException exception) {
// In general, this exception means that API returned HTTP 5XX code
System.err.println(exception.getMessage());
} catch (EmptyApiKeyException exception) {
System.err.println(exception.getMessage());
} catch (ApiErrorMessageException exception) {
// This exception means that API response contains ErrorMessage field
System.err.println(exception.getMessage());
}
```

Customizing request parameters:
```java
try {
// Creating a request parameters object
RequestParameters rp = new RequestParameters(System.getenv("API_KEY"));
// Setting domain availability check level to 2
rp.setDa("2");
WhoisRecord record3 = client.getWhois("whoisxmlapi.com", rp);
record3.getDomainAvailability().ifPresent((da) -> System.out.println(da));
} catch (BaseException exception) {
System.err.println(exception.getMessage());
}
```

Domain list processing is supported:
```java
// Handling a list of domains
String[] domains = new String[]{"whoisxmlapi.com", "whoisxmlapi.net", "incorrectdomain"};
try {
// Setting the number of concurrent requests
// Allowed values are from 1 to 10, default is 5
client.setPoolSize(2);
// The auto-retry feature available
client.setRetries(2);
BaseRecord[] records = client.getWhois(domains);
for (BaseRecord record: records) {
if (record.getClass() == WhoisRecord.class) {
((WhoisRecord) record).getDomainName().ifPresent((domain) -> System.out.println(domain));
} else {
((ErrorMessage) record).getMsg().ifPresent((message) -> System.out.println(message));
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
```
**Warning!** The internal executor service keeps running for some time after all requests have been completed.
You can force the service to stop using the following method:
```java
client.forceShutdown();
```
The client instance cannot be reused after this. You have to create a new one.

Getting raw API responses instead of parsed data
```java
// Getting raw responses
try {
// Specifying response format
String[] rawResponses = client.getRawResponse(domains, HttpClient.ResponseFormat.XML);
for (String response: rawResponses) {
System.out.println(response);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
```

Specifying custom timeouts and request parameters
```java
RequestParameters rp = new RequestParameters(System.getenv("API_KEY"));
rp.setOutputFormat(HttpClient.ResponseFormat.XML);
rp.setIgnoreRawTexts("1");
// Decreasing timeouts
NetworkTimeouts timeouts = new NetworkTimeouts();
timeouts.setConnectTimeout(2);
timeouts.setWriteTimeout(2);
timeouts.setReadTimeout(10);
ApiClient client2 = new ApiClient(rp, timeouts);

try {
String response = client2.getRawResponse("whoisxmlapi.com");
System.out.println(response);
} catch (BaseException exception) {
System.err.println(exception.getMessage());
}
```

There is an option to provide a custom OkHttpClient object
```java
client2.setOkHttpClient(new OkHttpClient);
```
This method could help to set up proxies. Further information available in the OkHttp [documentation](https://square.github.io/okhttp/)

64 changes: 64 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
plugins {
id 'java-library'
id 'maven-publish'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

group 'com.whoisxmlapi.whoisapi'
version '1.0.3'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.slf4j:slf4j-api:1.7.30'
}

test {
environment "API_KEY", System.getenv("API_KEY")
useJUnitPlatform()
}

task heavyJar(type: Jar) {
archiveClassifier = 'all'
from {configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)} }
with jar
}

publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'api-client'
groupId = 'com.whoisxmlapi.whoisapi'
version = '1.0.3'
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
name = 'WhoisXML API client'
description = 'API client for WhoisXML API service'
url = 'https://whois.whoisxmlapi.com/'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'WhoisApiClient'

Loading

0 comments on commit 476538b

Please sign in to comment.