Example #1
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));
            }
        }
Example #2
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);
        }