Being a popular e-commerce platform, nopCommerce is used in a variety of senarios. One popular nopCommerce use case is to integrate it with third-party ERP / CRM / POS solutions. For example, it is possible to develop an integration such that the POS system pulls product & customer information from nopCommerce, and places order to nopCommerce.
There are several method to implement the integration and nopCommerce already comes with a Web Service plugin (Nop.Plugin.Misc.WebServices). However, with the popularity of REST service, I feel a need to develop an nopCommerce REST API plugin to fill in the gap - and that's how NopRest - nopCommerce REST Service Plugin is born.
Using NopRest is easy as it follows a very intuitive, MVC-like architecture. In fact, it is implemented on top of MVC. For those who are familiar with ASP.Net Web API, you might be wondering why NopRest is not implemented on top of Web API. The reason is simple - I want to reuse as much code from nopCommerce as possible. A more detailed explanation is published here.
To provide a real world example of how NopRest can be used, I am sharing with you a use case whereby my client requires to clear nopCommerce cache from external WPF application.
Implementing NopRest nopCommerce REST API
Imagine you are using WPF to add / update product data, and for the URL record to take place, you need to clear the cache.
If you download NopRest's source code, you find that we do not have any existing method to do this. Since this is an open-source project, there is nobody stopping you from adding the method on your own!
Assuming you have downloaded the source code, all you need to do is to add the following method in ApiController.cs.
[HttpPost]
public ActionResult ClearCache(string apiToken)
{
if (!IsApiTokenValid(apiToken))
return InvalidApiToken(apiToken);
_cacheManager.Clear();
return Successful(null);
}
What this method does is to simply make use of ICacheManager to clear the cache. There is nothing fancy you need to do as the REST API architecture has already been coded in the plugin.
Using NopRest nopCommerce REST Service Plugin
Now that you've implemented the required plugin code, all you need to do is:
- Rebuild the plugin in Visual Studio.
- Deploy the plugin (and install it) on nopCommerce
- Once the plugin is installed, you need to generate an API Token. (Look at the method we have implemented earlier)
Now that everything is in place, you need to actually issue a HTTP POST request to the plugin. In WPF's case, you can make use of WebClient or WebRequest to issue the request from within your code. The URL to which you should issue the request has the format of http(s)://yourstore.com/Api/ClearCache?apiToken={REPLACE_WITH_YOUR_TOKEN}. You will also receive a response in JSON format.
Help Develop the Plugin
What I've describe here is a very simple use case. The plugin can of course do things that are much more meaningful, such as Add to Cart, Register New Client, Add New Product and etc. from external applications.
I invite you to Fork the source code to implement your new methods, and do a Pull Request when you have done. With the Pull Requests, I then can merge your code into the main branch! By doing so you'll be contributing directly to this exciting plugin!