Creating C# Wrapper Over Firebase API For Basic CRUD

Here we will see how to create a wrapper around Firebase REST API with C#, so .Net devlopers can consume it quickly.

Section 1 Create App in Firebase Console

Log into your Firebase account and click on "GO TO CONSOLE" to access the console.

C#

Click on "Add Project" to add a new Firebase project.

C#

Give "Project Name" of your choice, then select your Country/Region and hit "CREATE PROJECT".

C#

Now, you will be navigated to your newly created App Dashboard with all the Firebase features listed. Click on "Database" from the left pane.

C#

You can see your service plan as Spark which is Free. All the data stored is represented as JSON tree. You have database URL along with the root of JSON tree, which is null as of now. This database URL will be used to make HTTP request later in the article.

Section 2 Explore Firebase REST API with Postman

Before making wrapper, let’s get comfortable with Firebase REST API with Postman. You can get postman from here (https://www.getpostman.com/apps).

Firebase read/write access is secured by default as you can see in RULES.

C#

For the sake of simplicity in making HTTP requests, we will remove authentication for this article. In upcoming articles, we will continue with authentication.

 Make auth==null; and hit "PUBLISH" to remove authentication.

C#

If you are already familiar with Firebase REST API, you can directly jump to Section 3.

Writing Data with PUT

I have some data about a team and its members as follows. We are writing this data to root node of our database JSON tree.

  1. {  
  2.   "Team-Awesome": {  
  3.     "Members": {  
  4.       "M1": {  
  5.         "City""Hyderabad",  
  6.         "Name""Ashish"  
  7.       },  
  8.       "M2": {  
  9.         "City""Cyberabad",  
  10.         "Name""Vivek"  
  11.       },  
  12.       "M3": {  
  13.         "City""Secunderabad",  
  14.         "Name""Pradeep"  
  15.       }  
  16.     }  
  17.   }  
  18. }  

In Postman window, set method as PUT; in URL, enter your database URL appended with .json as we are writing to root node.
The .json suffix is always required if we are making REST calls. In Body, select raw and paste your JSON data there, hit "Send" now to make the request.

C#

You can see response code "200 OK" in Postman instead of 201 Created, but this is how Firebase returns it. Every request will return "200 OK" if successful, irrespective of the Request method.

C#

Created data will immediately be reflected to your app console. You can verify it.

C#

Reading Data with GET

To read data, we can give path to specific node which we wish to read. Here, I just want the details of Member M1 so I have given specific path in URL. Set method as GET and hit "Send" to retrieve the data.

C#

It would return the details of a specific member.

C#

Pushing Data with POST

In Firebase, POST pushes data to the specific node with auto-generated key; it never replaces existing data.

Here, we are adding scores for all team members, so our node is Scores here which is not already available in JSON tree. Firebase will automatically create it.

C#

You can see in console Scores node is created and data is added along with random keys.

C#

Updating Data with PATCH

To update data in a node we have to use PATCH, it can be used to add new fields also. Here we will update city of a member.

C#

You can observe in console whether value is updated.

C#

Removing Data with DELETE

To remove a resource DELETE is used. Let’s delete Member M3.

C#

Member M3 is removed, can be seen in console.

C#

Section 3 Building C# wrapper

Add new project of type Class Library with name FirebaseNet, or name of your choice.

I have taken .Net Standard here for broader compatibility, you can take .Net framework class library if it’s not available.

C#

Create folder Database and add 4 classes as follows.

  1. FirebaseDB.cs
  2. FirebaseRequest.cs
  3. FirebaseResponse.cs
  4. UtilityHelper.cs

Let’s look into each file one by one.

FirebaseDB.cs

  1. namespace FirebaseNet.Database  
  2. {  
  3.     using System;  
  4.     using System.Net.Http;  
  5.   
  6.     /// <summary>  
  7.     /// FirebasDB Class is reference of a Firebase Database  
  8.     /// </summary>  
  9.     public class FirebaseDB  
  10.     {  
  11.         /// <summary>  
  12.         /// Initializes a new instance of the <see cref="FirebaseDB"/> class with base url of Firebase Database  
  13.         /// </summary>  
  14.         /// <param name="baseURL">Firebase Database URL</param>  
  15.         public FirebaseDB(string baseURL)  
  16.         {  
  17.             this.RootNode = baseURL;  
  18.         }  
  19.   
  20.         /// <summary>  
  21.         /// Gets or sets Represents current full path of a Firebase Database resource  
  22.         /// </summary>  
  23.         private string RootNode { get; set; }  
  24.           
  25.         /// <summary>  
  26.         /// Adds more node to base URL  
  27.         /// </summary>  
  28.         /// <param name="node">Single node of Firebase DB</param>  
  29.         /// <returns>Instance of FirebaseDB</returns>  
  30.         public FirebaseDB Node(string node)  
  31.         {  
  32.             if (node.Contains("/"))  
  33.             {  
  34.                 throw new FormatException("Node must not contain '/', use NodePath instead.");  
  35.             }  
  36.   
  37.             return new FirebaseDB(this.RootNode + '/' + node);  
  38.         }  
  39.   
  40.         /// <summary>  
  41.         /// Adds more nodes to base URL  
  42.         /// </summary>  
  43.         /// <param name="nodePath">Nodepath of Firebase DB</param>  
  44.         /// <returns>Instance of FirebaseDB</returns>  
  45.         public FirebaseDB NodePath(string nodePath)  
  46.         {  
  47.             return new FirebaseDB(this.RootNode + '/' + nodePath);  
  48.         }  
  49.   
  50.         /// <summary>  
  51.         /// Make Get request  
  52.         /// </summary>  
  53.         /// <returns>Firebase Response</returns>  
  54.         public FirebaseResponse Get()  
  55.         {  
  56.             return new FirebaseRequest(HttpMethod.Get, this.RootNode).Execute();  
  57.         }  
  58.   
  59.         /// <summary>  
  60.         /// Make Put request  
  61.         /// </summary>  
  62.         /// <param name="jsonData">JSON string to PUT</param>  
  63.         /// <returns>Firebase Response</returns>  
  64.         public FirebaseResponse Put(string jsonData)  
  65.         {  
  66.             return new FirebaseRequest(HttpMethod.Put, this.RootNode, jsonData).Execute();  
  67.         }  
  68.   
  69.         /// <summary>  
  70.         /// Make Post request  
  71.         /// </summary>  
  72.         /// <param name="jsonData">JSON string to POST</param>  
  73.         /// <returns>Firebase Response</returns>  
  74.         public FirebaseResponse Post(string jsonData)  
  75.         {  
  76.             return new FirebaseRequest(HttpMethod.Post, this.RootNode, jsonData).Execute();  
  77.         }  
  78.   
  79.         /// <summary>  
  80.         /// Make Patch request  
  81.         /// </summary>  
  82.         /// <param name="jsonData">JSON sting to PATCH</param>  
  83.         /// <returns>Firebase Response</returns>  
  84.         public FirebaseResponse Patch(string jsonData)  
  85.         {  
  86.             return new FirebaseRequest(new HttpMethod("PATCH"), this.RootNode, jsonData).Execute();  
  87.         }  
  88.   
  89.         /// <summary>  
  90.         /// Make Delete request  
  91.         /// </summary>  
  92.         /// <returns>Firebase Response</returns>  
  93.         public FirebaseResponse Delete()  
  94.         {  
  95.             return new FirebaseRequest(HttpMethod.Delete, this.RootNode).Execute();  
  96.         }  
  97.   
  98.         /// <summary>  
  99.         /// To String  
  100.         /// </summary>  
  101.         /// <returns>Current resource URL as string</returns>  
  102.         public override string ToString()  
  103.         {  
  104.             return this.RootNode;  
  105.         }  
  106.     }  
  107. }  

  • FirebaseDB is main class user interacts with, it exposes the API to be consumed by user of library.
  • It’s constructor takes baseURI of Firebase Database as parameter and assigns to RootNode private property.
  • Class is using Method Chaining to maintain resource URI with the help of Node() & NodePath() They are taking string as input and appending to RootNode and returns FirebaseDB object with combined URI. Here Node can take only single node whether NodePath is for multiple Nodes.
  • Class has methods for each Firebase supported HTTP method which are returning FirebaseResponse Put, Post & Patch methods are having parameter to accept JSON data as string, whether Get & Delete don’t need it. They all are instantiating FirebaseRequest with HttpMethod object according to request type, resource URI as RootNode and optional parameter jsonData as string.
  • ToString() is overridden here to return current resource URI.

FirebaseRequest.cs

  1. namespace FirebaseNet.Database  
  2. {  
  3.     using System;  
  4.     using System.Net.Http;  
  5.   
  6.     /// <summary>  
  7.     /// Firebase Request  
  8.     /// </summary>  
  9.     class FirebaseRequest  
  10.     {  
  11.         /// <summary>  
  12.         /// Suffix to be added in each resource URI  
  13.         /// </summary>  
  14.         private const string JSON_SUFFIX = ".json";  
  15.   
  16.         /// <summary>  
  17.         /// Initializes a new instance of the <see cref="FirebaseRequest"/> class  
  18.         /// </summary>  
  19.         /// <param name="method">HTTP Method</param>  
  20.         /// <param name="uri">URI of resource</param>  
  21.         /// <param name="jsonString">JSON string</param>  
  22.         public FirebaseRequest(HttpMethod method, string uri, string jsonString = null)  
  23.         {  
  24.             this.Method = method;  
  25.             this.JSON = jsonString;  
  26.             if (uri.Replace("/", string.Empty).EndsWith("firebaseio.com"))  
  27.             {  
  28.                 this.Uri = uri + '/' + JSON_SUFFIX;  
  29.             }  
  30.             else  
  31.             {  
  32.                 this.Uri = uri + JSON_SUFFIX;  
  33.             }  
  34.         }  
  35.   
  36.         /// <summary>  
  37.         /// Gets or sets HTTP Method  
  38.         /// </summary>  
  39.         private HttpMethod Method { get; set; }  
  40.   
  41.         /// <summary>  
  42.         /// Gets or sets JSON string  
  43.         /// </summary>  
  44.         private string JSON { get; set; }  
  45.   
  46.         /// <summary>  
  47.         /// Gets or sets URI  
  48.         /// </summary>  
  49.         private string Uri { get; set; }  
  50.   
  51.         /// <summary>  
  52.         /// Executes a HTTP requests  
  53.         /// </summary>  
  54.         /// <returns>Firebase Response</returns>  
  55.         public FirebaseResponse Execute()  
  56.         {  
  57.             Uri requestURI;  
  58.             if (UtilityHelper.ValidateURI(this.Uri))  
  59.             {  
  60.                 requestURI = new Uri(this.Uri);  
  61.             }  
  62.             else  
  63.             {  
  64.                 return new FirebaseResponse(false"Proided Firebase path is not a valid HTTP/S URL");  
  65.             }  
  66.   
  67.             string json = null;  
  68.             if (this.JSON != null)  
  69.             {  
  70.                 if (!UtilityHelper.TryParseJSON(this.JSON, out json))  
  71.                 {  
  72.                     return new FirebaseResponse(false, string.Format("Invalid JSON : {0}", json));  
  73.                 }  
  74.             }  
  75.   
  76.             var response = UtilityHelper.RequestHelper(this.Method, requestURI, json);  
  77.             response.Wait();  
  78.             var result = response.Result;  
  79.   
  80.             var firebaseResponse = new FirebaseResponse()  
  81.             {  
  82.                 HttpResponse = result,  
  83.                 ErrorMessage = result.StatusCode.ToString() + " : " + result.ReasonPhrase,  
  84.                 Success = response.Result.IsSuccessStatusCode  
  85.             };  
  86.   
  87.              if (this.Method.Equals(HttpMethod.Get))  
  88.             {  
  89.                 var content = result.Content.ReadAsStringAsync();  
  90.                 content.Wait();  
  91.                 firebaseResponse.JSONContent = content.Result;  
  92.             }  
  93.   
  94.             return firebaseResponse;  
  95.         }  
  96.     }  
  97. }  

  • FirebaseRequet class is where actual magic happens. Constructor takes Http Method, Firebase URI and optional JSON data as parameter. It also appends .json to URI which is necessary to make REST calls, In base base URI it should be /.json while URI with any Node needs only .json to be appended.
  • Execute() is responsible for making HTTP calls with help of RequestHelper() of UtilityHelper It validates URI and JSON data, if invalid then it returns FirebaseResponse with Success=False and proper ErrorMessage.
  • If all parameters are valid, it makes HTTP calls, and forms FirebaseResponse object and returns it. If Request if of type GET then it parses response content to string and appends it to FirebaseResponse object to be returned to caller.
  • RequestHelper() is an Asynchronous method, which is returns Task<HttpResponseMessage>, but in Execute method with use of Wait() and Result we are handling it synchronously. In further enhancement Asynchronous API will also be exposed to consumer.

FirebaseResponse.cs

  1. namespace FirebaseNet.Database  
  2. {  
  3.     using System.Net.Http;  
  4.   
  5.     /// <summary>  
  6.     /// Firebase Response  
  7.     /// </summary>  
  8.     public class FirebaseResponse  
  9.     {  
  10.         /// <summary>  
  11.         /// Initializes a new instance of the <see cref="FirebaseResponse"/> class  
  12.         /// </summary>  
  13.         public FirebaseResponse()  
  14.         {  
  15.         }  
  16.   
  17.         /// <summary>  
  18.         /// Initializes a new instance of the <see cref="FirebaseResponse"/> class  
  19.         /// </summary>  
  20.         /// <param name="success">If Success</param>  
  21.         /// <param name="errorMessage">Error Message</param>  
  22.         /// <param name="httpResponse">HTTP Response</param>  
  23.         /// <param name="jsonContent">JSON Content</param>  
  24.         public FirebaseResponse(bool success, string errorMessage, HttpResponseMessage httpResponse = null, string jsonContent = null)  
  25.         {  
  26.             this.Success = success;  
  27.             this.JSONContent = jsonContent;  
  28.             this.ErrorMessage = errorMessage;  
  29.             this.HttpResponse = httpResponse;  
  30.         }  
  31.   
  32.         /// <summary>  
  33.         /// Gets or sets Boolean status of Success  
  34.         /// </summary>  
  35.         public bool Success { get; set; }  
  36.   
  37.         /// <summary>  
  38.         /// Gets or sets JSON content returned by the Request  
  39.         /// </summary>  
  40.         public string JSONContent { get; set; }  
  41.   
  42.         /// <summary>  
  43.         /// Gets or sets Error Message if Any  
  44.         /// </summary>  
  45.         public string ErrorMessage { get; set; }  
  46.   
  47.         /// <summary>  
  48.         /// Gets or sets full Http Response  
  49.         /// </summary>  
  50.         public HttpResponseMessage HttpResponse { get; set; }  
  51.     }  
  52. }  

FirebaseResponse represents output of API calls against FirebaseDB. It contains 4 public properties as follows.

  1. Success
  2. JSONContent
  3. ErrorMessage
  4. HttpResponse

    • In Firebase.Net I have tried to not surprise the consumer by throwing any unexpected error, so FirebaseResponse has boolean property Success, which tells you whether call is successful or not, along with proper message in ErrorMessage
    • Error will not always be a Http Error, it could be related to invalid JSON or improper URI also, they both are handled here.
    • If HTTP call is successful Success will have True, ErrorMessage will be null, HttpResponse will contain complete HttpResponseMessage and if request type is GET then JSONContent will contain the response JSON as string.

UtilityHelper.cs

  1. namespace FirebaseNet.Database  
  2. {  
  3.     using System;  
  4.     using System.Net.Http;  
  5.     using System.Text;  
  6.     using System.Threading.Tasks;  
  7.     using Newtonsoft.Json.Linq;  
  8.   
  9.     /// <summary>  
  10.     /// Utility Helper Class  
  11.     /// </summary>  
  12.     class UtilityHelper  
  13.     {  
  14.         /// <summary>  
  15.         /// User Agent Header in HTTP Request  
  16.         /// </summary>  
  17.         private const string USER_AGENT = "firebase-net/1.0";  
  18.   
  19.         /// <summary>  
  20.         /// Validates a URI  
  21.         /// </summary>  
  22.         /// <param name="url">URI as string</param>  
  23.         /// <returns>True if valid</returns>  
  24.         public static bool ValidateURI(string url)  
  25.         {  
  26.             Uri locurl;  
  27.             if (System.Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out locurl))  
  28.             {  
  29.                 if (  
  30.                     !(locurl.IsAbsoluteUri &&  
  31.                       (locurl.Scheme == "http" || locurl.Scheme == "https")) ||  
  32.                     !locurl.IsAbsoluteUri)  
  33.                 {  
  34.                     return false;  
  35.                 }  
  36.             }  
  37.             else  
  38.             {  
  39.                 return false;  
  40.             }  
  41.   
  42.             return true;  
  43.         }  
  44.   
  45.         /// <summary>  
  46.         /// Validates JSON string  
  47.         /// </summary>  
  48.         /// <param name="inJSON">JSON to be validatedd</param>  
  49.         /// <param name="output">Valid JSON or Error Message</param>  
  50.         /// <returns>True if valid</returns>  
  51.         public static bool TryParseJSON(string inJSON, out string output)  
  52.         {  
  53.             try  
  54.             {  
  55.                 JToken parsedJSON = JToken.Parse(inJSON);  
  56.                 output = parsedJSON.ToString();  
  57.                 return true;  
  58.             }  
  59.             catch (Exception ex)  
  60.             {  
  61.                 output = ex.Message;  
  62.                 return false;  
  63.             }  
  64.         }  
  65.   
  66.         /// <summary>  
  67.         /// Makes Asynchronus HTTP requests  
  68.         /// </summary>  
  69.         /// <param name="method">HTTP method</param>  
  70.         /// <param name="uri">URI of resource</param>  
  71.         /// <param name="json">JSON string</param>  
  72.         /// <returns>HTTP Responce as Task</returns>  
  73.         public static Task<HttpResponseMessage> RequestHelper(HttpMethod method, Uri uri, string json = null)  
  74.         {  
  75.             var client = new HttpClient();  
  76.             var msg = new HttpRequestMessage(method, uri);  
  77.             msg.Headers.Add("user-agent", USER_AGENT);  
  78.             if (json != null)  
  79.             {  
  80.                 msg.Content = new StringContent(  
  81.                     json,  
  82.                     UnicodeEncoding.UTF8,  
  83.                     "application/json");  
  84.             }  
  85.   
  86.             return client.SendAsync(msg);  
  87.         }  
  88.     }  
  89. }  

  • UtilityHelper contains 3 static methods to keep our keep running.
  • ValidateURI() takes URI as string and validates it using TryCreate() of Uri class and returns Boolean.
  • TryParseJSON() validates and format JSON string, Firebase does not allow JSON which uses single quotes instead of double quotes, but in C# most people will pass in single quotes because of multiline support, very few will use escape character for double quotes. That’s why I’m parsing it which validates JSON as well as calling ToString() on JToken object will returns me well formatted JSON in double quotes.
  • If valid returns True and out parameter output contains Formatted JSON.
  • If invalid then returns False and out parameter output contains Error Message.
  • RequestHelper() is an asynchrnous method, which is responsibe for making all HTTP calls to Firebase Database.

Section 4 Library in Action

Let’s consume our newly created library and see how it works. Create New Console App and give name of your choice.

C#

Now update your Program.cs with following code.

Program.cs

  1. namespace Examples.NetCore  
  2. {  
  3.     using FirebaseNet.Database;  
  4.     using static System.Console;  
  5.   
  6.     /// <summary>  
  7.     /// Examples  
  8.     /// </summary>  
  9.     public class Program  
  10.     {  
  11.         /// <summary>  
  12.         /// Main Method  
  13.         /// </summary>  
  14.         public static void Main()  
  15.         {  
  16.             // Instanciating with base URL  
  17.             FirebaseDB firebaseDB = new FirebaseDB("https://c-sharpcorner-2d7ae.firebaseio.com");  
  18.   
  19.             // Referring to Node with name "Teams"  
  20.             FirebaseDB firebaseDBTeams = firebaseDB.Node("Teams");  
  21.   
  22.             var data = @"{  
  23.                             'Team-Awesome': {  
  24.                                 'Members': {  
  25.                                     'M1': {  
  26.                                         'City''Hyderabad',  
  27.                                         'Name''Ashish'  
  28.                                         },  
  29.                                     'M2': {  
  30.                                         'City''Cyberabad',  
  31.                                         'Name''Vivek'  
  32.                                         },  
  33.                                     'M3': {  
  34.                                         'City''Secunderabad',  
  35.                                         'Name''Pradeep'  
  36.                                         }  
  37.                                    }  
  38.                                }  
  39.                           }";  
  40.   
  41.             WriteLine("GET Request");  
  42.             FirebaseResponse getResponse = firebaseDBTeams.Get();  
  43.             WriteLine(getResponse.Success);  
  44. if(getResponse.Success)  
  45.                 WriteLine(getResponse.JSONContent);  
  46.             WriteLine();  
  47.   
  48.             WriteLine("PUT Request");  
  49.             FirebaseResponse putResponse = firebaseDBTeams.Put(data);  
  50.             WriteLine(putResponse.Success);  
  51.             WriteLine();  
  52.   
  53.             WriteLine("POST Request");  
  54.             FirebaseResponse postResponse = firebaseDBTeams.Post(data);  
  55.             WriteLine(postResponse.Success);  
  56.             WriteLine();  
  57.   
  58.             WriteLine("PATCH Request");  
  59.             FirebaseResponse patchResponse = firebaseDBTeams  
  60.                 // Use of NodePath to refer path lnager than a single Node  
  61.                 .NodePath("Team-Awesome/Members/M1")  
  62.                 .Patch("{\"Designation\":\"CRM Consultant\"}");  
  63.             WriteLine(patchResponse.Success);  
  64.             WriteLine();  
  65.   
  66.             WriteLine("DELETE Request");  
  67.             FirebaseResponse deleteResponse = firebaseDBTeams.Delete();  
  68.             WriteLine(deleteResponse.Success);  
  69.             WriteLine();  
  70.   
  71.             WriteLine(firebaseDBTeams.ToString());  
  72.             ReadLine();  
  73.         }  
  74.     }  
  75. }  

Execution

C#

  • Here, I am calling all the methods in API. First I am instantiating FirebaseDB object with base URI of my Firebase App, you may replace it with your App URI. Then in next line Node(“Teams”) is used to point object to a different resource URI. “Teams” don’t need to be already available in your Firebase database.
  • Then we have some JSON data in which is a multiline JSON string using single quotes, It is supported in Library but Firebase. So you can use it here.
  • Next, we have "Get() call" which is returning FirebaseResponse in getResponse. It is always a good idea to verify success before calling getResponse.JSONContent, like I am doing here.
  • Now we are calling Put() & Post() with given data, behaviour of these HTTP calls is already described in Section 2.
  • Patch() is interesting here because of 2 reasons, First it is used along with NodePath() which is accessing 3 nodes depth. And second JSON data is passed using escape characters for double quotes, which is how Firebase actually accepts data.
  • Now "Delete()" called a method which deletes complete JSON tree at given URI.
  • Finally ToString() method prints current resource URI of Firebase Database.

I will keep adding new features to this library, you can track in further articles and repository. You can also contribute here.

Current stage of code can be found here.


Similar Articles