Skip to content

Commit

Permalink
javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jon8787 committed Feb 20, 2024
1 parent 865c1b3 commit f84fb01
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/main/java/com/uid2/client/IdentityMapClient.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.uid2.client;

public class IdentityMapClient {
/**
* @param uid2BaseUrl The <a href="https://unifiedid.com/docs/getting-started/gs-environments">UID2 Base URL</a>
* @param clientApiKey Your client API key
* @param base64SecretKey Your client secret key
*/
IdentityMapClient(String uid2BaseUrl, String clientApiKey, String base64SecretKey) {
advertiserAndDataProviderUid2Helper = new IdentityMapHelper(base64SecretKey);
identityMapHelper = new IdentityMapHelper(base64SecretKey);
uid2ClientHelper = new Uid2ClientHelper(uid2BaseUrl, clientApiKey);
}

/**
* @param identityMapInput represents the input required for <a href="https://unifiedid.com/docs/endpoints/post-identity-map">/identity/map</a>
* @return an IdentityMapResponse instance
* @throws Uid2Exception if the response did not contain a "success" status, or the response code was not 200, or there was an error communicating with the provided UID2 Base URL
*/
public IdentityMapResponse generateIdentityMap(IdentityMapInput identityMapInput) {
EnvelopeV2 envelope = advertiserAndDataProviderUid2Helper.createEnvelopeForIdentityMapRequest(identityMapInput);
EnvelopeV2 envelope = identityMapHelper.createEnvelopeForIdentityMapRequest(identityMapInput);

String responseString = uid2ClientHelper.makeRequest(envelope, "/v2/identity/map");
return advertiserAndDataProviderUid2Helper.createIdentityMapResponse(responseString, envelope, identityMapInput);
return identityMapHelper.createIdentityMapResponse(responseString, envelope, identityMapInput);
}

IdentityMapHelper advertiserAndDataProviderUid2Helper;
Uid2ClientHelper uid2ClientHelper;
private final IdentityMapHelper identityMapHelper;
private final Uid2ClientHelper uid2ClientHelper;
}
11 changes: 11 additions & 0 deletions src/main/java/com/uid2/client/IdentityMapHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ public class IdentityMapHelper {
*/
public IdentityMapHelper(String base64SecretKey) {uid2Helper = new Uid2Helper(base64SecretKey);}

/**
* @param identityMapInput represents the input required for <a href="https://unifiedid.com/docs/endpoints/post-identity-map">/identity/map</a>
* @return an EnvelopeV2 instance to use in the POST body of <a href="https://unifiedid.com/docs/endpoints/post-identity-map">/identity/map</a>
*/
public EnvelopeV2 createEnvelopeForIdentityMapRequest(IdentityMapInput identityMapInput) {
byte[] jsonBytes = identityMapInput.getAsJsonString().getBytes(StandardCharsets.UTF_8);
return uid2Helper.createEnvelopeV2(jsonBytes);
}


/**
* @param responseString the response body returned by a call to <a href="https://unifiedid.com/docs/endpoints/post-identity-map">/identity/map</a>
* @param envelope the EnvelopeV2 instance returned by {@link #createEnvelopeForIdentityMapRequest}
* @param identityMapInput the same instance that was passed to {@link #createEnvelopeForIdentityMapRequest}.
* @return an IdentityMapResponse instance
*/
public IdentityMapResponse createIdentityMapResponse(String responseString, EnvelopeV2 envelope, IdentityMapInput identityMapInput) {
String decryptedResponseString = uid2Helper.decrypt(responseString, envelope.getNonce());
return new IdentityMapResponse(decryptedResponseString, identityMapInput);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/uid2/client/IdentityMapInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
public class IdentityMapInput {
/**
* @param emails a list of normalized or unnormalized email addresses
* @return a IdentityMapInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
* @return a IdentityMapInput instance, to be used in {@link IdentityMapHelper#createEnvelopeForIdentityMapRequest}
*/
public static IdentityMapInput fromEmails(Iterable<String> emails) {
return new IdentityMapInput(IdentityType.Email, emails, false);
}

/**
* @param phones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number
* @return a IdentityMapInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromPhones(Iterable<String> phones) {
return new IdentityMapInput(IdentityType.Phone, phones, false);
}

/**
* @param hashedEmails a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address
* @return a IdentityMapInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromHashedEmails(Iterable<String> hashedEmails) {
return new IdentityMapInput(IdentityType.Email, hashedEmails, true);
}

/**
* @param hashedPhones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number
* @return a IdentityMapInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
* @return an IdentityMapInput instance
*/
public static IdentityMapInput fromHashedPhones(Iterable<String> hashedPhones) {
return new IdentityMapInput(IdentityType.Phone, hashedPhones, true);
Expand All @@ -48,7 +48,7 @@ String getAsJsonString() {
}
}

private IdentityMapInput(IdentityType identityType, Iterable<String> emailsOrPhones, boolean alreadyHashed) {
private IdentityMapInput(IdentityType identityType, Iterable<String> emailsOrPhones, boolean alreadyHashed) {
this.identityType = identityType;
this.emailsOrPhones = emailsOrPhones;
this.alreadyHashed = alreadyHashed;
Expand Down
22 changes: 21 additions & 1 deletion src/test/java/com/uid2/client/IdentityMapIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//most tests in this class require these env vars to be configured: UID2_BASE_URL, UID2_API_KEY, UID2_SECRET_KEY
@EnabledIfEnvironmentVariable(named = "UID2_BASE_URL", matches = "\\S+")
public class IdentityMapIntegrationTests {
final IdentityMapClient identityMapClient = new IdentityMapClient(System.getenv("UID2_BASE_URL"), System.getenv("UID2_API_KEY"), System.getenv("UID2_SECRET_KEY"));
final private IdentityMapClient identityMapClient = new IdentityMapClient(System.getenv("UID2_BASE_URL"), System.getenv("UID2_API_KEY"), System.getenv("UID2_SECRET_KEY"));

@Test
public void identityMapEmails() {
Expand Down Expand Up @@ -157,4 +157,24 @@ public void identityMapEmailsUseOwnHttp() {
}
}

@Test
public void identityMapBadUrl() {
IdentityMapClient identityMapClient = new IdentityMapClient("https://operator-bad-url.uidapi.com", System.getenv("UID2_API_KEY"), System.getenv("UID2_SECRET_KEY"));
IdentityMapInput identityMapInput = IdentityMapInput.fromEmails(Collections.singletonList("[email protected]"));
assertThrows(Uid2Exception.class, () -> identityMapClient.generateIdentityMap(identityMapInput));
}

@Test
public void identityMapBadApiKey() {
IdentityMapClient identityMapClient = new IdentityMapClient(System.getenv("UID2_BASE_URL"), "bad-api-key", System.getenv("UID2_SECRET_KEY"));
IdentityMapInput identityMapInput = IdentityMapInput.fromEmails(Collections.singletonList("[email protected]"));
assertThrows(Uid2Exception.class, () -> identityMapClient.generateIdentityMap(identityMapInput));
}

@Test
public void identityMapBadSecret() {
IdentityMapClient identityMapClient = new IdentityMapClient(System.getenv("UID2_BASE_URL"), System.getenv("UID2_API_KEY"), "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=");
IdentityMapInput identityMapInput = IdentityMapInput.fromEmails(Collections.singletonList("[email protected]"));
assertThrows(Uid2Exception.class, () -> identityMapClient.generateIdentityMap(identityMapInput));
}
}

0 comments on commit f84fb01

Please sign in to comment.