Пример #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
        // TODO: Add none async method
        public static async Task <MojiSearchResponse> SearchAsync(string query, CancellationToken token = default)
        {
            var client = new RestClient(baseAddress);

            MojiSearchPayload searchPayload = new MojiSearchPayload
            {
                langEnv         = "zh-CN_ja",                             // no need
                needWords       = "true",
                searchText      = query,
                _ApplicationId  = "E62VyFVLMiW7kvbtVq3p",                 // no need
                _ClientVersion  = "js2.12.0",                             // no need
                _InstallationId = "7d959a18-48c4-243c-7486-632147466544", // no need
                _SessionToken   = DataRepository.MojiSessionToken
            };

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

            MojiSearchResponse resp = new();

            try
            {
                resp = await client.PostAsync <MojiSearchResponse>(request).ConfigureAwait(false);


                if (token.IsCancellationRequested)
                {
                    resp.StatusCode = ResponseStatus.Aborted;
                    Log.Debug("Moji search task was canceled");
                }
                else if (resp.Result.Words.Count == 0)
                {
                    // There is no ResponseStatus.NotFound, so make it None
                    // None means no content, like string.Empty
                    resp.StatusCode = ResponseStatus.None;
                }
                else
                {
                    resp.StatusCode = ResponseStatus.Completed;
                }
            }
            catch (Exception ex)
            {
                // Same as `resp.Result.OriginalSearchText == string.Empty`
                // Any network transport error (network is down, failed DNS lookup, etc), or any kind of server error
                // (except 404), `resp.StatusCode` will be set to `ResponseStatus.Error`.
                resp.StatusCode = ResponseStatus.Error;
                Log.Error(ex);
            }

            return(resp);
        }