Ejemplo n.º 1
0
        public async Task Given_Utm_And_Medium_Then_BuildAsync_Generates_Utm_Link()
        {
            _request.Input    = GOODURL;
            _request.Mediums  = MEDIUMS;
            _request.Campaign = CAMPAIGN;
            _request.TagUtm   = true;
            var results = await _analytics.BuildAsync(
                _request,
                SOURCE,
                HOST,
                () => "X",
                async entity => await Task.Run(() => { }),
                msg => { },
                HttpUtility.ParseQueryString
                );

            Assert.AreEqual(1, results.Count);
            Assert.IsTrue(results[0].LongUrl.Contains(UTM_CAMPAIGN_FRAGMENT));
            Assert.IsTrue(results[0].LongUrl.Contains(UTM_SOURCE_FRAGMENT));
            Assert.IsTrue(results[0].LongUrl.Contains(UTM_MEDIUM_FRAGMENT));
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> ShortenUrl(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
            [Table(Utility.TABLE, "1", Utility.KEY, Take = 1)] NextId keyTable,
            [Table(Utility.TABLE)] CloudTable tableOut,
            ILogger log)
        {
            log.LogInformation($"C# triggered function called with req: {req}");

            if (req == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound));
            }

            var check = SecurityCheck(req);

            if (check != null)
            {
                return(check);
            }

            ShortRequest input = await req.Content.ReadAsAsync <ShortRequest>();

            if (input == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound));
            }

            try
            {
                var result    = new List <ShortResponse>();
                var analytics = new Analytics();

                // determine whether or not to process analytics tags
                bool tagMediums = analytics.Validate(input);

                var campaign = string.IsNullOrWhiteSpace(input.Campaign) ? DefaultCampaign : input.Campaign;
                var url      = input.Input.Trim();
                var utm      = analytics.TagUtm(input);
                var wt       = analytics.TagWt(input);

                log.LogInformation($"URL: {url} Tag UTM? {utm} Tag WebTrends? {wt}");
                log.LogInformation($"Current key: {keyTable.Id}");

                // get host for building short URL
                var host = req.RequestUri.GetLeftPart(UriPartial.Authority);

                // strategy for getting a new code
                string getCode() => Utility.Encode(keyTable.Id++);

                // strategy for logging
                void logFn(string msg) => log.LogInformation(msg);

                // strategy to save the key
                async Task saveKeyAsync()
                {
                    var operation = TableOperation.Replace(keyTable);
                    await tableOut.ExecuteAsync(operation);
                }

                // strategy to insert the new short url entry
                async Task saveEntryAsync(TableEntity entry)
                {
                    var operation       = TableOperation.Insert(entry);
                    var operationResult = await tableOut.ExecuteAsync(operation);
                }

                // strategy to create a new URL and track the dependencies
                async Task saveWithTelemetryAsync(TableEntity entry)
                {
                    await TrackDependencyAsync(
                        "AzureTableStorageInsert",
                        "Insert",
                        async() => await saveEntryAsync(entry),
                        () => true);
                    await TrackDependencyAsync(
                        "AzureTableStorageUpdate",
                        "Update",
                        async() => await saveKeyAsync(),
                        () => true);
                }

                if (tagMediums)
                {
                    // this will result in multiple entries depending on the number of
                    // mediums passed in
                    result.AddRange(await analytics.BuildAsync(
                                        input,
                                        Source,
                                        host,
                                        getCode,
                                        saveWithTelemetryAsync,
                                        logFn,
                                        HttpUtility.ParseQueryString));
                }
                else
                {
                    // no tagging, just pass-through the URL
                    result.Add(await Utility.SaveUrlAsync(
                                   url,
                                   null,
                                   host,
                                   getCode,
                                   logFn,
                                   saveWithTelemetryAsync));
                }

                log.LogInformation($"Done.");
                return(req.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                log.LogError(ex, "An unexpected error was encountered.");
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }