Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Identity Api response caching #508

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions android-core/src/main/java/com/mparticle/identity/IdentityApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.mparticle.internal.MessageManager;
import com.mparticle.internal.listeners.ApiClass;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -348,6 +350,26 @@ private void reset() {
}
}

private String generateHash(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes());

// Convert byte array to hex string
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
//throw new RuntimeException("Hashing algorithm not found", e);
}
return null;

}

private BaseIdentityTask makeIdentityRequest(IdentityApiRequest request, final IdentityNetworkRequestRunnable networkRequest) {
if (request == null) {
request = IdentityApiRequest.withEmptyUser().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@
import com.mparticle.internal.Logger;
import com.mparticle.internal.MPUtility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Class that represents observed changes in user state, can be used as a parameter in an Identity Request.
Expand All @@ -21,7 +33,7 @@
* @see IdentityApi#identify(IdentityApiRequest)
* @see IdentityApi#modify(IdentityApiRequest)
*/
public final class IdentityApiRequest {
public final class IdentityApiRequest implements Serializable {
private UserAliasHandler userAliasHandler = null;
private Map<MParticle.IdentityType, String> userIdentities = new HashMap<MParticle.IdentityType, String>();
// for /modify requests
Expand Down Expand Up @@ -88,6 +100,47 @@ public UserAliasHandler getUserAliasHandler() {
return userAliasHandler;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true; // Check if the same object
if (obj == null || getClass() != obj.getClass()) return false; // Check for null and class match

IdentityApiRequest that = (IdentityApiRequest) obj; // Cast to IdentityApiRequest

// Compare all relevant fields
return Objects.equals(userIdentities, that.userIdentities) &&
Objects.equals(otherOldIdentities, that.otherOldIdentities) &&
Objects.equals(otherNewIdentities, that.otherNewIdentities) &&
Objects.equals(userAliasHandler, that.userAliasHandler) &&
Objects.equals(mpid, that.mpid);
}


public String objectToHash() {
String input =this.toString();
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = md.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString().substring(0, 16); // Shorten to first 16 characters
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}


@NonNull
@Override
public String toString() {
return "userIdentities"+userIdentities+" otherOldIdentities " +otherOldIdentities+" otherNewIdentities "+otherNewIdentities
+" userAliasHandler "+userAliasHandler+" mpid "+String.valueOf(mpid);
}

/**
* A class used for constructing IdentityApiRequest.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,31 @@ public String toString() {
}
return builder.toString();
}

public static IdentityHttpResponse fromJson(@NonNull JSONObject jsonObject) throws JSONException {
int httpCode = jsonObject.optInt("http_code", 0);
return new IdentityHttpResponse(httpCode, jsonObject);
}

@NonNull
public JSONObject toJson() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("http_code", httpCode);
jsonObject.put(MPID, mpId);
jsonObject.put(CONTEXT, context);
jsonObject.put(LOGGED_IN, loggedIn);

if (!errors.isEmpty()) {
JSONArray errorsArray = new JSONArray();
for (Error error : errors) {
JSONObject errorObject = new JSONObject();
errorObject.put(CODE, error.code);
errorObject.put(MESSAGE, error.message);
errorsArray.put(errorObject);
}
jsonObject.put(ERRORS, errorsArray);
}

return jsonObject;
}
}
Loading
Loading