コード例 #1
0
ファイル: InaraService.cs プロジェクト: Test-of-gource/EDDI-1
        public void SendQueuedAPIEvents(bool eddiIsBeta)
        {
            List <InaraAPIEvent> queue = new List <InaraAPIEvent>();

            // The `GetConsumingEnumerable` method blocks the thread while the underlying collection is empty
            // If we haven't extracted events to send to Inara, this will wait / pause background sync until `queuedAPIEvents` is no longer empty.
            foreach (var pendingEvent in queuedAPIEvents.GetConsumingEnumerable())
            {
                queue.Add(pendingEvent);
                if (queue.Count > 0 && queuedAPIEvents.Count == 0)
                {
                    break;
                }
            }
            InaraConfiguration inaraConfiguration = InaraConfiguration.FromFile();

            if (checkAPIcredentialsOk(inaraConfiguration))
            {
                var responses = SendEventBatch(queue, inaraConfiguration, eddiIsBeta);
                if (responses != null && responses.Count > 0)
                {
                    inaraConfiguration.lastSync = queue.Max(e => e.eventTimeStamp);
                    inaraConfiguration.ToFile();
                }
            }
        }
コード例 #2
0
ファイル: InaraService.cs プロジェクト: BullZye54/EDDI
        public void SendAPIEvents(List <InaraAPIEvent> queue)
        {
            InaraConfiguration inaraConfiguration = InaraConfiguration.FromFile();

            if (checkAPIcredentialsOk(inaraConfiguration))
            {
                var responses = SendEventBatch(queue, inaraConfiguration);
                if (responses != null && responses.Count > 0)
                {
                    inaraConfiguration.lastSync = queue.Max(e => e.eventTimeStamp);
                    inaraConfiguration.ToFile();
                }
            }
        }
コード例 #3
0
        public async void SendQueuedAPIEventsAsync()
        {
            List <InaraAPIEvent> queue = new List <InaraAPIEvent>();

            while (Instance.queuedAPIEvents.TryDequeue(out InaraAPIEvent pendingEvent))
            {
                queue.Add(pendingEvent);
            }
            if (queue.Count > 0)
            {
                await Task.Run(() => Instance.SendEventBatch(ref queue));

                Instance.lastSync = queue.Max(e => e.eventTimeStamp);
                InaraConfiguration inaraConfiguration = InaraConfiguration.FromFile();
                inaraConfiguration.lastSync = Instance.lastSync;
                inaraConfiguration.ToFile();
            }
        }
コード例 #4
0
ファイル: InaraService.cs プロジェクト: BullZye54/EDDI
        private bool validateResponse(InaraResponse inaraResponse, List <InaraAPIEvent> indexedEvents, bool header = false)
        {
            // 200 - Ok
            if (inaraResponse.eventStatus == 200)
            {
                return(true);
            }

            // Anything else - something is wrong.
            Dictionary <string, object> data = new Dictionary <string, object>()
            {
                { "InaraAPIEvent", indexedEvents.Find(e => e.eventCustomID == inaraResponse.eventCustomID) },
                { "InaraResponse", inaraResponse },
                { "Stacktrace", new StackTrace() }
            };

            try
            {
                // 202 - Warning (everything is OK, but there may be multiple results for the input properties, etc.)
                // 204 - 'Soft' error (everything was formally OK, but there are no results for the properties set, etc.)
                if (inaraResponse.eventStatus == 202 || inaraResponse.eventStatus == 204)
                {
                    Logging.Warn("Inara responded with: " + (inaraResponse.eventStatusText ?? "(No response)"), JsonConvert.SerializeObject(data));
                }
                // Other errors
                else if (!string.IsNullOrEmpty(inaraResponse.eventStatusText))
                {
                    if (header)
                    {
                        Logging.Warn("Inara responded with: " + (inaraResponse.eventStatusText ?? "(No response)"), JsonConvert.SerializeObject(data));
                        if (inaraResponse.eventStatusText.Contains("Invalid API key"))
                        {
                            ReEnqueueAPIEvents(indexedEvents);
                            // The Inara API key has been rejected. We'll note and remember that.
                            InaraConfiguration inaraConfiguration = InaraConfiguration.FromFile();
                            inaraConfiguration.isAPIkeyValid = false;
                            inaraConfiguration.ToFile();
                            // Send internal events to the Inara Responder and the UI to handle the invalid API key appropriately
                            invalidAPIkey?.Invoke(inaraConfiguration, new EventArgs());
                        }
                        else if (inaraResponse.eventStatusText.Contains("access to API was temporarily revoked"))
                        {
                            // Note: This can be thrown by over-use of the readonly API key.
                            ReEnqueueAPIEvents(indexedEvents);
                            tooManyRequests = true;
                        }
                    }
                    else
                    {
                        // There may be an issue with a specific API event.
                        // We'll add that API event to a list and omit sending that event again in this instance.
                        Logging.Error("Inara responded with: " + inaraResponse.eventStatusText, data);
                        invalidAPIEvents.Add(indexedEvents.Find(e => e.eventCustomID == inaraResponse.eventCustomID).eventName);
                    }
                }
                else
                {
                    // Inara responded, but no status text description was given.
                    Logging.Error("Inara responded with: ", data);
                }
                return(false);
            }
            catch (Exception e)
            {
                data.Add("Exception", e);
                Logging.Error("Failed to handle Inara server response", data);
                return(false);
            }
        }