Esempio n. 1
0
        // If you need to do some testing on Inara's API, please set the `isDeveloped` boolean header property to true.
        public List <InaraResponse> SendEventBatch(List <InaraAPIEvent> events, InaraConfiguration inaraConfiguration)
        {
            // We always want to return a list from this method (even if it's an empty list) rather than a null value.
            List <InaraResponse> inaraResponses = new List <InaraResponse>();

            try
            {
                if (inaraConfiguration is null)
                {
                    inaraConfiguration = InaraConfiguration.FromFile();
                }
                List <InaraAPIEvent> indexedEvents = IndexAndFilterAPIEvents(events, inaraConfiguration);
                if (indexedEvents.Count > 0)
                {
                    var           client       = new RestClient("https://inara.cz/inapi/v1/");
                    var           request      = new RestRequest(Method.POST);
                    InaraSendJson inaraRequest = new InaraSendJson()
                    {
                        header = new Dictionary <string, object>()
                        {
                            // Per private conversation with Artie and per Inara API docs, the `isDeveloped` property
                            // should (counterintuitively) be set to true when the an application is in development.
                            // Quote: `it's "true" because the app "is (being) developed"`
                            // Quote: `isDeveloped is meant as "the app is currently being developed and may be broken`
                            { "appName", "EDDI" },
                            { "appVersion", Constants.EDDI_VERSION.ToString() },
                            { "isDeveloped", eddiIsBeta },
                            { "commanderName", !string.IsNullOrEmpty(inaraConfiguration?.commanderName) ? inaraConfiguration.commanderName : (eddiIsBeta ? "TestCmdr" : null) },
                            { "commanderFrontierID", !string.IsNullOrEmpty(inaraConfiguration?.commanderFrontierID) ? inaraConfiguration.commanderFrontierID : null },
                            { "APIkey", !string.IsNullOrEmpty(inaraConfiguration?.apiKey) ? inaraConfiguration.apiKey : readonlyAPIkey }
                        },
                        events = indexedEvents
                    };
                    request.RequestFormat = DataFormat.Json;
                    request.AddBody(inaraRequest); // uses JsonSerializer

                    Logging.Debug("Sending to Inara: " + client.BuildUri(request).AbsoluteUri);
                    var clientResponse = client.Execute <InaraResponses>(request);
                    if (clientResponse.IsSuccessful)
                    {
                        InaraResponses response = clientResponse.Data;
                        if (validateResponse(response.header, indexedEvents, true))
                        {
                            foreach (InaraResponse inaraResponse in response.events)
                            {
                                if (validateResponse(inaraResponse, indexedEvents))
                                {
                                    inaraResponses.Add(inaraResponse);
                                }
                            }
                        }
                    }
                    else
                    {
                        // Inara may return null as it undergoes a nightly maintenance cycle where the servers go offline temporarily.
                        Logging.Warn("Unable to connect to the Inara server.", clientResponse.ErrorMessage);
                        ReEnqueueAPIEvents(events);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error("Sending data to the Inara server failed.", ex);
            }
            return(inaraResponses);
        }
Esempio n. 2
0
        // If you need to do some testing on Inara's API, please set the `isDeveloped` boolean header property to true.
        public List <InaraResponse> SendEventBatch(ref List <InaraAPIEvent> events, bool sendEvenForBetaGame = false)
        {
            if (!sendEvenForBetaGame && gameInBeta)
            {
                return(null);
            }

            List <InaraResponse> inaraResponses = new List <InaraResponse>();

            if (string.IsNullOrEmpty(apiKey))
            {
                return(inaraResponses);
            }

            // Flag each event with a unique ID we can use when processing responses
            List <InaraAPIEvent> indexedEvents = new List <InaraAPIEvent>();

            for (int i = 0; i < events.Count; i++)
            {
                InaraAPIEvent indexedEvent = events[i];
                indexedEvent.eventCustomID = i;
                indexedEvents.Add(indexedEvent);
            }

            var           client       = new RestClient("https://inara.cz/inapi/v1/");
            var           request      = new RestRequest(Method.POST);
            InaraSendJson inaraRequest = new InaraSendJson()
            {
                header = new Dictionary <string, object>()
                {
                    // Per private conversation with Artie and per Inara API docs, the `isDeveloped` property
                    // should (counterintuitively) be set to true when the an application is in development.
                    // Quote: `it's "true" because the app "is (being) developed"`
                    // Quote: `isDeveloped is meant as "the app is currently being developed and may be broken`
                    { "appName", "EDDI" },
                    { "appVersion", Constants.EDDI_VERSION.ToString() },
                    { "isDeveloped", eddiInBeta },
                    { "commanderName", commanderName ?? (eddiInBeta ? "TestCmdr" : null) },
                    { "commanderFrontierID", commanderFrontierID },
                    { "APIkey", apiKey }
                },
                events = indexedEvents
            };

            request.RequestFormat = DataFormat.Json;
            request.AddBody(inaraRequest); // uses JsonSerializer

            Logging.Debug("Sending to Inara: " + client.BuildUri(request).AbsoluteUri);
            var clientResponse = client.Execute <InaraResponses>(request);

            if (clientResponse.IsSuccessful)
            {
                InaraResponses response = clientResponse.Data;
                if (validateResponse(response.header, ref indexedEvents))
                {
                    foreach (InaraResponse inaraResponse in response.events)
                    {
                        if (validateResponse(inaraResponse, ref indexedEvents))
                        {
                            inaraResponses.Add(inaraResponse);
                        }
                    }
                }
                return(inaraResponses);
            }
            else
            {
                Logging.Warn("Unable to connect to the Inara server.", clientResponse.ErrorMessage);
                foreach (InaraAPIEvent inaraAPIEvent in events)
                {
                    // Re-enqueue and send at a later time.
                    EnqueueAPIEvent(inaraAPIEvent);
                }
                return(null);
            }
        }