Esempio n. 1
0
        public void Encode_Decode_Results_Consistent()
        {
            int before = 8645;
            int after  = IdEncoder.Decode(IdEncoder.Encode(before));

            Assert.AreEqual(before, after);


            before = int.MaxValue;
            after  = IdEncoder.Decode(IdEncoder.Encode(before));

            Assert.AreEqual(before, after);
        }
Esempio n. 2
0
        public string ResolveUrlIdentifier(string urlShort)
        {
            var id        = IdEncoder.Decode(urlShort);
            var urlEntity = (from u in repository.Urls
                             where u.Id == id
                             select u
                             ).FirstOrDefault();

            if (urlEntity == null)
            {
                throw new Exception("Id not found");
            }
            return(urlEntity.Value);
        }
Esempio n. 3
0
 public void UpdateUrlStatistics(string encodedId, System.Collections.Specialized.NameValueCollection nameValueCollection)
 {
     repository.IncreaseUrlHitCount(IdEncoder.Decode(encodedId));
 }
Esempio n. 4
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "push")]
            HttpRequestMessage req,
            TraceWriter log,
            CancellationToken ct)
        {
            try
            {
                var verifyCode = req
                                 .GetQueryNameValuePairs()
                                 .FirstOrDefault(q => q.Key.Equals("verify", StringComparison.Ordinal))
                                 .Value;

                if (verifyCode.HasValue())
                {
                    return(verifyCode.Equals(Constants.FitbitVerificationCode, StringComparison.Ordinal)
                                                ? new HttpResponseMessage(HttpStatusCode.NoContent)
                                                : new HttpResponseMessage(HttpStatusCode.NotFound));
                }

                var notificationsContent = await req.Content.ReadAsStringAsync();

                var notifications           = JsonConvert.DeserializeObject <Notification[]>(notificationsContent);
                var notificationsForwarding = notifications
                                              .GroupBy(notif => notif.SubscriptionId?.Substring(1), StringComparer.Ordinal)
                                              .Select(notifs =>
                {
                    string accessToken, locationId;
                    var parts = notifs.Key?.Split(new[] { '~' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                    if (parts.Length == 2)
                    {
                        try
                        {
                            accessToken = IdEncoder.Decode(parts[0]).ToString("D");
                            locationId  = IdEncoder.Decode(parts[1]).ToString("D");
                        }
                        catch (Exception e)
                        {
                            log.Error($"Invalid subscription id: {notifs.Key}", e);
                            accessToken = locationId = null;
                        }
                    }
                    else
                    {
                        log.Error("Invalid number of parts, don't forward the notifications.");
                        accessToken = locationId = null;
                    }


                    return(new { accessToken, locationId, notifs });
                })
                                              .Where(x => x.accessToken.HasValue() && x.locationId.HasValue())
                                              .Select(x => Send(ct, log, x.accessToken, x.locationId, x.notifs));

                // TODO: Run async
                await Task.WhenAll(notificationsForwarding);

                return(req.CreateResponse(HttpStatusCode.NoContent));
            }
            catch (Exception e)
            {
                log.Error("Failed", e);

                throw;
            }
        }