Skip to content

Commit

Permalink
Remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
HindujaB committed Nov 18, 2024
1 parent 17b133f commit aa40a35
Showing 1 changed file with 28 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,73 +120,56 @@ public static Object executeSubscriptionResource(Environment environment, BObjec
fieldName.getValue().equals(resourceMethod.getResourcePath()[0])) {
ArgumentHandler argumentHandler =
new ArgumentHandler(resourceMethod, context, fieldObject, responseGenerator, validation);
try {
argumentHandler.validateInputConstraint(environment);
} catch (ConstraintValidationException e) {
return null;
}
return environment.yieldAndRun(() -> {
CompletableFuture<Object> subscriptionFutureResult = new CompletableFuture<>();
ExecutionCallback executionCallback = new ExecutionCallback(subscriptionFutureResult);
Object[] args = argumentHandler.getArguments();
HashMap<String, Object> properties = null;
if (ObserveUtils.isObservabilityEnabled() && ObserveUtils.isTracingEnabled()) {
properties = getPropertiesToPropagate(environment, context);
}
try {
Object result = callResourceMethod(environment.getRuntime(), service, resourceMethod.getName()
, properties, args);
executionCallback.notifySuccess(result);
} catch (BError bError) {
executionCallback.notifyFailure(bError);
}
return getResult(subscriptionFutureResult);
});
return getResultObject(environment, context, service, resourceMethod.getName(), argumentHandler);
}
}
return null;
}

private static Object callResourceMethod(Runtime runtime, BObject service, String methodName,
HashMap<String, Object> properties, Object[] args) {
ObjectType objectType = (ObjectType) TypeUtils.getReferredType(TypeUtils.getType(service));
boolean isIsolated = objectType.isIsolated() && objectType.isIsolated(methodName);
StrandMetadata strandMetadata = new StrandMetadata(isIsolated, properties);
return runtime.callMethod(service, methodName, strandMetadata, args);
}

public static Object executeQueryResource(Environment environment, BObject context, BObject service,
ResourceMethodType resourceMethod, BObject fieldObject,
BObject responseGenerator, boolean validation) {
if (resourceMethod == null) {
return null;
}
ArgumentHandler argumentHandler =
new ArgumentHandler(resourceMethod, context, fieldObject, responseGenerator, validation);
private static Object getResultObject(Environment environment, BObject context, BObject service,
String methodName, ArgumentHandler argumentHandler) {
try {
argumentHandler.validateInputConstraint(environment);
} catch (ConstraintValidationException e) {
return null;
}
return environment.yieldAndRun(() -> {
CompletableFuture<Object> future = new CompletableFuture<>();
ExecutionCallback executionCallback = new ExecutionCallback(future);
Object[] arguments = argumentHandler.getArguments();
CompletableFuture<Object> subscriptionFutureResult = new CompletableFuture<>();
ExecutionCallback executionCallback = new ExecutionCallback(subscriptionFutureResult);
Object[] args = argumentHandler.getArguments();
HashMap<String, Object> properties = null;
if (ObserveUtils.isObservabilityEnabled() && ObserveUtils.isTracingEnabled()) {
properties = getPropertiesToPropagate(environment, context);
}
try {
Object result = callResourceMethod(environment.getRuntime(), service, resourceMethod.getName(),
properties, arguments);
Object result = callResourceMethod(environment.getRuntime(), service, methodName, properties, args);
executionCallback.notifySuccess(result);
} catch (BError bError) {
executionCallback.notifyFailure(bError);
}
return getResult(future);
return getResult(subscriptionFutureResult);
});
}

private static Object callResourceMethod(Runtime runtime, BObject service, String methodName,
HashMap<String, Object> properties, Object[] args) {
ObjectType objectType = (ObjectType) TypeUtils.getReferredType(TypeUtils.getType(service));
boolean isIsolated = objectType.isIsolated() && objectType.isIsolated(methodName);
StrandMetadata strandMetadata = new StrandMetadata(isIsolated, properties);
return runtime.callMethod(service, methodName, strandMetadata, args);
}

public static Object executeQueryResource(Environment environment, BObject context, BObject service,
ResourceMethodType resourceMethod, BObject fieldObject,
BObject responseGenerator, boolean validation) {
if (resourceMethod == null) {
return null;
}
ArgumentHandler argumentHandler =
new ArgumentHandler(resourceMethod, context, fieldObject, responseGenerator, validation);
return getResultObject(environment, context, service, resourceMethod.getName(), argumentHandler);
}

public static Object executeMutationMethod(Environment environment, BObject context, BObject service,
BObject fieldObject, BObject responseGenerator, boolean validation) {
ServiceType serviceType = (ServiceType) TypeUtils.getType(service);
Expand All @@ -195,28 +178,7 @@ public static Object executeMutationMethod(Environment environment, BObject cont
if (remoteMethod.getName().equals(fieldName)) {
ArgumentHandler argumentHandler =
new ArgumentHandler(remoteMethod, context, fieldObject, responseGenerator, validation);
try {
argumentHandler.validateInputConstraint(environment);
} catch (ConstraintValidationException e) {
return null;
}
return environment.yieldAndRun(() -> {
CompletableFuture<Object> future = new CompletableFuture<>();
ExecutionCallback executionCallback = new ExecutionCallback(future);
Object[] arguments = argumentHandler.getArguments();
HashMap<String, Object> properties = null;
if (ObserveUtils.isObservabilityEnabled() && ObserveUtils.isTracingEnabled()) {
properties = getPropertiesToPropagate(environment, context);
}
try {
Object result = callResourceMethod(environment.getRuntime(), service, remoteMethod.getName(),
properties, arguments);
executionCallback.notifySuccess(result);
} catch (BError bError) {
executionCallback.notifyFailure(bError);
}
return getResult(future);
});
return getResultObject(environment, context, service, remoteMethod.getName(), argumentHandler);
}
}
return null;
Expand Down

0 comments on commit aa40a35

Please sign in to comment.