Saturday, October 13, 2012

RestUri in Resty.Net

Uris are very much basic part of the whole REST conceptual model, the Uniform Resource Locator (Uri) points to a unique resource. REST is all about performing operation on those resources. The RestUri class in Resty.Net is divided into mainly two parts:
  1. The Base Uri.
  2. The Resource Uri

The Base Uri:

The base Uri points the server where the REST resources exists. eg: http://localhost/ , https://localhost/ , https://locahost:50001 , etc. 

The Resource Uri:

The resource Uri points to the resource on the server. e.g. /Person, /Person/1, /book etc. The resource uri can be resource template which has variables embedded, e.g. /Person/{id}

API documentation:


Constructors:
  • RestUri(string baseUri)
    • Instantiate a RestUri with the complete url in a single string. 
      • e.g new RestUri("http://example.com/Person/{id}");
  • RestUri(string baseUri, string resourceUri)
    • Instantiate a RestUri with base url set to baseUri and the resource uri template set to resourceUri.
      • e.g. new RestUri("http://example.com","/Person/{id}");
  • RestUri(Uri baseUri, string resourceUri)
  • RestUri(Uri baseUri, Uri resourceUri)

Methods:
  • SetQuery(string name, object value)
    • Set a querystring parameter for the RestUri instance. Returns the current instance of RestUri hence making it possible to chain the methods in fluent manner.
      • e.g. new RestUri("http://example.com","/Person").SetQuery("searchTerm", "bob")
      • => http://example.com/Person?searchTerm=bob
  • SetQuery(object queryParams)
    • Set a querystring parameter for the RestUri instance. Returns the current instance of RestUri hence making it possible to chain the methods in fluent manner.
      • e.g. new RestUri("http://example.com","/Person/{id}").SetQuery(new { searchTerm = "bob"})
      • => http://example.com/Person?searchTerm=bob
  • SetParameter(string name, object value)
    • Set uri template parameter value.
      • e.g. new RestUri("http://example.com","/Person/{id}").SetParameter("id", 1)
      • => http://example.com/Person/1
  • SetParameter(object uriTemplateParams)
    • Set uri template parameter value.
      • e.g. new RestUri("http://example.com","/Person/{id}").SetParameter(new {id = 1})
      • => http://example.com/Person/1

Properties:
  • BaseUri
    • Gets the base uri of a request.

Setting multiple querystrings


Fluent style:
new RestUri("http://example.com","/Person")
        .SetQuery("searchTerm", "bob")
        .SetQuery("page",1)
        .SetQuery("perpage", 10);
=> http://example.com/Person?searchTerm=bob&page=1&perpage=10

Anonymous style:
new RestUri("http://example.com","/Person")
        .SetQuery(new {searchTerm = "bob", page= 1,perpage=10});
=> http://example.com/Person?searchTerm=bob&page=1&perpage=10

Setting multiple template parameters


Fluent style:
new RestUri("http://example.com","/Person/{searchTemr}/{page}/{perpage}")
        .SetParameter("searchTerm","bob")
        .SetParameter("page", 1)
        .SetParameter("perpage", 10);
=> http://example.com/Person/bob/1/10

Anonymous style:
new RestUri("http://example.com","/Person/{searchTemr}/{page}/{perpage}")
        .SetParameter(new{searchTerm = "bob", page= 1,perpage=10});
=> http://example.com/Person/bob/1/10

How RestUri sits together with RestRequest?

RestUri restUri = new RestUri("http://example.com","/Person/{searchTemr}/{page}/{perpage}").SetParameter(new{searchTerm = "bob", page= 1,perpage=10});
RestRequest request = new RestRequest(HttpMethod.Get, restUri);

No comments:

Post a Comment