DEV Community

Cover image for Simplified Azure Storage Client in .NET Core
Pranam K
Pranam K

Posted on

Simplified Azure Storage Client in .NET Core

I have created "Simplified Azure Storage Client in .NET Core". I was originally thinking to just create a RESTful client at first, and then decided to make it more powerful by enabling it to communicate with Azure Storage and Redis Cache.

All tools are wrapped into single class "Restme()" to keep everything simple. The class will automatically identify whether it's used as HTTP Client, Azure Storage Client or Redis Cache Client.

Since they are all about requesting and saving data, it is going to play a major role when it comes to saving time and space.

Code:


//HTTP Restful client (sudo code)  
// Get data  
rest.Get(requestUrl);  
// Save data  
rest.Add(paramKey, paramValue);  
rest.Post(requestUrl, data);  
// Delete data  
rest.Delete(requestUrl);
  
//Azure Storage or Redis ache sudo-code  
// Get data  
rest.Get("/container/filePath");  
rest.Get("/container/jsonData");  
rest.Get("/container/filePath");  
  
// Save data  
rest.Post("/container/filePath", myObject);  
// Delete data  
rest.Delete("/container/filePath");   

Example:

1. Use it as a basic RESTful HTTP Client:

//direct string JSON return    
var rest = new Restme(new Uri("http://freegeoip.net"));  
var result1 = rest.Get("/json/github.com");  
  
//automatic Generic cast    
var result2 = rest.Get < MyObject > ("/json/github.com");  
var resultAsync2 = await rest.GetAsync << MyObjecT > ("/json/github.com");  
  
//add parameters (Parameters get automatically converted into query string or post form fields)    
rest.add("q", "github.com");  
var result3 = rest.Get < MyObject > ("/json");  
var resultAsync3 = await rest.GetAsync < MyObjecT > ("/json");  
  
//supports POST, DELETE, PUT etc.    
var rest2 = new Restme(new Uri("http://example.com"));  
rest2.Add("Username", "abc@def.com");  
rest2.Add("Birthday", DateTime.UtcNow);  
rest2.Post < MyObject > ("/someurl");  
  
rest3.PostAsync < MyObject > ("/asyncexample");  
  
  
//supports direct object submission    
var myObject = new MyObject()  
{  
    Username = "abc@def.com",  
        Birthday = DateTime.UtcNow  
};  
var rest3 = new Restme(new Uri("http://example.com"));  
rest3.Add(myObject);  
rest3.Post < ExpectedResultObject > ("/directObjectPost");  

2. Use it as an Azure Storage Client:

//get blob stream directly      
var blobStorageConnectionString = "{Your Storage Account Connection String}";  
  
  
var rest = new Restme(blobStorageConnetionString);  
  
  
rest.CreateAzureBlobContainerIfNotExists = true; //do this only if you want to auto create the container        
  
  
//NOTE 1: first segment of the path should always be your container name      
  
  
//NOTE 3: Type T: if it is a type of Stream it will be stored as original Stream as Azure Blob, otherwise it is always saved into JSON format as Azure Blob      
  
  
//NOTE 2: use a type of Stream if you want the original value retrieved from the blob      
  
  
rest.Get < Stream > ("/myContainer/myfilePath");  
  
rest.GetAsync < Stream > ("/myContainer/myfilePath");  
  
  
//NOTE: only blob items saved in JSON format is suppported      
  
  
rest.Get < ObjectType > ("/myContainer/myfileObjectInJSONFileFormat");  
  
rest.GetAsync < ObjectType > ("/myContainer/myfileObjectInJSONFileFormat");  

3. Use it as a Redis Cache Client:

var redisConnectionString = "{Your Redis Cache Connection String}";    
var rest = new Restme(redisConnectionString);    
    
//get cache data (support Generic cast)    
var cacheResult = rest.Get("home:testKey");    
var cacheResult2 = rest.Get("home:testKey2");    
var cacheResult3 = rest.Get("home:testKey3");    
    
//set cache data    
rest.Post("home:testKey","value");    
rest.Post("home:testKey2",true);    
    
var myObject = new ObjectType();  //will be serialized into JSON format and stored as string on redis server    
rest.Post("home:testKey3", myObject);  

Advantages:

  1. The main goal is to increase productivity and keep code simplicity.
  2. Currently it includes: RESTful HTTP Client, Azure Storage Client, Redis Cache Client.
  3. Simple methods and flexible calls
  4. Uses Newtonsoft JSON and allows custom serilization
  5. Async support
  6. HTTP Client:
    • Parameters get automatically converted into query string or post form fields
    • Supports GET, POST, PUT, DELETE
    • Bearer Authentication and custom headers
  7. Azure Storage Client:
    • Simplified call stack
  8. Redis Cache Client
    • Simplified call stack (Currently only support Azure Redis Cache, you can modify the source code to support other Redis Servers as well)

Check out the full code here:

GitHub logo PranamBhat / AzureDotNet

Simplified Azure Storage Client in .NET Core

REST Http Client

Simplified Azure Storage Client in .NET Core is a collectino of useful utility tools implemented in .NET Core aiming to increase productivity and keep code simplicity. Currently it includes: RESTful HTTP Client, Azure Storage Client, Redis Cache Client.

All tools are wrapped into single class Restme() to keep everything simple, the class will automatically identify whether it's used as HTTP Client, Azure Storage Client or Redis Cache Client.

Features

  • Implemented based on the latest .NET Core 1.0 (RC2)
  • Simple methods and flexible calls
  • Uses Newtonsoft JSON and allows custom serilization
  • Async support
  • HTTP Client
    • Parameters get automatically converted into query string or post form fields
    • Supports GET, POST, PUT, DELETE
    • Bearer Authentication and custom headers
  • Azure Storage Client
    • Simplified call stack
  • Redis Cache Client
    • Simplified call stack (Currently only support Azure Redis Cache, you can modify the source code to support other Redis Servers)

Nuget Package

    Install
Enter fullscreen mode Exit fullscreen mode

Top comments (0)