A C# Simplified RESTful Client, Redis Client and Azure Storage Client in .NET Core

The new .NET Core is a  platform framework that has finally come into my life to shake my coding style - now coding in my favorite OS - MacOSX.
 
Having spent a few hours in coding under .NET Core 1.0 (RC2), I created the RESTme toolkit. I originally was thinking to just create a RESTful client in the first hour, and then decided to make it more powerful by enabling it to communicate with Azure Storage and Redis Cache - why not? Since they are all about requesting and saving data!
 
General Code Example
  1. //HTTP Restful client (sudo code)  
  2. // Get data  
  3. rest.Get(requestUrl);  
  4. // Save data  
  5. rest.Add(paramKey, paramValue);  
  6. rest.Post(requestUrl, data);  
  7. // Delete data  
  8. rest.Delete(requestUrl);  
  9.   
  10. //Azure Storage or Redis ache sudo-code  
  11. // Get data  
  12. rest.Get<Stream>("/container/filePath");  
  13. rest.Get("/container/jsonData");  
  14. rest.Get<myObject>("/container/filePath");  
  15.   
  16. // Save data  
  17. rest.Post<myObject>("/container/filePath", myObject);  
  18. // Delete data  
  19. rest.Delete("/container/filePath");   
Detailed Examples 
  1. Use it as a basic RESTful HTTP Client.
    1. //direct string JSON return    
    2. var rest = new Restme(new Uri("http://freegeoip.net"));  
    3. var result1 = rest.Get("/json/github.com");  
    4.   
    5. //automatic Generic cast    
    6. var result2 = rest.Get < MyObject > ("/json/github.com");  
    7. var resultAsync2 = await rest.GetAsync << MyObjecT > ("/json/github.com");  
    8.   
    9. //add parameters (Parameters get automatically converted into query string or post form fields)    
    10. rest.add("q""github.com");  
    11. var result3 = rest.Get < MyObject > ("/json");  
    12. var resultAsync3 = await rest.GetAsync < MyObjecT > ("/json");  
    13.   
    14. //supports POST, DELETE, PUT etc.    
    15. var rest2 = new Restme(new Uri("http://example.com"));  
    16. rest2.Add("Username""[email protected]");  
    17. rest2.Add("Birthday", DateTime.UtcNow);  
    18. rest2.Post < MyObject > ("/someurl");  
    19.   
    20. rest3.PostAsync < MyObject > ("/asyncexample");  
    21.   
    22.   
    23. //supports direct object submission    
    24. var myObject = new MyObject()  
    25. {  
    26.     Username = "[email protected]",  
    27.         Birthday = DateTime.UtcNow  
    28. };  
    29. var rest3 = new Restme(new Uri("http://example.com"));  
    30. rest3.Add(myObject);  
    31. rest3.Post < ExpectedResultObject > ("/directObjectPost");  
  2. Use it as an Azure Storage Client,
    1. //get blob stream directly      
    2. var blobStorageConnectionString = "{Your Storage Account Connection String}";  
    3.   
    4.   
    5. var rest = new Restme(blobStorageConnetionString);  
    6.   
    7.   
    8. rest.CreateAzureBlobContainerIfNotExists = true//do this only if you want to auto create the container        
    9.   
    10.   
    11. //NOTE 1: first segment of the path should always be your container name      
    12.   
    13.   
    14. //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      
    15.   
    16.   
    17. //NOTE 2: use a type of Stream if you want the original value retrieved from the blob      
    18.   
    19.   
    20. rest.Get < Stream > ("/myContainer/myfilePath");  
    21.   
    22. rest.GetAsync < Stream > ("/myContainer/myfilePath");  
    23.   
    24.   
    25. //NOTE: only blob items saved in JSON format is suppported      
    26.   
    27.   
    28. rest.Get < ObjectType > ("/myContainer/myfileObjectInJSONFileFormat");  
    29.   
    30. rest.GetAsync < ObjectType > ("/myContainer/myfileObjectInJSONFileFormat");  
  3. Use it as a Redis Cache Client,
    1. var redisConnectionString = "{Your Redis Cache Connection String}";    
    2. var rest = new Restme(redisConnectionString);    
    3.     
    4. //get cache data (support Generic cast)    
    5. var cacheResult = rest.Get("home:testKey");    
    6. var cacheResult2 = rest.Get<bool>("home:testKey2");    
    7. var cacheResult3 = rest.Get<ObjectType>("home:testKey3");    
    8.     
    9. //set cache data    
    10. rest.Post("home:testKey","value");    
    11. rest.Post<bool>("home:testKey2",true);    
    12.     
    13. var myObject = new ObjectType();  //will be serialized into JSON format and stored as string on redis server    
    14. rest.Post<ObjectType>("home:testKey3", myObject);  
I've made the code open source under MIT on github.
Ebook Download
View all
Learn
View all