Esempio n. 1
0
        public TValue GetItem <TValue>(Expression <Func <TParameter, TValue> > getItem)
        {
            var(_, _, itemInfo) = MemberVisitor.GetMemberInfo(getItem);
            var itemMetadata = CommandParameterProperty.Create((PropertyInfo)itemInfo);

            var(uri, metadata) = ItemRequestFactory.CreateItemRequest(itemMetadata);

            var item = _args.GetAsync(uri, metadata).GetAwaiter().GetResult();

            if (item.Exists)
            {
                if (itemMetadata.IsCollection)
                {
                    return(item.DeserializeJsonAsync <TValue>().GetAwaiter().GetResult());
                }
                else
                {
                    var values = item.DeserializeJsonAsync <List <TValue> >().GetAwaiter().GetResult();
                    if (((PropertyInfo)itemInfo).PropertyType == typeof(bool))
                    {
                        return
                            (values.Any()
                                ? values.Single()
                                : itemMetadata.DefaultValue is TValue defaultValue
                                    ? defaultValue
                                    : (TValue)(object)true);
                    }
                    else
                    {
                        return
                            (values.Any()
                                ? values.Single()
                                : itemMetadata.DefaultValue is TValue defaultValue
                                    ? defaultValue
                                    : default);
Esempio n. 2
0
        public static Task <IResourceInfo> GetHttpAsync(this IResourceProvider resourceProvider, string path, ImmutableSession metadata = default)
        {
            var uri = new UriString(path);

            return(resourceProvider.GetAsync
                   (
                       uri.IsAbsolute
                    ? uri
                    : new UriString(HttpProvider.DefaultScheme, (string)uri.Path.Original),
                       metadata
                   ));
        }
Esempio n. 3
0
        public static async Task <T> GetItemAsync <T>(this IResourceProvider resources, UriString uri, IImmutableSession metadata = default)
        {
            using (var item = await resources.GetAsync(uri, metadata))
            {
                var itemFormat = item.Metadata.Get(Use <IResourceNamespace> .Namespace, x => x.Format);

                if (item.Exists)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await item.CopyToAsync(memoryStream);

                        if (itemFormat == MimeType.Text)
                        {
                            using (var streamReader = new StreamReader(memoryStream.Rewind()))
                            {
                                return((T)(object)await streamReader.ReadToEndAsync());
                            }
                        }

                        if (itemFormat == MimeType.Binary)
                        {
                            return((T)await ResourceHelper.DeserializeBinaryAsync <object>(memoryStream.Rewind()));
                        }
                    }

                    throw DynamicException.Create
                          (
                              $"ItemFormat",
                              $"Item's '{uri}' format is '{itemFormat}' but only '{MimeType.Binary}' and '{MimeType.Text}' are supported."
                          );
                }
                else
                {
                    throw DynamicException.Create
                          (
                              $"ItemNotFound",
                              $"Could not find '{uri}' that maps to '{item.Metadata.Get(Use<IResourceNamespace>.Namespace, x => x.ActualName) ?? "N/A"}'."
                          );
                }
            }
        }
Esempio n. 4
0
 protected override async Task <IResourceInfo> GetAsyncInternal(UriString uri, IImmutableSession metadata)
 {
     return(await _resourceProvider.GetAsync(Combine(uri), metadata));
 }
Esempio n. 5
0
 public static Task <IResourceInfo> GetAnyAsync(this IResourceProvider resourceProvider, UriString uri, IImmutableSession metadata = default)
 {
     return(resourceProvider.GetAsync(uri.With(x => x.Scheme, ResourceProvider.DefaultScheme), metadata));
 }
 protected override Task <IResourceInfo> GetAsyncInternal(UriString uri, IImmutableSession metadata)
 {
     return(_resourceProvider.GetAsync(UpdatePath(uri), metadata));
 }
Esempio n. 7
0
 public async Task <Stream> GetAsync(string resourceKey)
 {
     return(await provider.GetAsync(resourceKey));
 }
Esempio n. 8
0
 public static async Task <IResourceInfo> GetFileAsync(this IResourceProvider resourceProvider, string path, MimeType format, IImmutableSession metadata = default)
 {
     return(await resourceProvider.GetAsync(CreateUri(path), metadata.ThisOrEmpty().Set(Use <IResourceNamespace> .Namespace, x => x.Format, format)));
 }