Esempio n. 1
0
        public async Task <IActionResult> HandleClick(string token, string callback, string ph)
        {
            var ip          = HttpContext.Connection.RemoteIpAddress?.ToString();
            var userAgent   = Request.Headers[HeaderNames.UserAgent].ToString();
            var clientClick = new ClientClickModel
            {
                Token     = token,
                IP        = ip,
                UserAgent = userAgent,
                ClickedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
            };

            var clickId = await _clientClickService.InsertClick(clientClick);

            // Notes: in task description https://docs.google.com/document/d/1PSgqRJPaJ0EY9lxMHmmPqSj__OGxKC2JnbvIJJK4-v0/edit?usp=sharing
            // "ph" parameter URL encoded too, e.g. http://localhost/abc123?callback=http%3A%2F%2Fwww.google.com%2F%3Fq%3D%7Bph%7D%26%0Aph%3Dakvelon%0A
            // But technically only "callback" parameters should be encoded,
            // If API should handle such type of encoded "ph" parameter anyway, need to implement custom query params handler for this
            if (!String.IsNullOrEmpty(callback))
            {
                // Callback request/response handling can be moved to background thread, e.g. with using RabbitMQ or in simple case - with using Hangfire
                await _callbackResponseService.HandleCallbackResponse(clickId, callback, ph);
            }

            return(Ok());
        }
        public async Task <Guid> InsertClick(ClientClickModel model)
        {
            var click = _mapper.Map <ClientClick>(model);
            var repo  = _uow.GetRepository <ClientClick>();

            repo.Insert(click);
            await _uow.SaveChangesAsync();

            return(click.Id);
        }
Esempio n. 3
0
        public async Task TestClickAndCallbackResponseHandling()
        {
            var clickService    = _serviceProvider.GetService <IClientClickService>();
            var callbackService = _serviceProvider.GetService <ICallbackResponseService>();

            var clickModel = new ClientClickModel
            {
                Token     = "abc123",
                IP        = "test",
                UserAgent = "test",
                ClickedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
            };
            var clickId = await clickService.InsertClick(clickModel);

            var callbackUrl = WebUtility.UrlDecode("http%3A%2F%2Fwww.google.com%2F%3Fq%3D%7Bph%7D");
            var ph          = "akvelon";
            await callbackService.HandleCallbackResponse(clickId, callbackUrl, ph);

            // Here can be any logic, but just return true if no exceptions arise during operations
            Assert.IsTrue(true);
        }