Skip to content

Commit

Permalink
Examples of authenticating with the X-Authentication-Token header.
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisxmlapi committed Jul 23, 2018
1 parent 8374733 commit f1439b4
Show file tree
Hide file tree
Showing 16 changed files with 875 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ languages.
You'll need a
[WhoisXmlApi account](https://registrant-alert-api.whoisxmlapi.com/signup) to
authenticate.
You can use either the **X-Authentication-Token** header or the request's body
**apiKey** parameter.

Please, refer to the
[Registrant Alert API User Guide](https://registrant-alert-api.whoisxmlapi.com/docs)
Expand Down
39 changes: 39 additions & 0 deletions header-auth/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.whoisxmlapi</groupId>
<artifactId>registrant-alert-v2-header-auth</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>RegistrantAlertV2HeaderAuthSample</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
16 changes: 16 additions & 0 deletions header-auth/java/registrant-alert-v2-header-auth.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.2" level="project" />
</component>
</module>
129 changes: 129 additions & 0 deletions header-auth/java/src/main/java/RegistrantAlertV2HeaderAuthSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.*;

public class RegistrantAlertV2HeaderAuthSample
{
private String apiKey;

public static void main(String[] args) throws Exception
{
RegistrantAlertV2HeaderAuthSample query =
new RegistrantAlertV2HeaderAuthSample();

// Fill in your details
query.setApiKey("Your registrant alert api key");

try {
String responseStringPost = query.sendPost();
query.prettyPrintJson(responseStringPost);

responseStringPost = query.sendPost(true);
query.prettyPrintJson(responseStringPost);
} catch (IOException e) {
e.printStackTrace();
}
}

public String getApiKey()
{
return this.apiKey;
}

public void setApiKey(String apiKey)
{
this.apiKey = apiKey;
}

public String sendPost() throws Exception
{
return sendPost(false);
}

// HTTP POST request
public String sendPost(boolean isAdvanced) throws Exception
{
String userAgent = "Mozilla/5.0";
String url = "https://registrant-alert-api.whoisxmlapi.com/api/v2";

URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", userAgent);
con.setRequestProperty("X-Authentication-Token", this.getApiKey());

String terms = isAdvanced ? getAdvancedTerms() : getBasicTerms();

// Send POST request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(terms);
wr.flush();
wr.close();

System.out.println("\nSending 'POST' request to URL : " + url);

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));

String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

return response.toString();
}

private String getAdvancedTerms()
{
String field = "\"field\": \"RegistrantContact.Name\"";
String term = "\"term\": \"Test\"";
String options = this.getRequestOptions();
String searchTerms = "{ " + field + ", " + term + " }";

String terms =
"\"advancedSearchTerms\": [" + searchTerms + "]";

return "{ " + terms + ", " + options +" }";
}

private String getBasicTerms()
{
String exclude = "\"exclude\": [\"pay\"]";
String include = "\"include\": [\"test\"]";
String options = this.getRequestOptions();

String terms =
"\"basicSearchTerms\": {" + include + ", " + exclude + "}";

return "{ " + terms + ", " + options +" }";
}

private String getRequestOptions()
{
return "\"sinceDate\": \"2018-07-16\","
+ "\"mode\": \"purchase\"";
}

private void prettyPrintJson(String jsonString)
{
Gson gson = new GsonBuilder().setPrettyPrinting().create();

JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonString);
String prettyJsonString = gson.toJson(je);

System.out.println("\n\n" + prettyJsonString);
System.out.println("----------------------------------------");
}
}
65 changes: 65 additions & 0 deletions header-auth/js/registrant_alert_api_v2_header_auth_jquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<title>Registrant Alert API 2.0 jQuery Search Sample</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">

var url = "https://registrant-alert-api.whoisxmlapi.com/api/v2";
var apiKey = "Your registrant alert api key";

var data_basic = {
"apiKey": apiKey,
"sinceDate": "2018-07-15",
"mode": "purchase",
"basicSearchTerms": {
"include": [
"cinema",
],
"exclude": [
"online"
]
}
};

var data_advanced = {
"apiKey": apiKey,
"sinceDate": "2018-07-15",
"mode": "purchase",
"advancedSearchTerms": [
{
"field": "RegistrantContact.Name",
"term": "Test"
}
]
};

function sendQuery(isAdvanced=false) {
$.ajax(
{
url: url,
type: "post",
data:
JSON.stringify(isAdvanced ? data_advanced : data_basic),
headers: {
"X-Authentication-Token": apiKey
},
dataType: "json",
success: function(data) {
$("body").append(
(isAdvanced ? "Advanced" : "Basic") + "<br>"
+ "<pre>" +JSON.stringify(data,null,2) +"</pre>");
}
}
);
}

$(function() {
sendQuery();
sendQuery(true);
});

</script>
</head>
<body></body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5D6D13A4-D65E-4459-824D-CADEEEC339B8}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RegistrantAlertApi</RootNamespace>
<AssemblyName>RegistrantAlertApiV2HeaderAuth</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Compile Include="RegistrantAlertApiV2HeaderAuthSample.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.0 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit f1439b4

Please sign in to comment.