So far, we have cooked the things required for writing code for our REST web client; in this section, we will be writing actual code:
- Add a simple code to call or consume your product APIs.
If you created a new project in the same solution (refer to step 1 of the section Cooking the web client), please make sure that the project Product API is running before you start your web client project.
- Add a new class (Ctrl + Shift + C) in the Client folder and name it RestSharpWebClient.
- Now open the RestSharpWebClient class and add the following code:
private readonly RestClient _client = new RestClient("http://localhost:10065/api/");
The preceding code initializes RestClient of RestSharp and accepts the base URL as a string or a URI.
URI stands for Uniform Resource Identifier and is a representation of a string used to identify resources.
You may come across a scenario where there are multiple environments; in this case, you should store a URI where you point it as per your environment. For example, you can have the URI http://devserver:10065/api/ for your development environment or the URI http://testenv:10068/api/ for your QA environment. You should store these keys in the config file or somewhere similar so that the values are easily accessible. We recommend using new RestClient(somevariableforURI);.
Let's discuss the following code snippet to call or consume the GET /api/product
/productlist resource and populate the complete product list, as follows:
public List<ProductViewModel> GetProducts()
{
var request = new RestRequest("product/productlist", Method.GET);
var response = _client.Execute<List<ProductViewModel>>(request);
return response.Data ?? new List<ProductViewModel> {new
ProductViewModel()};
}
Here, we are making a GET request using RestRequest, where we passed a resource and method.
To get a particular product using productid, enter the following code:
public ProductViewModel GetProductDetails(string productId)
{
var request = new RestRequest("product/{productid}", Method.GET);
request.AddParameter("productid", productId);
var response = _client.Execute<ProductViewModel>(request);
return response.Data ?? new ProductViewModel();
}
In the preceding code block, the GetProductDetails method does a similar thing to the method GetProducts. The difference is that it accepts the parameter productId.
The following is the complete code of our REST client:
public class RestSharpWebClient
{
private readonly RestClient _client = new
RestClient("http://localhost:10065/api/");
public List<ProductViewModel> GetProducts()
{
var request = new RestRequest("product/productlist",
Method.GET);
var response = _client.Execute<List<ProductViewModel>>
(request);
//To avoid any exception lets return an empty view model
//On production environment return exact exception or your
custom code
return response.Data ?? new List<ProductViewModel> {new
ProductViewModel()};
}
public ProductViewModel GetProductDetails(string productId)
{
var request = new RestRequest("product/{productid}",
Method.GET);
request.AddParameter("productid", productId);
var response = _client.Execute<ProductViewModel>(request);
//To avoid any exception lets return an empty view model
//On production environment return exact exception or your
custom code
return response.Data ?? new ProductViewModel();
}
public bool AddProduct(ProductViewModel product)
{
var request = new RestRequest("product/addproduct",
Method.POST);
request.AddBody(product);
var response = _client.Execute(request);
return response.StatusCode == HttpStatusCode.OK;
}
public bool UpdateProduct(string id, ProductViewModel product)
{
var request = new RestRequest("updateproduct", Method.PUT);
request.AddQueryParameter("productid", id);
request.AddBody(product);
var response = _client.Execute(request);
return response.StatusCode == HttpStatusCode.NoContent;
}
public bool DeleteProduct(string id, ProductViewModel product)
{
var request = new RestRequest("deleteproduct", Method.DELETE);
request.AddQueryParameter("productid", id);
request.AddBody(product);
var response = _client.Execute(request);
return response.StatusCode == HttpStatusCode.NoContent;
}
}
With the preceding code snippet, you have now added the functionality that will call and consume your product APIs.