Last active
July 28, 2020 19:38
-
-
Save nsivabalan/da752a1a02937c8b83ee432a2cf4562e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface UberCallHeaders { | |
| String getHeaderValue(String key); | |
| void setHeader(String key, String value); | |
| Set<String> getAllHeaderKeys(); | |
| Map<String, String> getHeadersAsMap(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface UberNetworkRequestStatus { | |
| public int getResponseCode(); | |
| public String getResponseMessage(); | |
| public boolean isSuccess() ; | |
| public Throwable getThrowable(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * An enum to represent the type of method. | |
| */ | |
| public enum MethodType { | |
| /** | |
| * One request message followed by one response message. | |
| */ | |
| UNARY, | |
| /** | |
| * Zero or more request messages followed by one response message. | |
| */ | |
| CLIENT_STREAMING, | |
| /** | |
| * One request message followed by zero or more response messages. | |
| */ | |
| SERVER_STREAMING, | |
| /** | |
| * Zero or more request and response messages arbitrarily interleaved in time. | |
| */ | |
| BIDI_STREAMING, | |
| /** | |
| * An unknown type. | |
| */ | |
| UNKNOWN; | |
| /** | |
| * Returns {@code true} if the client will immediately send one request message to the server | |
| * after calling {@link UberClientCall#start(Listener, RequestHeaders)} | |
| * and then immediately half-close the stream by calling {@link UberClientCall#halfClose()}. | |
| * | |
| */ | |
| public final boolean clientSendsOneMessage() { | |
| return this == UNARY || this == SERVER_STREAMING; | |
| } | |
| /** | |
| * Returns {@code true} if the server will immediately send one response message to the client | |
| * upon receipt of {@link Listener#onHalfClose()} and then immediately | |
| * close the stream by calling | |
| * {@link ServerCall#close(UberNetworkRequestStatus, TrailingHeaders)}. | |
| * | |
| */ | |
| public final boolean serverSendsOneMessage() { | |
| return this == UNARY || this == CLIENT_STREAMING; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Generic interface to represent MethodInfo fo type <T>. | |
| * @param <T> | |
| */ | |
| public interface MethodInfo<T> { | |
| /** | |
| * @returns the methodInfo object as is. | |
| */ | |
| T getMethodInfo(); | |
| /** | |
| * @returns the urlpath/endpoint of the request. | |
| */ | |
| String getUrlPath(); | |
| /** | |
| * @returns the servicename. Applicable for grpc transport. | |
| */ | |
| String getServiceName(); | |
| /** | |
| * @returns {@code true} if the method represents a unary calls. {@code false} otherwise. | |
| */ | |
| boolean isUnary(); | |
| /** | |
| * @returns the {@link MethodType} of the request. | |
| */ | |
| MethodType getMethodType(); | |
| /** | |
| * @returns {@code true} if the method is idempotent. {@link false} otherwise. | |
| */ | |
| boolean isIdempotent(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GrpcMethodInfo<ReqT, RespT> implements MethodInfo<MethodDescriptor> { | |
| private MethodDescriptor<ReqT, RespT> methodDescriptor; | |
| public GrpcMethodInfo(MethodDescriptor<ReqT, RespT> methodDescriptor){ | |
| this.methodDescriptor = methodDescriptor; | |
| } | |
| @Override | |
| public MethodDescriptor<ReqT, RespT> getMethodInfo() { | |
| return methodDescriptor; | |
| } | |
| @Override | |
| public String getUrlPath() { | |
| return methodDescriptor.getFullMethodName(); | |
| } | |
| @Override | |
| public String getServiceName() { | |
| return methodDescriptor.getServiceName(); | |
| } | |
| @Override | |
| public boolean isUnary() { | |
| return methodDescriptor.getType() == io.grpc.MethodDescriptor.MethodType.UNARY; | |
| } | |
| @Override | |
| public MethodType getMethodType() { | |
| return convertToUberMethodType(methodDescriptor.getType()); | |
| } | |
| @Override | |
| public boolean isIdempotent() { | |
| return methodDescriptor.isIdempotent(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Interface for client side UberInterceptor at mobile networking layer | |
| * @param <ReqT> request object of generic type. | |
| * @param <RespT> response object of generic type. | |
| */ | |
| public abstract class UberInterceptor<ReqT, RespT> extends UberInterceptorInternal<ReqT, RespT> { | |
| // MethodInfo is available via getMethodInfo() from base class. | |
| // UbercallOptions is available via getUberCallOptions() from base class. | |
| /** | |
| * Invoked with outgoing request headers. | |
| * @param requestHeaders request headers that are sent as part of the request | |
| */ | |
| protected abstract void onRequestHeaders(UberCallHeaders requestHeaders); | |
| /** | |
| * Invoked when a message is sent out from client to server. | |
| * @param message message being sent out. | |
| */ | |
| protected abstract void onRequestMessage(ReqT message); | |
| /** | |
| * Invoked with response headers. | |
| * @param responseHeaders response headers that are received from the server. | |
| */ | |
| protected abstract void onResponseHeaders(UberCallHeaders responseHeaders); | |
| /** | |
| * Invoked with message received from the server. | |
| * @param msg message being received. | |
| */ | |
| protected abstract void onResponseMessage(RespT msg); | |
| /** | |
| * Invoked when the call is complete | |
| * @param status instance of UberNetworkRequestStatus with final status of the request/call. | |
| * @param trailingHeaders final set of headers if any. | |
| */ | |
| protected void onComplete(UberNetworkRequestStatus status, UberCallHeaders trailingHeaders) {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment