Ejemplo n.º 1
0
        protected override Task ExecuteAsync(SendMessageOptions options, IEstablishedChannel channel, CancellationToken cancellationToken)
        {
            var content = _documentSerializer.Deserialize(options.JoinedContent, options.Type);

            return(channel.SendMessageAsync(
                       new Message(options.Id)
            {
                From = options.From,
                To = options.To,
                Content = content
            },
                       cancellationToken));
        }
        protected override Task ExecuteAsync(CreateAccountOptions options, IEstablishedChannel channel, CancellationToken cancellationToken)
        {
            var identity = Identity.Parse(options.Identity);

            return(channel.SetResourceAsync(
                       new LimeUri($"lime://{identity}{UriTemplates.ACCOUNT}"),
                       new Account()
            {
                Identity = identity,
                Password = options.Password.ToBase64(),
                FullName = options.Name
            },
                       cancellationToken));
        }
Ejemplo n.º 3
0
        protected override Task ExecuteAsync(SendCommandOptions options, IEstablishedChannel channel, CancellationToken cancellationToken)
        {
            var resource = _documentSerializer.Deserialize(options.JoinedResource, options.Type);

            return(channel.SendCommandAsync(
                       new Command(options.Id ?? EnvelopeId.NewId())
            {
                From = options.From,
                To = options.To,
                Method = options.Method,
                Uri = new LimeUri(options.Uri),
                Resource = resource
            },
                       cancellationToken));
        }
Ejemplo n.º 4
0
        protected override async Task ExecuteAsync(ProcessCommandOptions options, IEstablishedChannel channel, CancellationToken cancellationToken)
        {
            if (options.ExpectedResource != null && options.Method != CommandMethod.Get)
            {
                throw new Exception("The expected resource option can only be used with 'get' method");
            }

            var resource = _documentSerializer.Deserialize(options.JoinedResource, options.Type);

            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(options.Timeout)))
                using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken))
                {
                    var responseCommand = await channel.ProcessCommandAsync(
                        new Command(options.Id ?? EnvelopeId.NewId())
                    {
                        From     = options.From,
                        To       = options.To,
                        Method   = options.Method,
                        Uri      = new LimeUri(options.Uri),
                        Resource = resource
                    },
                        cancellationToken);

                    if (responseCommand.Status != options.ExpectedStatus)
                    {
                        throw new Exception($"Unexpected response status '{responseCommand.Status}' ({responseCommand.Reason?.ToString()})");
                    }

                    if (options.ExpectedResource != null)
                    {
                        var expectedResourceRegex = new Regex(options.ExpectedResource);
                        var responseResource      = _documentSerializer.Serialize(responseCommand.Resource);

                        if (!expectedResourceRegex.IsMatch(responseResource))
                        {
                            throw new Exception($"Unexpected response resource: {responseResource}");
                        }
                    }
                }
        }
Ejemplo n.º 5
0
 protected abstract Task ExecuteAsync(TOptions options, IEstablishedChannel channel, CancellationToken cancellationToken);
Ejemplo n.º 6
0
 Task IAction.ExecuteAsync(object options, IEstablishedChannel channel, CancellationToken cancellationToken)
 {
     return(ExecuteAsync((TOptions)options, channel, cancellationToken));
 }