Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

OfficialFeedManager.cs

using System.Xml;
using Nop.Core;
using Nop.Services.Common;
using Nop.Services.Logging;

namespace Nop.Services.Plugins.Marketplace;

/// 
/// Represents the official feed manager (plugins from nopcommerce marketplace)
/// 
public partial class OfficialFeedManager
{
    #region Fields

    protected readonly ILogger _logger;
    protected readonly NopHttpClient _nopHttpClient;

    #endregion

    #region Ctor

    public OfficialFeedManager(ILogger logger,
        NopHttpClient nopHttpClient)
    {
        _logger = logger;
        _nopHttpClient = nopHttpClient;
    }

    #endregion

    #region Utilities

    /// 
    /// Get element value
    /// 
    /// XML node
    /// Element name
    /// Value (text)
    protected virtual string GetElementValue(XmlNode node, string elementName)
    {
        return node?.SelectSingleNode(elementName)?.InnerText;
    }

    #endregion

    #region Methods

    /// 
    /// Get available categories of marketplace extensions
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the result
    /// 
    public virtual async Task> GetCategoriesAsync()
    {
        //load XML
        var xml = new XmlDocument();
        try
        {
            xml.LoadXml(await _nopHttpClient.GetExtensionsCategoriesAsync());
        }
        catch (Exception ex)
        {
            await _logger.ErrorAsync("No access to the list of plugins. Website www.nopcommerce.com is not available.", ex);
        }

        //get list of categories from the XML
        return xml.SelectNodes(@"//categories/category").Cast().Select(node => new OfficialFeedCategory
        {
            Id = int.Parse(GetElementValue(node, @"id")),
            ParentCategoryId = int.Parse(GetElementValue(node, @"parentCategoryId")),
            Name = GetElementValue(node, @"name")
        }).ToList();
    }

    /// 
    /// Get available versions of marketplace extensions
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the result
    /// 
    public virtual async Task> GetVersionsAsync()
    {
        //load XML
        var xml = new XmlDocument();
        try
        {
            xml.LoadXml(await _nopHttpClient.GetExtensionsVersionsAsync());
        }
        catch (Exception ex)
        {
            await _logger.ErrorAsync("No access to the list of plugins. Website www.nopcommerce.com is not available.", ex);
        }

        //get list of versions from the XML
        return xml.SelectNodes(@"//versions/version").Cast().Select(node => new OfficialFeedVersion
        {
            Id = int.Parse(GetElementValue(node, @"id")),
            Name = GetElementValue(node, @"name")
        }).ToList();
    }

    /// 
    /// Get all plugins
    /// 
    /// Category identifier
    /// Version identifier
    /// Price; 0 - all, 10 - free, 20 - paid
    /// Search term
    /// Page index
    /// Page size
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the plugins
    /// 
    public virtual async Task> GetAllPluginsAsync(int categoryId = 0,
        int versionId = 0, int price = 0, string searchTerm = "",
        int pageIndex = 0, int pageSize = int.MaxValue)
    {
        //load XML
        var xml = new XmlDocument();
        try
        {
            xml.LoadXml(await _nopHttpClient.GetExtensionsAsync(categoryId, versionId, price, searchTerm, pageIndex, pageSize));
        }
        catch (Exception ex)
        {
            await _logger.ErrorAsync("No access to the list of plugins. Website www.nopcommerce.com is not available.", ex);
        }

        //get list of extensions from the XML
        var list = xml.SelectNodes(@"//extensions/extension").Cast().Select(node => new OfficialFeedPlugin
        {
            Name = GetElementValue(node, @"name"),
            Url = GetElementValue(node, @"url"),
            PictureUrl = GetElementValue(node, @"picture"),
            Category = GetElementValue(node, @"category"),
            SupportedVersions = GetElementValue(node, @"versions"),
            Price = GetElementValue(node, @"price")
        }).ToList();

        _ = int.TryParse(GetElementValue(xml.SelectNodes(@"//totalRecords")?[0], @"value"), out var totalRecords);

        return new PagedList(list, pageIndex, pageSize, totalRecords);
    }

    #endregion
}