コード例 #1
0
        /// <summary>
        /// Background task work method.
        /// Contacts the WaniKani API.
        /// </summary>
        private void DoRequestApi(object sender, DoWorkEventArgs e)
        {
            string[] responses;
            try
            {
                List<string> uri = new List<string>();
                if (_parent.ImportMode == WkImportMode.All || _parent.ImportMode == WkImportMode.Kanji)
                {
                    uri.Add(string.Format("https://www.wanikani.com/api/v1.2/user/{0}/kanji", _parent.ApiKey));
                }
                if (_parent.ImportMode == WkImportMode.All || _parent.ImportMode == WkImportMode.Vocab)
                {
                    uri.Add(string.Format("https://www.wanikani.com/api/v1.2/user/{0}/vocabulary/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25", _parent.ApiKey));
                    uri.Add(string.Format("https://www.wanikani.com/api/v1.2/user/{0}/vocabulary/26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50", _parent.ApiKey));
                }   

                responses = new string[uri.Count];

                for (int i = 0; i < uri.Count(); i++)
                {
                    responses[i] = Request(uri[i]);
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogger("WkImport").Error("An error occured during the request to the WaniKani API.", ex);
                Error = "An error occured while trying to request the data. Please consult your log file for more details.";
                IsError = true;
                return;
            }

            // Now read that response!
            try
            {
                JObject[] jroots = new JObject[responses.Count()];
                bool isOk = true;
                for (int i = 0; i < jroots.Count(); i++)
                {
                    jroots[i] = JObject.Parse(responses[i]);
                    if (CheckError(jroots[i]))
                    {
                        isOk = false;
                        break;
                    }
                }

                if (isOk)
                {
                    // No error.
                    WkImportResult result = new WkImportResult();
                    result.Username = (string)jroots[0]["user_information"]["username"];
                    List<WkItem> items = new List<WkItem>();

                    for (int i = 0; i < jroots.Count(); i++)
                    {
                        foreach (WkItem item in ReadItems(jroots[i], i == 0 && (_parent.ImportMode == WkImportMode.All || _parent.ImportMode == WkImportMode.Kanji)))
                        {
                            items.Add(item);
                        }
                    }
                    result.Items = items;

                    Result = result;
                    IsComplete = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogger("WkImport").Error("An error occured during the JSON parsing.", ex);
                Error = "An error occured while trying to read the data. Please consult your log file for more details.";
                IsError = true;
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// Starts a background task to perform the request in the background.
        /// </summary>
        private void RequestApi()
        {
            // Check the worker
            if (_worker != null)
            {
                _worker.Dispose();
            }

            // Initialize values
            IsComplete = false;
            IsError = false;
            IsWorking = true;
            Error = string.Empty;
            Result = null;

            // Run the initialization in the background.
            _worker = new BackgroundWorker();
            _worker.DoWork += DoRequestApi;
            _worker.RunWorkerCompleted += DoneRequestApi;
            _worker.RunWorkerAsync();
        }