示例#1
0
        public static async Task <T> AzureStorageDeleteAsync <T>(this Rest restme, string storageRelativePath)
        {
            MustBeStorageMode(restme);
            var container = await restme.GetAzureBlobContainerAsync(storageRelativePath);

            var blobItemPath = restme.IdentifyBlobItemPath(storageRelativePath);

            if (blobItemPath.IsNullOrEmpty())
            {
                throw new PranamWebException("Invalid blob item name.");
            }
            var blockBlob = container.GetBlockBlobReference(blobItemPath);

            try
            {
                await blockBlob.DeleteIfExistsAsync();

                if (typeof(T) == typeof(bool))
                {
                    return((T)Convert.ChangeType(true, typeof(T)));
                }
            }
            catch (Exception ex)
            {
                restme.LogDebug("Unable to delete requested data:\n" + ex.Message, ex);
            }

            return(default(T));
        }
示例#2
0
        public static async Task <T> AzureStoragePostAsync <T>(this Rest restme, string storageRelativePath, object dataObject)
        {
            MustBeStorageMode(restme);
            if (dataObject == null)
            {
                throw new PranamWebException(
                          "Uploading null blob is not supported, use delete method if you intended to delete.");
            }

            var container = await restme.GetAzureBlobContainerAsync(storageRelativePath);

            var blobItemPath = restme.IdentifyBlobItemPath(storageRelativePath);

            if (blobItemPath.IsNullOrEmpty())
            {
                throw new PranamWebException("Invalid blob item name.");
            }
            var    blockBlob = container.GetBlockBlobReference(blobItemPath);
            Stream stream    = null;

            using (stream = new MemoryStream())
            {
                try
                {
                    var extension = FileUtils.GetFileExtensionName(storageRelativePath);
                    if (extension.IsNotNullOrEmpty())
                    {
                        blockBlob.Properties.ContentType = FileUtils.GetMimeType(extension);
                    }
                    if (typeof(Stream).IsAssignableFrom(typeof(T)))
                    {
                        stream          = dataObject as Stream;
                        stream.Position = 0;
                        await blockBlob.UploadFromStreamAsync(stream);
                    }
                    else
                    {
                        var jsonValue =
                            dataObject.JsonSerialize(restme.Configuration.UseRestConvertForCollectionSerialization,
                                                     restme.Configuration.SerializerSettings);
                        await
                        blockBlob.UploadTextAsync(jsonValue, restme.Configuration.DefaultEncoding,
                                                  restme.DefaultAzureBlobAccessCondition, restme.DefaultAzureBlobRequestOptions,
                                                  restme.DefaultAzureBlobOperationContext);
                    }

                    return((T)dataObject);
                }
                catch (Exception ex)
                {
                    restme.LogDebug("Unable to upload requested data:\n" + ex.Message, ex);
                    return(default(T));
                }
            }
        }
示例#3
0
        public static async Task <T> RedisPostAsync <T>(this Rest restme, string redisKey, object dataObject)
        {
            MustBeRedisMode(restme);
            var objectInString =
                dataObject.JsonSerialize(restme.Configuration.UseRestConvertForCollectionSerialization);

            restme.LogDebug($"Redis convert - object to string:  {objectInString}");
            if (await restme.redisDatabase.StringSetAsync(redisKey, objectInString))
            {
                return((T)dataObject);
            }
            return(default(T));
        }
示例#4
0
        public static async Task <T> RedisGetAsync <T>(this Rest restme, string redisKey)
        {
            MustBeRedisMode(restme);
            string stringValue = await restme.redisDatabase.StringGetAsync(redisKey);

            restme.LogDebug($" RestmeRedis - GET:  {redisKey}\n RESULT: \n {stringValue}");
            if (!stringValue.IsNotNullOrEmpty())
            {
                return(default(T));
            }
            if (typeof(T).IsPrimitiveType())
            {
                return((T)Convert.ChangeType(stringValue, typeof(T)));
            }

            return(stringValue.JsonDeserialize <T>(restme.Configuration.UseRestConvertForCollectionSerialization));
        }
示例#5
0
        public static async Task <T> AzureStorageGetAsync <T>(this Rest restme, string storageRelativePath)
        {
            MustBeStorageMode(restme);
            var container = await restme.GetAzureBlobContainerAsync(storageRelativePath);

            var blobItemPath = restme.IdentifyBlobItemPath(storageRelativePath);

            if (blobItemPath.IsNullOrEmpty())
            {
                throw new PranamWebException("Invalid blob item name.");
            }
            var blockBlob = container.GetBlockBlobReference(blobItemPath);

            using (var stream = new MemoryStream())
            {
                try
                {
                    if (!await blockBlob.ExistsAsync())
                    {
                        return(default(T));
                    }

                    if (typeof(Stream).IsAssignableFrom(typeof(T)))
                    {
                        await blockBlob.DownloadToStreamAsync(stream);

                        var bytes = FileUtils.ReadStreamToEnd(stream);
                        T   result;
                        if (typeof(T).GetTypeInfo().IsAbstract)
                        {
                            result = (T)Activator.CreateInstance(typeof(MemoryStream), bytes);
                        }
                        else
                        {
                            result = (T)Activator.CreateInstance(typeof(T), bytes);
                        }

                        return(result);
                    }


                    var jsonStringValue = await blockBlob.DownloadTextAsync();

                    if (!jsonStringValue.IsNotNullOrEmpty())
                    {
                        return(default(T));
                    }

                    if (typeof(T) == typeof(string))
                    {
                        return((T)Convert.ChangeType(jsonStringValue, typeof(T)));
                    }

                    return(jsonStringValue.JsonDeserialize <T>());
                }
                catch (Exception ex)
                {
                    restme.LogDebug(
                        $"Unable to fetch requested blob: {storageRelativePath}\n {ex.Message} \n {ex.StackTrace}", ex);
                    return(default(T));
                }
            }
        }