Wednesday, 18 January 2017

,

Nancy API Routing

Routing is most complicated and time consuming while developing Rest API. And.And we have many different things to think on and give time to them for performing well, so why can’t we save time in routing.This approach took me to create Nancy Rest API and one more time we are on same path of being smart developer.

we all know that for calling Rest API we have to hit URL and URL is address of the function which is execute when request from user comes in.

Nancy API following different type of way for creating function than web Api and it’s which always written in constructor of class and looks like lambda expression.We can also make URL with combination of base and derived class so if we have one parent class and many child class functionality and URL which we make for that child class function is include both parent and child class components.

CRUD is one of the main functionality of Rest API which include all type of HTTP methods like GET,PUT,POST and DELETE.


    GET : For fetching data and in search functionality.
    POST : For adding data
    PUT : For updating data
    DELETE : For Deleting data

    All the above things is in air so,Let’s look some real time routing example with explanation.
                    
      Let's consider first one,Get is indicating that we are using HTTP GET method,followed by that string which is inside of square braces indicate that URL of that Get function which written at last in curly braces and all the parameter which coming with request are bind in param.  

      For PUT and DELETE methods we have to add following code in our web.config because default it is not allowed and you get 405-Method Not Allowed.


        <system.webServer>
         <modules runAllManagedModulesForAllRequests="true">
           <remove name="WebDAVModule" />
         </modules>  
        </system.webServer>

        As you seen in above there is simple URL is present,but what to do if i want to pass some parameter in URL and also want that parameter to be a part of URL.

        All parameter which passing between browser and server are in key-value pair.So parameter which you want to as a part of URL, just include it with curly braces as show in below,


          GET[“api/v1/user/{id}”]=param=>{ return “Write Here ”;};
          URL becomes like below for parameter id=2
                
                           http://localhost/api/v1/user/2

            Let’s look into details of URL part,
              It’s just follow the best practices for API implementation and URL formation.


                api:It is best practice to write ‘api’ at the each start point of API URL,it's easy to maintain in future and another things is that it looks good ‘api’ word is part of actual API address.

                v1:It is nothing but the current version of API code.If we want to change something in same function and i want to keep both then we can bifercate using versioning approch.Clone the code in another folder then change version accroding to you,here it is v2 and you will change any function with same signature.Here it’s look like below,


                user:It is nothing but the real entity of your function.It’s can be anything like Employee,Product,Students etc.

                Here id is required part of URL you must have to pass it with URL.Each URL pointing to unique method and suppose you have two requirement as per below,
                1. Fetch user. 2. Fetch user by userId

                What you do?Are you making two different methods for above requirement?
                Yes,I have to make.But think if your method having something optional parameter then we can create something which fulfill both requirements in one method.Look at below example,


                  GET[“api/v1/user/{id?}”]=param=>{ return “ ”;};
                  Here question mark indicate that id is optional if you don’t passed id then URL become something like this  http://localhost/api/v2/user and you pass id=2 then it is like http://localhost/api/v2/user/2

                  In both case it is pointing to one methods which address is like above.

                  Thing is that if you going to do same in web api it’s required some 2 to 3 line code which overcome here in a single line with easy steps.

                  We can also provide default value to id parameter if it is not supply from client side or it segment don’t match.In that case it is like,


                    GET[“api/v1/user/{id?2}”]=param=>{ return “ ”;};
                    If we have to pass any of input then in that just put  asterisk at the end of the id,


                      GET[“api/v1/user/{id*}”]=param=>{ return “ ”;};
                      We can also make it some strictly type in rout segment by applying certain constraints to our route segments.we can do it just by adding “ : ” before name of constraints and place it at end of route segment.


                        GET[“api/v1/user/{id:int}”]=param=>{ return “ ”;};
                        Many different constraints are available which is very useful while custom routing.There is some constraints listed as per below,


                          int
                          Allows only Int32 (int) values.
                          long
                          Allows only Int64 (long) values.
                          decimal
                          Allows only decimal values.
                          guid
                          Allows only GUID values.
                          bool
                          Allows only boolean values
                          alpha
                          Allows only values containing alphabetical character
                          datetime
                          Allows only date values, optionally containing time.
                          min(minimum)
                          Allows only integer values with the specified minimum value.
                          max(maximum)
                          Allows only integer values with the specified maximum value.
                          range(minimum, maximum)
                          Allows only integer values within the specified range. (Between minimum and maximum)
                          minlength(length)
                          Allows only values longer than the specified minimum length.
                          maxlength(length)
                          Allows only values shorter that the maximum length.
                          length(minimum, maximum)
                          Allows only values with length within the specified range. (Between minimum and maximum)

                          It is some basic concept and overview of routing with Nancy API,I hope it  add some sharpen thing in your technical knowledge which is helpful in your career path.
                          Share:

                          0 comments:

                          Post a Comment