Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets arguments that carry base event information.
        /// </summary>
        /// <param name="eventInfo">Base event information.</param>
        public static IEnumerable <string> GetBaseEventInfoArgs(PiwikEventInfoBase eventInfo)
        {
            if (null == eventInfo)
            {
                return(Enumerable.Empty <string>());
            }

            var args = new List <string>()
            {
                $"url={Uri.EscapeDataString(eventInfo.Url)}"
            };

            args.SafeAddStringArgument("urlref", eventInfo.ReferrerUrl);

            args.AddRange(new[] { $"h={eventInfo.LocalTimestamp.Hour}", $"m={eventInfo.LocalTimestamp.Minute}", $"s={eventInfo.LocalTimestamp.Second}" });

            return(args);
        }