Пример #1
0
        public async Task <IServiceResponse> RequestDataAsync()
        {
            clock.Start();
            //  Create an SSE session to receive notification when reply is delivered
            using (var sseClient = new Sse.SseClient(MakeUri("/eap/notifications/sse"), BeapSession))
            {
                await sseClient.Connect();

                // Fetch our account number.
                var catalog = await GetScheduledCatalog();

                // Create a universe component.
                var universePath = await CreateUniverse(catalog);

                // Fetch the newly-created universe component.
                await BeapSession.GetAsync(MakeUri(universePath));

                // Create a field list component.
                var fieldListPath = await CreateFieldList(catalog);

                // Fetch the newly-created field list component.
                await BeapSession.GetAsync(MakeUri(fieldListPath));

                // Create a trigger component.
                var triggerPath = await CreateTrigger(catalog);

                // Fetch the newly-created trigger component.
                await BeapSession.GetAsync(MakeUri(triggerPath));

                // Create a request component.
                var requestPath = await CreateRequest(catalog, universePath, fieldListPath, triggerPath);

                // Fetch the newly-created request component.
                await BeapSession.GetAsync(MakeUri(requestPath));

                // Wait for notification that our output is ready for download. We allow a reasonable amount of time for
                // the request to be processed and avoid waiting forever for the purposes of the sample code -- a timeout
                // may not apply to your actual business workflow. For larger requests or requests made during periods of
                // high load, you may need to increase the timeout.
                var replyTimeout        = TimeSpan.FromMinutes(15);
                var expirationTimestamp = DateTimeOffset.UtcNow.Add(replyTimeout);
                while (DateTimeOffset.UtcNow < expirationTimestamp)
                {
                    var sseEvent = await sseClient.ReadEvent();

                    if (sseEvent.IsHeartbeat)
                    {
                        Console.WriteLine("Received heartbeat event, keep waiting for events");
                        continue;
                    }

                    DeliveryNotification notification;
                    try
                    {
                        notification = JsonSerializer.Deserialize <DeliveryNotification>(sseEvent.Data);
                    }
                    catch (JsonException)
                    {
                        Console.WriteLine("Received other event type, continue waiting");
                        continue;
                    }

                    Console.WriteLine($"Received reply delivery notification event: {sseEvent.Data}");

                    var deliveryDistributionId = notification.Generated.Identifier;
                    var replyUrl          = notification.Generated.Id;
                    var deliveryCatalogId = notification.Generated.Snapshot.Dataset.Catalog.Identifier;

                    if (deliveryCatalogId != catalog || deliveryDistributionId != $"{RequestId}.bbg")
                    {
                        Console.WriteLine("Some other delivery occurred - continue waiting");
                        continue;
                    }
                    // Prepare for storing reply file.
                    Directory.CreateDirectory(Options.ResponseOutputFolderName);
                    var zippedFilePath = Path.Combine(Options.ResponseOutputFolderName, $"{deliveryDistributionId}.gz");
                    // Download reply file from server.
                    await DownloadDistribution(replyUrl, zippedFilePath);

                    Console.WriteLine($"Reply file was downloaded: {zippedFilePath}");
                    var unzippedFilePath = UnzipFile(zippedFilePath);
                    Console.WriteLine($"File was unzipped: {unzippedFilePath}");
                    var securitiesValues = ProcessFile(unzippedFilePath);
                    // Clean files if nedded.
                    if (Options.RemoveFilesAfterResponse == ServiceOptions.ResponseRemoveFilesEnum.RemoveAll ||
                        Options.RemoveFilesAfterResponse == ServiceOptions.ResponseRemoveFilesEnum.RemoveZippedFile)
                    {
                        File.Delete(zippedFilePath);
                    }
                    if (Options.RemoveFilesAfterResponse == ServiceOptions.ResponseRemoveFilesEnum.RemoveAll ||
                        Options.RemoveFilesAfterResponse == ServiceOptions.ResponseRemoveFilesEnum.RemoveUnzippedFile)
                    {
                        File.Delete(unzippedFilePath);
                    }
                    clock.Stop();
                    ServiceResponse.ElapsedTime              = clock.Elapsed;
                    ServiceResponse.ResponseZippedFilePath   = zippedFilePath;
                    ServiceResponse.ResponseUnzippedFilePath = unzippedFilePath;
                    ServiceResponse.SecuritiesValues         = securitiesValues;
                    return(ServiceResponse);
                }
            }
            Console.WriteLine("Reply NOT delivered, try to increase waiter loop timeout");
            return(null);
        }
Пример #2
0
        /// <summary>
        /// Creates a new request along with its associated resources.
        /// </summary>
        async Task DefineRequest()
        {
            //  Create an SSE session to receive notification when reply is delivered
            using (var sseClient = new Sse.SseClient(MakeUri("/eap/notifications/sse"), BeapSession))
            {
                await sseClient.Connect();

                // Fetch our account number.
                var catalog = await GetScheduledCatalog();

                // Create a universe component.
                var universePath = await CreateUniverse(catalog);

                // Fetch the newly-created universe component.
                await BeapSession.GetAsync(MakeUri(universePath));

                // Create a field list component.
                var fieldListPath = await CreateFieldList(catalog);

                // Fetch the newly-created field list component.
                await BeapSession.GetAsync(MakeUri(fieldListPath));

                // Create a trigger component.
                var triggerPath = await CreateTrigger(catalog);

                // Fetch the newly-created trigger component.
                await BeapSession.GetAsync(MakeUri(triggerPath));

                // Create a request component.
                var requestPath = await CreateRequest(catalog, universePath, fieldListPath, triggerPath);

                // Fetch the newly-created request component.
                await BeapSession.GetAsync(MakeUri(requestPath));

                // Wait for notification that our output is ready for download. We allow a reasonable amount of time for
                // the request to be processed and avoid waiting forever for the purposes of the sample code -- a timeout
                // may not apply to your actual business workflow. For larger requests or requests made during periods of
                // high load, you may need to increase the timeout.
                var replyTimeout        = TimeSpan.FromMinutes(15);
                var expirationTimestamp = DateTimeOffset.UtcNow.Add(replyTimeout);
                while (DateTimeOffset.UtcNow < expirationTimestamp)
                {
                    var sseEvent = await sseClient.ReadEvent();

                    if (sseEvent.IsHeartbeat)
                    {
                        Console.WriteLine("Received heartbeat event, keep waiting for events");
                        continue;
                    }

                    DeliveryNotification notification;
                    try
                    {
                        notification = JsonConvert.DeserializeObject <DeliveryNotification>(sseEvent.Data);
                    }
                    catch (JsonException)
                    {
                        Console.WriteLine("Received other event type, continue waiting");
                        continue;
                    }

                    Console.WriteLine($"Received reply delivery notification event: {sseEvent.Data}");

                    var deliveryDistributionId = notification.Generated.Identifier;
                    var replyUrl          = notification.Generated.Id;
                    var deliveryCatalogId = notification.Generated.Snapshot.Dataset.Catalog.Identifier;

                    if (deliveryCatalogId != catalog || deliveryDistributionId != $"{RequestId}.bbg")
                    {
                        Console.WriteLine("Some other delivery occurred - continue waiting");
                        continue;
                    }

                    // Prepare for storing reply file.
                    Directory.CreateDirectory(OutputPath);
                    //var outputFilePath = Path.Combine(OutputPath, $"{deliveryDistributionId}.gz");
                    var outputFilePath = Path.Combine(OutputPath, $"{deliveryDistributionId}");
                    // Download reply file from server.
                    await DownloadDistribution(replyUrl, outputFilePath);

                    Console.WriteLine("Reply was downloaded, exit now");
                    return;
                }
            }
            Console.WriteLine("Reply NOT delivered, try to increase waiter loop timeout");
        }