Пример #1
0
        /// <summary>
        /// Sends request to an API endpoint.
        /// </summary>
        /// <param name="eventInfo">Base event information.</param>
        /// <param name="extraArgs">Additional arguments.</param>
        private async Task SendRequestAsync(PiwikEventInfoBase eventInfo, IEnumerable <string> extraArgs)
        {
            if (null == eventInfo)
            {
                throw new ArgumentNullException(nameof(eventInfo));
            }
            if (null == extraArgs)
            {
                throw new ArgumentNullException(nameof(extraArgs));
            }

            var args = new List <string>
            {
                "rec=1",
                "apiv=1",
                $"idsite={options.SiteId}",
                $"rand={rnd.Next()}"
            };

            args.AddRange(PiwikDataConverter.GetBaseEventInfoArgs(eventInfo));

            args.AddRange(PiwikDataConverter.GetSessionInfoArgs(eventInfo.Session));

            args.AddRange(PiwikDataConverter.GetEnvironmentInfoArgs(eventInfo.EnvironmentInfo));

            args.AddRange(extraArgs);

            var requestUri = new UriBuilder(options.Endpoint)
            {
                Query = string.Join("&", args)
            };

            try
            {
                using (var http = new HttpClient(new ApplyUserAgentHttpMessageHandler(options.TestOnlyHttpMessageHandler ?? new HttpClientHandler(), eventInfo.EnvironmentInfo)))
                {
                    (await http.GetAsync(requestUri.Uri).ConfigureAwait(false)).EnsureSuccessStatusCode();
                }
            }
            catch (Exception ex)
            {
                if (!options.SwallowExceptions)
                {
                    throw new PiwikApiException(ex);
                }
            }
        }