Exemplo n.º 1
0
 public SyncanoConversation(ApiCommandRequest request, Func <JToken, T> responseMapperFunc)
 {
     _responseMapperFunc = responseMapperFunc;
     Request             = request;
     _id = request.MessageId.ToString();
     _responseSubject = new Subject <T>();
     Created          = DateTime.UtcNow;
 }
Exemplo n.º 2
0
        /// <inheritdoc />
        public IReadOnlyList <object> OnCallCommand(ApiUserInfo user, ApiCommandRequest request)
        {
            var result = new List <object>();

            // let's inform client in case if there is no any commands
            if (!_apiPlugins.Any())
            {
                throw new ApiCommandNotFoundException($"No methods found for the command '{request.CommandName}'");
            }

            var found = false;

            foreach (var apiPlugin in _apiPlugins)
            {
                // let's try to find target methods with same command name
                var apiCommands = apiPlugin.Methods

                                  // looking for command by name
                                  .Where(x => x.ApiInfo.CommandName.Equals(request.CommandName, StringComparison.InvariantCultureIgnoreCase))

                                  // filter by required and user permissions
                                  .Where(x => IsUserHasAccess(user, x.ApiInfo.RequiredPermissions));

                // in case if command with requested name not found
                if (!apiCommands.Any())
                {
                    continue;
                }
                found = true;

                // execute api methods and build response
                foreach (var apiMethod in apiCommands)
                {
                    var methodResult = ExecuteMethod(apiPlugin, apiMethod, request, user);
                    if (methodResult != default)
                    {
                        result.Add(methodResult);
                    }
                }
            }

            if (!found) // in case if command with requested name not found
            {
                var userName = user.IsAnonymous ? "Anonymous" : user.Name;
                throw new ApiCommandNotFoundException($"Command '{request.CommandName}' not found for user '{userName}'");
            }

            return(result);
        }
Exemplo n.º 3
0
        protected override async Task OnHandleRequestAsync(HttpRequestMessage message, LoginToken token)
        {
            if (!_configuration.TryGet(SecurityTokenConstants.ApiConfiguration, out ApiConfiguration configuration))
            {
                throw new InvalidOperationException("There is no api configuration configured. Please ensure that it has been added to the security configuration.");
            }

            var parameters = await GetRequestParameters(token);

            var request = new ApiCommandRequest(configuration.TokenEndpoint, parameters);

            var response = await _apiCommand.ExecuteAsync(request);

            var newToken = await response.TryGetContent <LoginToken>();

            _configuration.Update(SecurityTokenConstants.LoginToken, newToken);

            message.Headers.Authorization = new AuthenticationHeaderValue(SecurityTokenConstants.JwtAuthHeader, newToken.AccessToken);
        }
Exemplo n.º 4
0
 public virtual void OnApiCommandError(ApiCommandRequest request, ApiCommandResponse response, Exception exception)
 {
     throw exception;
 }
Exemplo n.º 5
0
 public virtual void OnApiCommandComplete(ApiCommandRequest request, ApiCommandResponse response)
 {
 }
Exemplo n.º 6
0
 public virtual void OnApiCommandInitialization(ApiCommandRequest request, ApiCommandResponse response)
 {
 }
Exemplo n.º 7
0
        /// <summary>
        /// Execute method.
        /// </summary>
        /// <param name="commandsPluginInfo">Api plugin information.</param>
        /// <param name="commandMethodInfo">Api method information.</param>
        /// <param name="request">Current request.</param>
        /// <param name="user">Current user info.</param>
        /// <returns></returns>
        private object ExecuteMethod(CommandPluginInfo commandsPluginInfo, CommandMethodInfo commandMethodInfo, ApiCommandRequest request, ApiUserInfo user)
        {
            try
            {
                // build required parameters (in case of unknown type will set as default)
                var parameters = commandMethodInfo
                                 .Method
                                 .GetParameters()
                                 .Select(x =>
                {
                    if (x.ParameterType == typeof(ApiCommandRequest))
                    {
                        return(request);
                    }
                    if (x.ParameterType == typeof(ApiUserInfo))
                    {
                        return(user);
                    }
                    if (x.ParameterType == typeof(ApiCommandAttribute))
                    {
                        return(commandMethodInfo.ApiInfo);
                    }

                    return(default(object));
                })
                                 .ToArray();

                // invoke target method
                var result = commandMethodInfo.Method.Invoke(commandsPluginInfo.Instance, parameters);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Api command execution error");
                return(default);