1

I have created a new web API module, this API should execute a direct SQL query.

This is the code:

namespace Mymodule\ProductByEbayItemid\Model\ProductByEbayItemidQuery;

class ProductByEbayItemidQuery {
    protected $_resourceConnection;
    protected $_connection;
    public function __construct(
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->_resourceConnection = $resourceConnection;
    }
    public function getProductid($ebayitemid)
    {   
        $this->_connection = $this->_resourceConnection->getConnection();
        //Your custom sql query
        $query = "SELECT product_id FROM etbrm2epro_ebay_item WHERE item_id=:item_id";
        $binds = array ( 'item_id' =>  $ebayitemid );
        $collection = $this->_connection->fetchOne( $query,$binds );
        return $collection;
    }
}

I don't know how to instance this class in the API class.

namespace EternalParquet\ProductByEbayItemid\Model;
use EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface;

class ProductByEbayItemid implements ProductByEbayItemidInterface
{
    /**
     * Returns product data by ebay item id,
     * if m2epro is not installed it return null.
     * @ebayitemid ebay item id
     * @api
     * @return product data.
     */
    public function get($ebayitemid) {
             //?????
    }
}

acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="EternalParquet_ProductByEbayItemid::productid" title="GetProductByEbayItemid" translate="title" sortOrder="110" />
            </resource>
        </resources>
    </acl>
</config>

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface"
                type="EternalParquet\ProductByEbayItemid\Model\ProductByEbayItemid" />
</config>

webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/productByEbayItemid/productid/:ebayitemid" method="GET">
        <service class="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" method="productid"/>
        <resources>
            <resource ref="EternalParquet_ProductByEbayItemid::productid"/>
        </resources>
    </route>
</routes>
1
  • I changed get method to productid – blob Nov 22 '19 at 13:40
2

no , the paths are like for interface and model should be like :
app\code\EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface
and app\code\EternalParquet\ProductByEbayItemid\Model\ProductByEbayItemidQuery.
you should specify parameters before the function

/**
     * Returns greeting message to user
     *
     * @api
     * @param mixed $ebayitemid ID.
     * @return string Greeting message with users name.
     */
1
  • I get always this error message. – blob Nov 22 '19 at 13:33
0

Call the interface and model in the webapi.xml in the below format

<route url="/V1/account/getproductid" method="GET">
        <service class="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" method="getProductid"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>

and in di.xml

 <preference for="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" type="Mymodule\ProductByEbayItemid\Model\ProductByEbayItemidQuery" />

the method and parameters should be same in API interface and model

2
  • These classes are in the same directory – blob Nov 22 '19 at 10:36
  • I get this error: AH01071: Got error 'PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\\Framework\\Reflection\\TypeProcessor::resolveFullyQualifiedClassName() must be of the type string, null given, called in /var/www/vhosts/domain.com/httpdocs/vendor/magento/framework/Reflection/TypeProcessor.php on line 535 and defined in /var/www/vhosts/domain.com/httpdocs/vendor/magento/framework/Reflection/TypeProcessor.php:640\nStack trace:\n#0 ...... – blob Nov 22 '19 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.