Пример #1
0
		public async Task<Dictionary<string, string>> Validate(Settings settings, IMessageManager messageManager = null)
		{
			_logger.Info("Validating settings...");
			var errors = new Dictionary<string, string>();

			// pinballx folder
			if (string.IsNullOrEmpty(settings.PbxFolder)) {
				errors.Add("PbxFolder", "The folder where PinballX is installed must be set.");
			} else if (!Directory.Exists(settings.PbxFolder) || !Directory.Exists(settings.PbxFolder + @"\Config")) {
				errors.Add("PbxFolder", "The folder \"" + settings.PbxFolder + "\" is not a valid PinballX folder.");
			}

			// network params
			if (string.IsNullOrEmpty(settings.ApiKey)) {
				errors.Add("ApiKey", "The API key is mandatory and needed in order to communicate with VPDB.");
			}
			if (string.IsNullOrEmpty(settings.Endpoint)) {
				errors.Add("Endpoint", "The endpoint is mandatory. In doubt, put \"https://vpdb.io\".");
			}

			// xml file name
			var badFilenameChars = new Regex("[\\\\" + Regex.Escape(new string(Path.GetInvalidPathChars())) + "]");
			var filename = settings.XmlFile[Platform.PlatformType.VP];
			if (string.IsNullOrWhiteSpace(filename)) {
				errors.Add("XmlFileVP", "You need to provide a file name for the XML database.");
			} else if (badFilenameChars.IsMatch(filename)) {
				errors.Add("XmlFileVP", "That doesn't look like a valid file name!");
			} else if (filename.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) {
				errors.Add("XmlFileVP", "No need to provide the .xml extension, we'll do that!");
			}

			// test params if set
			if (!string.IsNullOrEmpty(settings.ApiKey) && !string.IsNullOrEmpty(settings.Endpoint)) {
				try {
					var handler = new AuthenticatedHttpClientHandler(settings.ApiKey, settings.AuthUser, settings.AuthPass);
					var client = new HttpClient(handler) {BaseAddress = new Uri(settings.Endpoint)};
					var s = new RefitSettings { JsonSerializerSettings = new JsonSerializerSettings {ContractResolver = new SnakeCasePropertyNamesContractResolver()} };
					var api = RestService.For<IVpdbApi>(client, s);
					var user = await api.GetProfile().SubscribeOn(Scheduler.Default).ToTask();

					_logger.Info("Logged as <{0}>", user.Email);
					OnValidationResult(user);

				} catch (ApiException e) {
					HandleApiError(errors, e);
					OnValidationResult(null);
					messageManager?.LogApiError(e, "Error while logging in");

				} catch (Exception e) {
					errors.Add("ApiKey", e.Message);
					OnValidationResult(null);
					messageManager?.LogError(e, "Error while logging in");
				}
			}
			settings.IsValidated = errors.Count == 0;

			return errors;
		}
Пример #2
0
        public IVpdbClient Initialize()
        {
            // setup rest client
            var handler = new AuthenticatedHttpClientHandler(_settingsManager.Settings.ApiKey, _settingsManager.Settings.AuthUser, _settingsManager.Settings.AuthPass);
            // todo enable gzip in api!!
            // AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

            var client = new HttpClient(handler) {
                BaseAddress = new Uri(_settingsManager.Settings.Endpoint)
            };
            var settings = new RefitSettings {
                JsonSerializerSettings = new JsonSerializerSettings {
                    ContractResolver = new SnakeCasePropertyNamesContractResolver()
                }
            };
            Api = RestService.For<IVpdbApi>(client, settings);

            // subscribe to pusher if profile allows
            _settingsManager.ApiAuthenticated.Subscribe(user => {
                if (user != null && user.Permissions.Messages?.Contains("receive") == true) {
                    SetupPusher(user);
                }
            }, exception => HandleApiError(exception, "subscribing to ApiAuthenticated for Pusher"));

            return this;
        }