Esempio n. 1
0
        /// <summary>
        /// Submits all unknown resources.
        /// TODO: Upon successful submission check the result and mark items that were reported as already present/accepted.
        /// </summary>
        public void SubmitUnknown()
        {
            if (!apiAvailable || !EnableSubmissions)
            {
                return;
            }

            var sortedstrings = from submitstring in unknownStrings where !submitstring.AlreadySubmitted group submitstring by new { submitstring.Culture, submitstring.ProviderType };

            foreach (var submitstringgroup in sortedstrings)
            {
                string response;                 // API response
                var    providertype = submitstringgroup.Key.ProviderType;
                var    culture      = submitstringgroup.Key.Culture;
                var    rawData      = submitstringgroup.Select(x => x.RawData).ToList();
                var    submission   = JsonConvert.SerializeObject(new SubmitUnknownApi(culture, rawData));

                Debug.WriteLine("Updater: serialising data for provider {0} and culture {1}.", culture, providertype);

                Debug.WriteLine("Updater: serialised data: " + submission);

                using (var client = new ViewerWebClient())
                {
                    var apiUri = new Uri(kcvApiUrl + "submit/" + providertype.ToString().ToLower());
                    client.Encoding = Encoding.UTF8;
                    client.Headers.Add("Content-Type", "application/json");
                    try
                    {
                        response = client.UploadString(apiUri, "POST", submission);
                    }
                    catch
                    {
                        Debug.WriteLine("Updater: couldn't talk to the API.");
                        continue;
                    }
                }
                // Parse the result and clean up here.
                Debug.WriteLine("Updater: received <" + response + "> from the server.");

                // Attempt to parse the response.
                SubmitUnknownApiResponse apiresponse;
                try { apiresponse = JsonConvert.DeserializeObject <SubmitUnknownApiResponse>(response); }
                catch { Debug.WriteLine("Updater: response could not be handled using the API."); continue; }

                if (!apiresponse.success)
                {
                    Debug.WriteLine("Updater: API returned an error: " + apiresponse.verbose + "."); return;
                }

                // Set already submitted flag on successful submission.
                foreach (var submitstring in submitstringgroup)
                {
                    unknownStrings.First(s => (s.Culture == culture) && (s.ProviderType == providertype) && (s.Equals(submitstring))).AlreadySubmitted = true;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Downloads translations for the specified provider.
        /// </summary>
        /// <param name="type">Provider type.</param>
        /// <returns></returns>
        private bool FetchTranslations(TranslationProviderType type)
        {
            if (!apiAvailable)
            {
                return(false);
            }

            if (type == TranslationProviderType.App)
            {
                return(false);
            }
            if (type == TranslationProviderType.Operations)
            {
                return(false);
            }
            if (type == TranslationProviderType.Expeditions)
            {
                return(false);
            }

            var apiUri = new Uri(kcvApiUrl + type.ToString().ToLower() + "/");

            using (var client = new ViewerWebClient())
            {
                byte[] responseBytes;
                try
                {
                    responseBytes = client.UploadValues(apiUri, "POST", this.DefaultRequestParameters());
                    Debug.WriteLine("{0}: API request sent for {1}, URL: {2}. Response: {3}.", nameof(Updater), type, apiUri, Encoding.UTF8.GetString(responseBytes));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("{0} API request sent for {1}, URL: {2}. Request failed with exception {3}.", nameof(Updater), type, apiUri, ex.Message);
                    return(false);
                }

                return(TranslationDataProvider.LoadJson(type, responseBytes));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Loads remote version data from the URL specified.
        /// </summary>
        /// <param name="url">Remote version data URL.</param>
        /// <returns>True if version information was retrieved and parsed successfully.</returns>
        private bool LoadVersions(string url)
        {
            using (ViewerWebClient client = new ViewerWebClient())
            {
                byte[] responseBytes;

                try
                {
                    responseBytes = client.UploadValues(url, "POST", this.DefaultRequestParameters());
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Updater: Could not access the API, reason: " + ex.Message + ".");
                    return(false);
                }

                kcvapi_version rawResult;
                if (!this.TryConvertTo(responseBytes, out rawResult))
                {
                    return(false);
                }

                bool cultureAvailable = rawResult.selected_culture == CurrentCulture;

                if (!cultureAvailable)
                {
                    Debug.WriteLine("Updater: server returned a different culture; expected {0}, got {1}.", CurrentCulture, rawResult.selected_culture);
                }

                Version apiRemoteVersion;

                if (!Version.TryParse(rawResult.api_version, out apiRemoteVersion) && (apiRemoteVersion.CompareTo("1.0") >= 0))
                {
                    Debug.WriteLine("Updater: Server API version check failed.");
                    return(false);
                }

                kcvApiUrl = rawResult.api_url;

                Debug.WriteLine("Updater: remote API version {0}; providers available: {1}.", rawResult.api_version, rawResult.components.Count());

                foreach (var component in rawResult.components)
                {
                    Debug.WriteLine("Updater: provider {0}: version {1}{2}.", component.type, component.version, string.IsNullOrEmpty(component.url) ? "" : " (" + component.url + ")");
                    var typeTemp = TranslationDataProvider.StringToTranslationProviderType(component.type);
                    if (typeTemp == null)
                    {
                        continue;
                    }
                    // Enforce version = 1 for unsupported cultures
                    // versions[typeTemp.Value] = (!cultureAvailable && (typeTemp != TranslationProviderType.App)) ? "1" : component.version;
                    // If not enforced, upon adding a new culture its components' versions must be set to values greater than those for 'en'.
                    versions[typeTemp.Value] = component.version;
                    if (typeTemp == TranslationProviderType.App)
                    {
                        downloadUrl = component.url;                                                              // TODO: proper implementation of overrides for all resource types
                    }
                }
            }

            return(true);
        }