Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

[PIR-96] Add Google Java formatting to all 3 projects #44

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion iterable-extension/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'java'
plugins {
id 'java'
id 'com.github.sherter.google-java-format' version '0.8'
}

repositories {
mavenLocal()
Expand All @@ -25,4 +28,10 @@ task buildZip(type: Zip) {
}
}

tasks.verifyGoogleJavaFormat.ignoreFailures = true

googleJavaFormat {
options style: 'AOSP'
}

build.dependsOn buildZip

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,57 @@
import com.google.gson.GsonBuilder;
import com.mparticle.iterable.IterableApiResponse;
import com.mparticle.iterable.IterableErrorHandler;
import retrofit2.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import retrofit2.Response;

/**
* A utility class for logging state while processing mParticle batches. All
* static methods write a JSON object to Standard Output where it can be queried
* in Cloudwatch.
* A utility class for logging state while processing mParticle batches. All static methods write a
* JSON object to Standard Output where it can be queried in Cloudwatch.
*/
public class IterableExtensionLogger {

private static final Gson gson = new GsonBuilder().create();
private static final Gson gson = new GsonBuilder().create();

static void logApiError(Response<?> response, UUID id) {
String iterableApiCode = null;
try {
IterableApiResponse errorBody = IterableErrorHandler.parseError(response);
iterableApiCode = errorBody.code;
} catch (IOException e) {
iterableApiCode = "Unable to parse Iterable API code";
static void logApiError(Response<?> response, UUID id) {
String iterableApiCode = null;
try {
IterableApiResponse errorBody = IterableErrorHandler.parseError(response);
iterableApiCode = errorBody.code;
} catch (IOException e) {
iterableApiCode = "Unable to parse Iterable API code";
}
String requestId = id != null ? id.toString() : "Unavailable";
String url = response.raw().request().url().encodedPath();
String httpStatus = String.valueOf(response.code());
Map<String, String> logMessage = new HashMap<>();
logMessage.put("message", "Error sending request to Iterable");
logMessage.put("url", url);
logMessage.put("httpStatus", httpStatus);
logMessage.put("iterableApiCode", iterableApiCode);
logMessage.put("mParticleEventId", requestId);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}
String requestId = id != null ? id.toString() : "Unavailable";
String url = response.raw().request().url().encodedPath();
String httpStatus = String.valueOf(response.code());
Map<String, String> logMessage = new HashMap<>();
logMessage.put("message", "Error sending request to Iterable");
logMessage.put("url", url);
logMessage.put("httpStatus", httpStatus);
logMessage.put("iterableApiCode", iterableApiCode);
logMessage.put("mParticleEventId", requestId);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}

static void logApiTimeout(String url, UUID mparticleEventId) {
String eventIdString = mparticleEventId != null ? mparticleEventId.toString() : "Unavailable";
Map<String, String> logMessage = new HashMap<>();
logMessage.put("errorType", "Retriable");
logMessage.put("message", "A timeout occurred while making request to Iterable");
logMessage.put("url", url);
logMessage.put("mparticleEventId", eventIdString);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}
static void logApiTimeout(String url, UUID mparticleEventId) {
String eventIdString =
mparticleEventId != null ? mparticleEventId.toString() : "Unavailable";
Map<String, String> logMessage = new HashMap<>();
logMessage.put("errorType", "Retriable");
logMessage.put("message", "A timeout occurred while making request to Iterable");
logMessage.put("url", url);
logMessage.put("mparticleEventId", eventIdString);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}

public static void logError(String message) {
Map<String, String> logMessage = new HashMap<>();
logMessage.put("message", message);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}
public static void logError(String message) {
Map<String, String> logMessage = new HashMap<>();
logMessage.put("message", message);
String messageJson = gson.toJson(logMessage);
System.out.println(messageJson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mparticle.sdk.model.Message;
import com.mparticle.sdk.model.MessageSerializer;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;

public class IterableLambdaEndpoint implements RequestStreamHandler {

static MessageSerializer serializer = new MessageSerializer();
static IterableExtension processor = new IterableExtension();
static final ObjectMapper mapper = new ObjectMapper();
static MessageSerializer serializer = new MessageSerializer();
static IterableExtension processor = new IterableExtension();
static final ObjectMapper mapper = new ObjectMapper();

@Override
public void handleRequest(InputStream input, OutputStream output, Context context)
throws RetriableError {
try {
String inputString = IOUtils.toString(input, "UTF-8");
Message request = parseQueueTrigger(inputString);
Message response = processor.processMessage(request);
serializer.serialize(output, response);
} catch (RetriableError e) {
IterableExtensionLogger.logError("Processing terminated by a RetriableError");
throw e;
} catch (Exception e) {
IterableExtensionLogger.logError("Processing terminated by An unexpected error");
e.printStackTrace(System.out);
@Override
public void handleRequest(InputStream input, OutputStream output, Context context)
throws RetriableError {
try {
String inputString = IOUtils.toString(input, "UTF-8");
Message request = parseQueueTrigger(inputString);
Message response = processor.processMessage(request);
serializer.serialize(output, response);
} catch (RetriableError e) {
IterableExtensionLogger.logError("Processing terminated by a RetriableError");
throw e;
} catch (Exception e) {
IterableExtensionLogger.logError("Processing terminated by An unexpected error");
e.printStackTrace(System.out);
}
}
}

public static Message parseQueueTrigger(String triggerString) throws IOException {
QueueTrigger trigger = mapper.readValue(triggerString, QueueTrigger.class);
String messageBody = trigger.records.get(0).body;
Message request = serializer.deserialize(messageBody, Message.class);
return request;
}
public static Message parseQueueTrigger(String triggerString) throws IOException {
QueueTrigger trigger = mapper.readValue(triggerString, QueueTrigger.class);
String messageBody = trigger.records.get(0).body;
Message request = serializer.deserialize(messageBody, Message.class);
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class QueueMessageBody {
@JsonProperty(value = "body", required = true)
public String body;
@JsonProperty(value = "body", required = true)
public String body;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class QueueTrigger {
@JsonProperty(value = "Records", required = true)
public List<QueueMessageBody> records;
@JsonProperty(value = "Records", required = true)
public List<QueueMessageBody> records;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import java.io.IOException;

/**
* Exception thrown when batch processing fails and should be retried.
*/
/** Exception thrown when batch processing fails and should be retried. */
public class RetriableError extends IOException {

public RetriableError() {
super();
}
public RetriableError() {
super();
}

public RetriableError(String message) {
super(message);
}
public RetriableError(String message) {
super(message);
}
}
Loading