Search in sources :

Example 1 with LangResponse

use of com.aimmatic.natural.voice.rest.response.LangResponse in project natural-voice-mobile-sdk-android by aimmatic.

the class Language method loadLanguage.

@VisibleForTesting
static void loadLanguage(final AppContext appContext, final Callback callback) {
    OkHttpClient client = appContext.getOkHttpClient();
    final Request request = new Request.Builder().url(appContext.getHost() + Resources.ApiVersion + Resources.NaturalVoiceLanguage).build();
    final File dir = appContext.getDataDir();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            if (callback != null)
                callback.onFailure(call, e);
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            if (response.code() == 200 && response.body() != null) {
                ResponseBody responseBody = response.body();
                BufferedSource source = responseBody.source();
                // request the entire body.
                source.request(Long.MAX_VALUE);
                Buffer buffer = source.buffer();
                String resp = buffer.clone().readString(Charset.forName("UTF-8"));
                LangResponse lr = new Gson().fromJson(resp, LangResponse.class);
                File file = new File(dir, "aimmatic-speech-lang.json");
                FileOutputStream out = new FileOutputStream(file);
                String data = new Gson().toJson(lr.getLanguages());
                out.write(data.getBytes());
                out.close();
            }
            if (callback != null)
                callback.onResponse(call, response);
        }
    });
}
Also used : Buffer(okio.Buffer) Call(okhttp3.Call) LangResponse(com.aimmatic.natural.voice.rest.response.LangResponse) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Gson(com.google.gson.Gson) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) LangResponse(com.aimmatic.natural.voice.rest.response.LangResponse) Response(okhttp3.Response) Callback(okhttp3.Callback) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedSource(okio.BufferedSource) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 2 with LangResponse

use of com.aimmatic.natural.voice.rest.response.LangResponse in project natural-voice-mobile-sdk-android by aimmatic.

the class GetLangTest method testGetLanguage.

@Test
public void testGetLanguage() throws InterruptedException, IOException {
    final AsyncWait cdl = new AsyncWait(1);
    TestAppContext appContext = new TestAppContext();
    Language.loadLanguage(appContext, new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            cdl.failed = true;
            cdl.countDown();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.code() == 200 && response.body() != null) {
                String resp = response.body().string();
                LangResponse lr = new Gson().fromJson(resp, LangResponse.class);
                cdl.response = new Gson().toJson(lr.getLanguages());
                cdl.failed = false;
            } else {
                cdl.failed = true;
            }
            cdl.countDown();
        }
    });
    cdl.await();
    Assert.assertFalse("Loading language failed", cdl.failed);
    File file = new File(appContext.getDataDir(), "aimmatic-speech-lang.json");
    Assert.assertTrue("File not existed", file.exists());
    FileInputStream in = new FileInputStream(file);
    byte[] bf = new byte[in.available()];
    in.read(bf);
    in.close();
    Assert.assertTrue("Response is not identical", new String(bf).compareTo((String) cdl.response) == 0);
}
Also used : Call(okhttp3.Call) LangResponse(com.aimmatic.natural.voice.rest.response.LangResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Response(okhttp3.Response) LangResponse(com.aimmatic.natural.voice.rest.response.LangResponse) Callback(okhttp3.Callback) File(java.io.File) Test(org.junit.Test)

Aggregations

LangResponse (com.aimmatic.natural.voice.rest.response.LangResponse)2 Gson (com.google.gson.Gson)2 File (java.io.File)2 IOException (java.io.IOException)2 Call (okhttp3.Call)2 Callback (okhttp3.Callback)2 Response (okhttp3.Response)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 ResponseBody (okhttp3.ResponseBody)1 Buffer (okio.Buffer)1 BufferedSource (okio.BufferedSource)1 Test (org.junit.Test)1