示例#1
0
        // Todo 5: return custom Type
        public async Task <string> RequestAsync(string query)
        {
            MojiSearchPayload searchPayload = new MojiSearchPayload
            {
                //langEnv = "zh-CN_ja",
                needWords      = "true",
                searchText     = query,
                _ApplicationId = "E62VyFVLMiW7kvbtVq3p",
                //_ClientVersion = "",
                //_InstallationId = "",
                //_SessionToken = ""
            };

            var request = new RestRequest(searchApi, Method.POST)
                          .AddJsonBody(searchPayload);

            var searchResp = await client.PostAsync <MojiSearchResponse>(request).ConfigureAwait(false);

            //if(searchResp.result.words.Count != 0)
            //{
            MojiFetchPayload fetchPayload = new MojiFetchPayload
            {
                wordId         = searchResp.result.searchResults[0].tarId,
                _ApplicationId = "E62VyFVLMiW7kvbtVq3p",
            };

            var requestFetch = new RestRequest(fetchApi, Method.POST)
                               .AddJsonBody(fetchPayload);
            var fetchResp = await client.PostAsync <MojiFetchResponse>(requestFetch).ConfigureAwait(false);

            //}

            return(fetchResp.result.word.spell);
        }
示例#2
0
        public static async Task <MojiFetchResponse> FetchAsync(string tarId, CancellationToken token)
        {
            var client = new RestClient(baseAddress);

            MojiFetchPayload fetchPayload = new MojiFetchPayload
            {
                wordId          = tarId,
                _ApplicationId  = "E62VyFVLMiW7kvbtVq3p",                 // no need
                _ClientVersion  = "js2.12.0",                             // no need
                _InstallationId = "7d959a18-48c4-243c-7486-632147466544", // no need
                _SessionToken   = DataRepository.MojiSessionToken         // no need
            };

            IRestRequest?requestFetch = new RestRequest(fetchApi, Method.POST)
                                        .AddJsonBody(fetchPayload);

            MojiFetchResponse resp = new();

            try
            {
                resp = await client.PostAsync <MojiFetchResponse>(requestFetch).ConfigureAwait(false);

                if (token.IsCancellationRequested)
                {
                    resp.StatusCode = ResponseStatus.Aborted;
                    Log.Debug("Moji fetch task was canceled");
                }
                else
                {
                    resp.StatusCode = ResponseStatus.Completed;
                }
            }
            catch (Exception ex)
            {
                resp.StatusCode = ResponseStatus.Error;
                Log.Error(ex);
            }

            return(resp);
        }