Calling web API from a stand alone asp.net application

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
oa2a
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Wed Oct 01, 2014 7:12 am

Calling web API from a stand alone asp.net application

Post by oa2a » Wed Oct 01, 2014 10:58 am

I have a standalone asp.net application and i need to be able to call AbleCommerce Gold Web Api the following methods from the code behind in c#:
api/AdminProducts
api/AdminProducts/{id}
api/AdminProducts
api/AdminProducts/{id}
and so on....

Please advise how this should be setup?

Thanks in advance!

User avatar
Shopping Cart Admin
AbleCommerce Admin
AbleCommerce Admin
Posts: 3055
Joined: Mon Dec 01, 2003 8:41 pm
Location: Vancouver, WA
Contact:

Re: Calling web API from a stand alone asp.net application

Post by Shopping Cart Admin » Wed Oct 01, 2014 11:08 am

Hello,

Have you seen the following information in our wiki?

http://wiki.ablecommerce.com/index.php/ ... ld_Web_Api
Thanks for your support

Shopping Cart Guru
AbleCommerce.com
Follow us on Facebook

oa2a
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Wed Oct 01, 2014 7:12 am

Re: Calling web API from a stand alone asp.net application

Post by oa2a » Wed Oct 01, 2014 12:28 pm

Yes, i have and it is not very helpful with the specifics..

Can you provide an example of how this should be called from the c# code.

Also do i need to include a web api dll in my web app ?

User avatar
Katie
AbleCommerce Admin
AbleCommerce Admin
Posts: 2651
Joined: Tue Dec 02, 2003 1:54 am
Contact:

Re: Calling web API from a stand alone asp.net application

Post by Katie » Wed Oct 01, 2014 12:33 pm

This is a code example -

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js" defer="defer"></script>
    <script language="javascript" type="text/javascript">
        var productIndex = 0;

        function loadProduct() {
            $.getJSON("api/products?$top=1&$skip=" + productIndex, "", readProduct);
            if (productIndex < 1) { $("#previousButton").prop("disabled", "disabled"); }
            else { $("#previousButton").prop("disabled", ""); }
        }

        function readProduct(data) {
            $("#productName").text(data[0].Name);
        }

        function getPrevious() {
            productIndex -= 1;
            loadProduct();   
        }

        function getNext() {
            productIndex += 1;
            loadProduct();
        }
    </script>
</head>
<body onload="loadProduct()">
<span id="productName"></span>
<br />
<input id="previousButton" type="button" value="Previous" onclick="getPrevious()" />
<input id="nextButton" type="button" value="Next" onclick="getNext()" />
</body>
</html>
Thank you for choosing AbleCommerce!

http://help.ablecommerce.com - product support
http://wiki.ablecommerce.com - developer support

oa2a
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Wed Oct 01, 2014 7:12 am

Re: Calling web API from a stand alone asp.net application

Post by oa2a » Wed Oct 01, 2014 12:43 pm

Thank you for your example. This is a Readonly example using JSON. I need an ability to add/delete products from C# code. Can you provide an example of that?

Thanks!

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Calling web API from a stand alone asp.net application

Post by mazhar » Thu Oct 02, 2014 6:02 am

In C# you could do is something like this

Code: Select all

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace webapi
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            using (var handler = new HttpClientHandler())
            {
                handler.Credentials = new NetworkCredential("admin@yourstore.com", "password");

                using (var client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri("https://xyz.tld");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // HTTP GET
                    HttpResponseMessage response = await client.GetAsync("api/adminproducts/1");
                    if (response.IsSuccessStatusCode)
                    {
                        Product product = await response.Content.ReadAsAsync<Product>();
                        Console.WriteLine("{0}\t${1}\t{2}", product.Id, product.Name, product.Price);
                    }

                    // HTTP POST
                    var gizmo = new Product() { Name = "Gizmo", Price = 100 };
                    response = await client.PostAsJsonAsync("api/adminproducts/postproduct", gizmo);
                    if (response.IsSuccessStatusCode)
                    {
                        Uri gizmoUrl = response.Headers.Location;

                        // HTTP PUT
                        gizmo.Price = 80;   // Update price
                        response = await client.PutAsJsonAsync(gizmoUrl, gizmo);

                        // HTTP DELETE
                        response = await client.DeleteAsync(gizmoUrl);
                    }
                }
            }
        }
    }

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public decimal CostOfGoods { get; set; }
        public decimal MSRP { get; set; }
        public decimal Weight { get; set; }
        public decimal Length { get; set; }
        public decimal Width { get; set; }
        public decimal Height { get; set; }
        public string Manufacturer { get; set; }
        public string Sku { get; set; }
        public string ModelNumber { get; set; }
        public string TaxCode { get; set; }
        public string Warehouse { get; set; }
        public int InStock { get; set; }
        public int InStockWarningLevel { get; set; }
        public InventoryModeType InventoryMode { get; set; }
        public string ThumbnailUrl { get; set; }
        public string ThumbnailAltText { get; set; }
        public string ImageUrl { get; set; }
        public string ImageAltText { get; set; }
        public string Summary { get; set; }
        public string Description { get; set; }
        public string ExtendedDescription { get; set; }
        public string Vendor { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime LastModifiedDate { get; set; }
        public bool IsFeatured { get; set; }
        public bool IsProhibited { get; set; }
        public bool AllowReviews { get; set; }
        public bool AllowBackorder { get; set; }
        public bool ExcludeFromFeed { get; set; }
        public bool DisablePurchase { get; set; }
        public int MinQuantity { get; set; }
        public int MaxQuantity { get; set; }
        public bool IsGiftCertificate { get; set; }
        public string WrapGroup { get; set; }
        public VisibilityType Visibility { get; set; }
        public ShippableType Shippable { get; set; }
    }

    public enum InventoryModeType
    {
        /// <summary>
        /// Inventory is not enabled
        /// </summary>
        None,

        /// <summary>
        /// Inventory is managed for the product itself
        /// </summary>
        Product,

        /// <summary>
        /// Inventory is managed for the product variants
        /// </summary>
        Variant
    }

    public enum ShippableType
    {
        /// <summary>
        /// The product is not shippable
        /// </summary>
        No,

        /// <summary>
        /// The product is shippable (separately or with other products)
        /// </summary>
        Yes,

        /// <summary>
        /// The product is only shippable separately
        /// </summary>
        Separately
    }

    public enum VisibilityType
    {
        /// <summary>
        /// CatalogNode is publicly visible. 
        /// </summary>
        Public,

        /// <summary>
        /// CatalogNode is hidden. Can be accessed via direct link.
        /// </summary>
        Hidden,

        /// <summary>
        /// CatalogNode is private. Can not be accessed.
        /// </summary>
        Private
    }
}

As of this point WEB API is very simple only few objects are exposed via it. If you are looking to do something complex you should better create your own webservice within ablecommerce with your custom logic. In that web service you can utilize ablecommerce API to take care of ablecommerce entities. Finally you can consume it from your client application.

User avatar
ForumsAdmin
AbleCommerce Moderator
AbleCommerce Moderator
Posts: 399
Joined: Wed Mar 13, 2013 7:19 am

Re: Calling web API from a stand alone asp.net application

Post by ForumsAdmin » Thu Oct 02, 2014 6:27 am

When it comes to adding, updating and deleting of products via RESTful API there could be too many complicated scenarios to take care of. If you just want to add or delete products in the simplest form you may make use of the existing web-api provided. However anything fancy you may be thinking of should be tackled via custom APIs.

Post Reply