示例#1
0
        public async Task <bool> AddUserDictionaryItemAsync(
            ClassiiiUser classiiiUser, UserDictionaryItem item)
        {
            string url = baseUrl + "/dictionary";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "fromLang", item.fromLang },
                { "fromText", item.fromText },
                { "toLang", item.toLang },
                { "toText", item.toText }
            };

            var content = await HttpUtils.SendAsync(HttpMethod.Post, url, headers, body);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "" };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("add user dictionary item failed");
            }

            return(true);
        }
示例#2
0
        private static async System.Threading.Tasks.Task TestUserDictionary(string baseUrl, RozettaApiUser rozettaApiUser)
        {
            UserDictionaryClient userDictionaryClient = new UserDictionaryClient(baseUrl);

            UserDictionaryItem item = new UserDictionaryItem {
                fromLang = "en", fromText = "hello", toLang = "ja", toText = "おはよう"
            };

            bool bRet;

            string[] text = new string[] { "hello" };
            TextTranslationOption option = new TextTranslationOption
            {
                FieldId    = 1,
                SourceLang = "en",
                TargetLang = "ja"
            };
            TextTranslationClient textTranslationClient = new TextTranslationClient(baseUrl);

            TextTranslationResult[] textTranslationResults;
            UserDictionaryItem[]    userDictionaryItems;

            // ユーザー辞書を登録
            bRet = await userDictionaryClient.AddUserDictionaryItemAsync(rozettaApiUser, item);

            Debug.Assert(bRet);

            // ユーザー辞書を取得
            userDictionaryItems = await userDictionaryClient.GetUserDictionaryAsync(rozettaApiUser);

            Debug.Assert(userDictionaryItems.Length == 1);
            Debug.Assert(userDictionaryItems[0] == item);

            // ユーザー辞書を確認 -----------------------------------------------------
            textTranslationResults = await textTranslationClient.TranslateTextBySyncModeAsync(rozettaApiUser, option, text);

            Debug.Assert(textTranslationResults.Length == 1);
            Debug.Assert(textTranslationResults[0].sourceText == "hello");
            Debug.Assert(textTranslationResults[0].translatedText == "おはよう");
            // ユーザー辞書を確認 ----------------------------------------------------- end


            // ユーザー辞書を削除
            bRet = await userDictionaryClient.DeleteUserDictionaryItemAsync(rozettaApiUser, userDictionaryItems[0].id);

            Debug.Assert(bRet);

            // ユーザー辞書数を確認
            userDictionaryItems = await userDictionaryClient.GetUserDictionaryAsync(rozettaApiUser);

            Debug.Assert(userDictionaryItems.Length == 0);
        }
示例#3
0
        public async Task <bool> AddUserDictionaryItemAsync(
            RozettaApiUser rozettaApiUser, UserDictionaryItem item)
        {
            string url = baseUrl + "/dictionary";

            string jwtToken = await HttpUtils.GenerateJwtDataAsync(
                rozettaApiUser.AccessKey,
                rozettaApiUser.SecretKey,
                this.defautDuration,
                this.jwtTokenRequestUrl);

            Dictionary <string, object> headers = HttpUtils.BuildJwtHeaders(jwtToken);

            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "fromLang", item.fromLang },
                { "fromText", item.fromText },
                { "toLang", item.toLang },
                { "toText", item.toText }
            };

            var content = await HttpUtils.SendAsync(HttpMethod.Post, url, headers, body);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "" };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("add user dictionary item failed");
            }

            return(true);
        }