Exemplo n.º 1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "text/incr_score")] HttpRequest req, ILogger log, ExecutionContext context,
                                                     [Inject] ITextTransceiverService textTransceiverService, [Inject] IAccessTokenService accessTokenService)
        {
            try
            {
                var token = req.GetAccessToken();
                if (string.IsNullOrEmpty(token))
                {
                    return(new UnauthorizedResult());
                }
                var identity = await accessTokenService.GetIdentityAsync(token);

                if (identity == null)
                {
                    return(new BadRequestResult());
                }
                if (!identity.IsAuthenticated)
                {
                    return(new UnauthorizedResult());
                }
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                var dto         = JsonConvert.DeserializeObject <IncrementSuggestedTextScoreDto>(requestBody);
                var scriptFile  = GetScriptFile(context, LuaScripts.IncrementTextScore);
                textTransceiverService.ScriptFile = scriptFile;
                var isTextScoreIncremented = await textTransceiverService.IncrementSuggestedTextScore(dto);

                return(isTextScoreIncremented ? (ActionResult) new OkObjectResult(null) : new BadRequestResult());
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                return(new BadRequestErrorMessageResult(ex.Message));
            }
        }
Exemplo n.º 2
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "text/get")] HttpRequest req, ILogger log, ExecutionContext context,
                                                     [Inject] ITextTransceiverService textTransceiverService, [Inject] IAccessTokenService accessTokenService)
        {
            try
            {
                var token = req.GetAccessToken();
                if (string.IsNullOrEmpty(token))
                {
                    return(new BadRequestErrorMessageResult("Token is null"));
                }
                var identity = await accessTokenService.GetIdentityAsync(token);

                if (identity == null)
                {
                    return(new BadRequestErrorMessageResult("Identity is null"));
                }
                if (!identity.IsAuthenticated)
                {
                    return(new UnauthorizedResult());
                }
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                var dto         = JsonConvert.DeserializeObject <QuerySuggestionTextDto>(requestBody);
                log.LogInformation($"got: [{dto.UserKey}:{string.Join(" ", dto.Text)}]");
                var scriptFile = GetScriptFile(context, LuaScripts.FindSuggestionText);
                textTransceiverService.ScriptFile = scriptFile;
                var suggestionText = await textTransceiverService.GetSuggestedTextAsync(dto);

                var json = string.Empty;
                if (suggestionText != null && suggestionText.Any())
                {
                    log.LogInformation($"sending count: [{string.Join(" ", suggestionText.Count())}]");
                    json = JsonConvert.SerializeObject(suggestionText);
                }

                return(new OkObjectResult(json));
            }
            catch (SecurityTokenExpiredException ex)
            {
                log.LogInformation(ex.Message);
                return(new UnauthorizedResult());
            }
            catch (Exception ex)
            {
                log.LogInformation($"{ex.Message}\n{ex.StackTrace}");
                return(new BadRequestErrorMessageResult(ex.Message));
            }
        }