Exemplo n.º 1
0
        public override async void FlushEvents(ICollection <JObject> events)
        {
            if (!Settings.Verified)
            {
                return;
            }
            if (string.IsNullOrEmpty(Settings.ApiKey))
            {
                return;
            }

            try
            {
                var apiFacade        = new EdsmApiFacade(RestClient, GlobalSettings.CommanderName, Settings.ApiKey);
                var apiEventsBatches = events
                                       .Where(e => !ignoredEvents.Result.Contains(e["event"].ToString()))
                                       .TakeLast(1000) // Limit to last N events to avoid EDSM overload
                                       .Reverse()
                                       .Batch(100)     // EDSM API only accepts 100 events in single call
                                       .ToList();
                foreach (var batch in apiEventsBatches)
                {
                    await apiFacade.PostLogEvents(batch.ToArray());
                }
                Log.Info()
                .Message("Uploaded {0} events", events.Count)
                .Property("eventsCount", events.Count)
                .Write();
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while processing events");
            }
        }
Exemplo n.º 2
0
        public override async void FlushEvents(ICollection <JObject> events)
        {
            try
            {
                var commander = CurrentCommander;
                if (commander != null && ApiKeys.TryGetValue(commander.Name, out string apiKey))
                {
                    var apiFacade        = new EdsmApiFacade(RestClient, commander.Name, apiKey);
                    var apiEventsBatches = events
                                           .Where(e => !ignoredEvents.Result.Contains(e["event"].ToString()))
                                           .Reverse()
                                           .Batch(100) // EDSM API only accepts 100 events in single call
                                           .ToList();
                    foreach (var batch in apiEventsBatches)
                    {
                        await apiFacade.PostLogEvents(batch.ToArray());
                    }

                    Log.Info()
                    .Message("Uploaded events")
                    .Property("eventsCount", events.Count)
                    .Property("commander", commander)
                    .Write();
                }
                else
                {
                    Log.Info()
                    .LoggerName(Log.Name)
                    .Message("No EDSM API key set for commander, events discarded")
                    .Property("eventsCount", events.Count)
                    .Property("commander", commander?.Name ?? "null")
                    .Write();
                }
            }
            catch (InvalidApiKeyException)
            {
                notificationInterface.ShowErrorNotification("Invalid EDSM API key for CMDR " + CurrentCommander?.Name);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while processing events for EDSM");
            }
        }
Exemplo n.º 3
0
        public async Task <bool> ValidateKeyAsync(string cmdrName, string apiKey)
        {
            try
            {
                var apiFacade = new EdsmApiFacade(new ThrottlingRestClient.Factory().CreateRestClient("https://www.edsm.net/api-commander-v1/get-ranks"), cmdrName, apiKey);
                var result    = await apiFacade.GetCommanderRanks();

                var combatRank = result?["ranksVerbose"]?["Combat"]?.ToString();
                return(combatRank != null);
            }
            catch (InvalidApiKeyException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                Log.Info(ex, "Exception while validating API key");
                return(false);
            }
        }