/// <summary> /// Composes a command envelope with a get method for the specified resource. /// </summary> /// <typeparam name="TResource">The type of the resource.</typeparam> /// <param name="channel">The channel.</param> /// <param name="uri">The resource uri.</param> /// <param name="from">The originator to be used in the command.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">channel</exception> /// <exception cref="LimeException">Returns an exception with the failure reason</exception> public static async Task <TResource> GetResourceAsync <TResource>(this ICommandChannel channel, LimeUri uri, Node from, CancellationToken cancellationToken) where TResource : Document { if (channel == null) { throw new ArgumentNullException(nameof(channel)); } if (uri == null) { throw new ArgumentNullException(nameof(uri)); } var requestCommand = new Command { From = from, Method = CommandMethod.Get, Uri = uri }; var responseCommand = await channel.ProcessCommandAsync(requestCommand, cancellationToken).ConfigureAwait(false); if (responseCommand.Status == CommandStatus.Success) { return((TResource)responseCommand.Resource); } else if (responseCommand.Reason != null) { throw new LimeException(responseCommand.Reason.Code, responseCommand.Reason.Description); } else { throw new InvalidOperationException("An invalid command response was received"); } }
/// <summary> /// Merges the resource value. /// </summary> /// <typeparam name="TResource">The type of the resource.</typeparam> /// <param name="channel">The channel.</param> /// <param name="uri">The resource uri.</param> /// <param name="resource">The resource to be merge.</param> /// <param name="from">The originator to be used in the command.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">channel</exception> /// <exception cref="LimeException"></exception> public static async Task MergeResourceAsync <TResource>(this ICommandChannel channel, LimeUri uri, TResource resource, Node from, CancellationToken cancellationToken) where TResource : Document { if (channel == null) { throw new ArgumentNullException(nameof(channel)); } if (uri == null) { throw new ArgumentNullException(nameof(uri)); } if (resource == null) { throw new ArgumentNullException(nameof(resource)); } var requestCommand = new Command { From = from, Method = CommandMethod.Merge, Uri = uri, Resource = resource }; var responseCommand = await channel.ProcessCommandAsync(requestCommand, cancellationToken).ConfigureAwait(false); if (responseCommand.Status != CommandStatus.Success) { if (responseCommand.Reason != null) { throw new LimeException(responseCommand.Reason.Code, responseCommand.Reason.Description); } else { #if DEBUG if (requestCommand == responseCommand) { throw new InvalidOperationException("The request and the response are the same instance"); } #endif throw new InvalidOperationException("An invalid command response was received"); } } }