/// <summary>
        /// Asynchronously requests messages for the specified sport event and returns a request number used when issuing the request
        /// </summary>
        /// <param name="producer">An <see cref="IProducer"/> for which to make the recovery</param>
        /// <param name="eventId">A <see cref="URN"/> specifying the sport event for which to request the messages</param>
        /// <returns> <see cref="Task{HttpStatusCode}"/> representing the async operation</returns>
        public async Task <long> RecoverEventMessagesAsync(IProducer producer, URN eventId)
        {
            if (!producer.IsAvailable || producer.IsDisabled)
            {
                throw new ArgumentException($"Producer {producer} is disabled in the SDK", nameof(producer));
            }
            var requestNumber = _sequenceGenerator.GetNext();
            var _producer     = (Producer)producer;
            var url           = string.Format(EventMessagesRecoveryUrlFormat, _producer.ApiUrl, eventId, requestNumber);

            if (_nodeId != 0)
            {
                url = $"{url}&node_id={_nodeId}";
            }

            // await the result in case an exception is thrown
            var responseMessage = await _dataPoster.PostDataAsync(new Uri(url)).ConfigureAwait(false);

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new CommunicationException($"Recovery of event messages for Event={eventId}, RequestId={requestNumber}, failed with StatusCode={responseMessage.StatusCode}",
                                                 url,
                                                 responseMessage.StatusCode,
                                                 null);
            }

            _producer.EventRecoveries.TryAdd(requestNumber, eventId);
            return(requestNumber);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Books the live odds event associated with the provided {@link URN} identifier
        /// </summary>
        /// <param name="eventId">The event id</param>
        /// <returns><c>true</c> if event was successfully booked, <c>false</c> otherwise</returns>
        public bool BookLiveOddsEvent(URN eventId)
        {
            Contract.Requires(eventId != null);

            try
            {
                _clientLog.Info($"Invoking BookingManager.BookLiveOddsEvent({eventId})");
                var postUrl = string.Format(BookLiveOddsEventUrl, _config.ApiBaseUri, eventId);

                var response = _dataPoster.PostDataAsync(new Uri(postUrl)).Result;

                _clientLog.Info($"BookingManager.bookLiveOddsEvent({eventId}) completed, status: {response.StatusCode}.");
                if (response.IsSuccessStatusCode)
                {
                    _cacheManager.SaveDto(eventId, eventId, CultureInfo.CurrentCulture, DtoType.BookingStatus, null);
                    return(true);
                }

                _cacheManager.RemoveCacheItem(eventId, CacheItemType.SportEvent, "BookingManager");

                _executionLog.Warn($"Event[{eventId}] booking failed. API response code: {response.StatusCode}.");
            }
            catch (CommunicationException ce)
            {
                _executionLog.Warn($"Event[{eventId}] booking failed, CommunicationException: {ce.Message}");
            }
            catch (Exception e)
            {
                _executionLog.Warn($"Event[{eventId}] booking failed.", e);
            }

            return(false);
        }
        /// <summary>
        /// Asynchronously gets a <see cref="CalculationDTO"/> instance
        /// </summary>
        /// <param name="selections">The <see cref="IEnumerable{ISelection}"/> containing selections for which the probability should be fetched</param>
        /// <returns>A <see cref="Task{CalculationDTO}"/> representing the probability calculation</returns>
        public async Task <CalculationDTO> GetDataAsync(IEnumerable <ISelection> selections)
        {
            var content = GetContent(new SelectionsType
            {
                selection = selections.Select(s => new SelectionType
                {
                    id         = s.EventId.ToString(),
                    market_id  = s.MarketId,
                    outcome_id = s.OutcomeId,
                    specifiers = s.Specifiers
                }).ToArray()
            });

            var responseMessage = await _poster.PostDataAsync(new Uri(_uriFormat), content).ConfigureAwait(false);

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new CommunicationException($"Getting probability calculations failed with StatusCode={responseMessage.StatusCode}",
                                                 _uriFormat,
                                                 responseMessage.StatusCode,
                                                 null);
            }

            var stream = await responseMessage.Content.ReadAsStreamAsync();

            return(_mapperFactory.CreateMapper(_deserializer.Deserialize(stream)).Map());
        }
        /// <summary>
        /// Asynchronously requests messages for the specified sport event and returns a request number used when issuing the request
        /// </summary>
        /// <param name="producer">An <see cref="IProducer"/> for which to make the recovery</param>
        /// <param name="eventId">A <see cref="URN"/> specifying the sport event for which to request the messages</param>
        /// <returns> <see cref="Task{HttpStatusCode}"/> representing the async operation</returns>
        public async Task <long> RecoverEventMessagesAsync(IProducer producer, URN eventId)
        {
            Guard.Argument(producer, nameof(producer)).NotNull();
            Guard.Argument(eventId, nameof(eventId)).NotNull();

            if (!producer.IsAvailable || producer.IsDisabled)
            {
                throw new ArgumentException($"Producer {producer} is disabled in the SDK", nameof(producer));
            }
            var requestNumber = _sequenceGenerator.GetNext();
            var myProducer    = (Producer)producer;
            var url           = string.Format(EventMessagesRecoveryUrlFormat, myProducer.ApiUrl, eventId, requestNumber);

            if (_nodeId != 0)
            {
                url = $"{url}&node_id={_nodeId}";
            }

            // await the result in case an exception is thrown
            var responseMessage = await _dataPoster.PostDataAsync(new Uri(url)).ConfigureAwait(false);

            if (!responseMessage.IsSuccessStatusCode)
            {
                var message = $"Recovery of event messages for Event={eventId}, RequestId={requestNumber}, failed with StatusCode={responseMessage.StatusCode}";
                ExecutionLog.Error(message);
                _producerManager.InvokeRecoveryInitiated(new RecoveryInitiatedEventArgs(requestNumber, producer.Id, 0, eventId, message));
                throw new CommunicationException(message, url, responseMessage.StatusCode, null);
            }

            var messageOk = $"Recovery request of event messages for Event={eventId}, RequestId={requestNumber} succeeded.";

            ExecutionLog.Info(messageOk);
            myProducer.EventRecoveries.TryAdd(requestNumber, eventId);
            _producerManager.InvokeRecoveryInitiated(new RecoveryInitiatedEventArgs(requestNumber, producer.Id, 0, eventId, string.Empty));
            return(requestNumber);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fetches and deserializes the data from the provided <see cref="Uri"/>.
        /// </summary>
        /// <param name="content">A <see cref="HttpContent"/> to be posted to the specific <see cref="Uri"/></param>
        /// <param name="authorization">The value of authorization header</param>
        /// <param name="uri">A <see cref="Uri"/> specifying the data location</param>
        /// <returns>A <see cref="Task{TOut}"/> representing the ongoing operation</returns>
        protected async Task <TOut> PostDataAsyncInternal(string authorization, HttpContent content, Uri uri)
        {
            Guard.Argument(uri, nameof(uri)).NotNull();

            var responseMessage = await _poster.PostDataAsync(authorization, uri, content).ConfigureAwait(false);

            if (!responseMessage.IsSuccessStatusCode)
            {
                throw new CommunicationException($"Response StatusCode={responseMessage.StatusCode} does not indicate success.", uri?.ToString(), responseMessage.StatusCode, null);
            }
            var stream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var deserializedObject = _deserializer.Deserialize(stream);

            return(_mapperFactory.CreateMapper(deserializedObject).Map());
        }