1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16package com.android.voicemail.impl.transcribe.grpc;
17
18import android.support.annotation.Nullable;
19import android.support.annotation.WorkerThread;
20import com.android.dialer.common.Assert;
21import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailRequest;
22import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailResponse;
23import com.google.internal.communications.voicemailtranscription.v1.VoicemailTranscriptionServiceGrpc;
24import io.grpc.Status;
25import io.grpc.StatusRuntimeException;
26
27/** Wrapper around Grpc transcription server stub */
28public class TranscriptionClient {
29
30  private final VoicemailTranscriptionServiceGrpc.VoicemailTranscriptionServiceBlockingStub stub;
31
32  /** Wraps the server response and status objects, either of which may be null. */
33  public static class TranscriptionResponseWrapper {
34    public final TranscribeVoicemailResponse response;
35    public final Status status;
36
37    public TranscriptionResponseWrapper(
38        @Nullable TranscribeVoicemailResponse response, @Nullable Status status) {
39      Assert.checkArgument(!(response == null && status == null));
40      this.response = response;
41      this.status = status;
42    }
43  }
44
45  TranscriptionClient(
46      VoicemailTranscriptionServiceGrpc.VoicemailTranscriptionServiceBlockingStub stub) {
47    this.stub = stub;
48  }
49
50  @WorkerThread
51  public TranscriptionResponseWrapper transcribeVoicemail(TranscribeVoicemailRequest request) {
52    TranscribeVoicemailResponse response = null;
53    Status status = null;
54    try {
55      response = stub.transcribeVoicemail(request);
56    } catch (StatusRuntimeException e) {
57      status = e.getStatus();
58    }
59    return new TranscriptionClient.TranscriptionResponseWrapper(response, status);
60  }
61}
62