Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.google.ads.googleads.lib;

import com.google.ads.googleads.lib.ExceptionTransformingCallable;
import com.google.ads.googleads.lib.ExceptionTransformingCallable.ExceptionTransformation;
import com.google.ads.googleads.lib.GoogleAdsExceptionTransformation;
import com.google.ads.googleads.lib.callables.ExceptionTransformation;
import com.google.ads.googleads.lib.callables.ExceptionTransformingServerStreamingCallable;
import com.google.ads.googleads.lib.callables.ExceptionTransformingUnaryCallable;
import com.google.ads.googleads.lib.callables.GoogleAdsExceptionTransformation;
import com.google.api.gax.grpc.GrpcCallSettings;
import com.google.api.gax.grpc.GrpcCallableFactory;
import com.google.api.gax.grpc.GrpcStubCallableFactory;
Expand All @@ -37,42 +38,61 @@
import com.google.longrunning.Operation;
import com.google.longrunning.stub.OperationsStub;

/**
* Defines the factory used to create instances for all Google Ads services.
*
* <p>Used in place of the default generated code to override the exceptions generated to throw
* GoogleAdsException instead of ApiException.
*/
public class GrpcGoogleAdsCallableFactory implements GrpcStubCallableFactory {

private static final ExceptionTransformation googleAdsExceptionTransformation = new GoogleAdsExceptionTransformation();
private static final ExceptionTransformation googleAdsExceptionTransformation =
new GoogleAdsExceptionTransformation();

public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, UnaryCallSettings<?, ?> callSettings, ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable = GrpcCallableFactory.createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext);
return new ExceptionTransformingCallable<>(callable, googleAdsExceptionTransformation);
public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
UnaryCallSettings<?, ?> callSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable =
GrpcCallableFactory.createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext);
return new ExceptionTransformingUnaryCallable<>(callable, googleAdsExceptionTransformation);
}

@Override
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
UnaryCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable = createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext);
UnaryCallable<RequestT, ResponseT> callable =
createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext);
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}

@Override
public <RequestT, ResponseT, PagedListResponseT>
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> pagedCallSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> innerCallable = createBaseUnaryCallable(grpcCallSettings, pagedCallSettings, clientContext);
UnaryCallable<RequestT, PagedListResponseT> pagedCallable = Callables.paged(innerCallable, pagedCallSettings);
UnaryCallable<RequestT, ResponseT> innerCallable =
createBaseUnaryCallable(grpcCallSettings, pagedCallSettings, clientContext);
UnaryCallable<RequestT, PagedListResponseT> pagedCallable =
Callables.paged(innerCallable, pagedCallSettings);
return pagedCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}

@Override
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
BatchingCallSettings<RequestT, ResponseT> batchingCallSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable = createBaseUnaryCallable(grpcCallSettings, batchingCallSettings, clientContext);
UnaryCallable<RequestT, ResponseT> callable =
createBaseUnaryCallable(grpcCallSettings, batchingCallSettings, clientContext);
callable = Callables.batching(callable, batchingCallSettings, clientContext);
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}

@Override
public <RequestT, ResponseT, MetadataT>
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
Expand All @@ -83,6 +103,7 @@ OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
grpcCallSettings, operationCallSettings, clientContext, operationsStub);
}

@Override
public <RequestT, ResponseT>
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
Expand All @@ -92,15 +113,20 @@ BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
grpcCallSettings, streamingCallSettings, clientContext);
}

@Override
public <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createServerStreamingCallable(
grpcCallSettings, streamingCallSettings, clientContext);
ServerStreamingCallable<RequestT, ResponseT> defaultCallable =
GrpcCallableFactory.createServerStreamingCallable(
grpcCallSettings, streamingCallSettings, clientContext);
return new ExceptionTransformingServerStreamingCallable(
defaultCallable, new GoogleAdsExceptionTransformation());
}

@Override
public <RequestT, ResponseT>
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.googleads.lib.callables;

/** Represents a transformation between {@link Throwable}s. */
public interface ExceptionTransformation {
Comment thread
jradcliff marked this conversation as resolved.
Outdated

/**
* Transforms an input throwable to an output throwable.
*
* <p>If no transformation is applied this must return the input throwable.
*/
Throwable transform(Throwable throwable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.googleads.lib.callables;

import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StreamController;

/**
* Wrapper around a {@link ServerStreamingCallable} which invokes an {@link ExceptionTransformation}
* for {@link Throwable}s which occur on the stream.
*
* <p>NOTE: This class could be pushed into the gax library, as it is not specific to the Google Ads
* API.
*/
public class ExceptionTransformingServerStreamingCallable<RequestT, ResponseT>
extends ServerStreamingCallable<RequestT, ResponseT> {

private final ServerStreamingCallable<RequestT, ResponseT> innerCallable;
private final ExceptionTransformation exceptionTransformation;

public ExceptionTransformingServerStreamingCallable(
ServerStreamingCallable<RequestT, ResponseT> innerCallable,
ExceptionTransformation exceptionTransformation) {
this.innerCallable = innerCallable;
this.exceptionTransformation = exceptionTransformation;
}

@Override
public void call(
RequestT request, ResponseObserver<ResponseT> responseObserver, ApiCallContext context) {
innerCallable.call(request, new ExceptionTransformingStreamObserver(responseObserver), context);
}

/** Provides a mechanism for transforming any exceptions which occur on the stream. */
private class ExceptionTransformingStreamObserver implements ResponseObserver<ResponseT> {
Comment thread
nwbirnie marked this conversation as resolved.
Outdated

private final ResponseObserver<ResponseT> innerObserver;

public ExceptionTransformingStreamObserver(ResponseObserver<ResponseT> innerObserver) {
this.innerObserver = innerObserver;
}

@Override
public void onStart(StreamController controller) {
innerObserver.onStart(controller);
}

@Override
public void onResponse(ResponseT response) {
innerObserver.onResponse(response);
}

@Override
public void onError(Throwable t) {
if (t instanceof ApiException) {
t = exceptionTransformation.transform(t);
}
innerObserver.onError(t);
}

@Override
public void onComplete() {
innerObserver.onComplete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.googleads.lib;
package com.google.ads.googleads.lib.callables;

import com.google.api.core.AbstractApiFuture;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.common.base.Preconditions;
import java.util.concurrent.CancellationException;

/**
* NOTE: This class could be pushed into the gax library, as it is not specific to the Google Ads
* Wrapper around a {@link UnaryCallable} which invokes an {@link ExceptionTransformation} for
* {@link Throwable}s which occur on the stream.
*
* <p>NOTE: This class could be pushed into the gax library, as it is not specific to the Google Ads
* API.
*/
public class ExceptionTransformingCallable<RequestT, ResponseT>
public class ExceptionTransformingUnaryCallable<RequestT, ResponseT>
extends UnaryCallable<RequestT, ResponseT> {

public interface ExceptionTransformation {
Throwable transform(ApiException throwable);
}

private final UnaryCallable<RequestT, ResponseT> callable;
private final ExceptionTransformation transformation;

public ExceptionTransformingCallable(
public ExceptionTransformingUnaryCallable(
UnaryCallable<RequestT, ResponseT> callable, ExceptionTransformation transformation) {
this.callable = Preconditions.checkNotNull(callable);
this.transformation = transformation;
Expand All @@ -55,6 +53,7 @@ public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputCon
return transformingFuture;
}

/** Provides a mechanism to transform exceptions which occur on the inner callable. */
private class ExceptionTransformingFuture extends AbstractApiFuture<ResponseT>
implements ApiFutureCallback<ResponseT> {
private ApiFuture<ResponseT> innerCallFuture;
Expand All @@ -79,8 +78,8 @@ public void onSuccess(ResponseT r) {
public void onFailure(Throwable throwable) {
if (throwable instanceof CancellationException && cancelled) {
// this just circled around, so ignore.
} else if (throwable instanceof ApiException) {
setException(transformation.transform((ApiException) throwable));
} else {
setException(transformation.transform(throwable));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ads.googleads.lib;
package com.google.ads.googleads.lib.callables;

import com.google.ads.googleads.lib.BaseGoogleAdsException;
import com.google.ads.googleads.lib.catalog.GeneratedCatalog;
import com.google.ads.googleads.lib.catalog.Version;
import com.google.api.gax.rpc.ApiException;
Expand All @@ -23,20 +24,21 @@
* Transforms an ApiException into a GoogleAdsException whenever a binary GoogleAdsFailure message
* was sent in the RPC trailers.
*/
public class GoogleAdsExceptionTransformation
implements ExceptionTransformingCallable.ExceptionTransformation {
public class GoogleAdsExceptionTransformation implements ExceptionTransformation {

private static final GeneratedCatalog catalog = GeneratedCatalog.getDefault();

@Override
public Throwable transform(ApiException apiException) {
for (Version version : catalog.getSupportedVersions()) {
Optional<? extends BaseGoogleAdsException> result =
version.getExceptionFactory().createGoogleAdsException(apiException);
if (result.isPresent()) {
return result.get();
public Throwable transform(Throwable input) {
if (ApiException.class.isAssignableFrom(input.getClass())) {
for (Version version : catalog.getSupportedVersions()) {
Optional<? extends BaseGoogleAdsException> result =
version.getExceptionFactory().createGoogleAdsException((ApiException) input);
if (result.isPresent()) {
return result.get();
}
}
}
return apiException;
return input;
}
}
Loading