0

On the Magento 2, I want to custom return object on my custom rest API, I have code like this on the Hz\Clients\Api\TransactionInterface.php

<?php

namespace Hz\Clients\Api;

interface TransactionInterface 
{

    /**
     * Get List Data by Client Id
     *
     * @param string $clientId
     * 
     * @return \Hz\Clients\Api\Data\FirstOptionDataInterface|\Hz\Clients\Api\Data\SecondOptionDataInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\InputException
     */
    public function getTransaction($clientId);
}

here is my FirstOptionDataInterface.php :

<?php

namespace Hz\Clients\Api\Data;

interface FirstOptionDataInterface 
{
    const INFO_MESSAGE = "info_message";

    /**
     * @param mixed $infoMessage
     * @return $this
     */
    public function setInfoMessage($infoMessage);

    /**
     * @return mixed
     */
    public function getInfoMessage();

}

and this is for SecondOptionDataInterface.php :

<?php

namespace Hz\Clients\Api\Data;

interface SecondOptionDataInterface 
{
    const TRANSACTIONS = "transactions";

    /**
     * @param mixed $transactions
     * @return $this
     */
    public function setTransactions($transactions);

    /**
     * @return mixed
     */
    public function getTransactions();

}

so when on the rest API method I select the first option then it will return \Hz\Clients\Api\Data\FirstOptionDataInterface object and if I select the second option then it will return \Hz\Clients\Api\Data\SecondOptionDataInterface, is it possible to do that in rest API Magento 2?

note: I cannot use throw exception for it since the format of throw exception output is different from my requirement.

7
  • you shouldn't do this. is it possible for you to create a "parent-interface"? or are both interfaces too different? Oct 24, 2019 at 8:00
  • hi @PhilippSander, yes both interface is too different that why I must separate them, for "parent-interface" could you give me an example ? I'm sorry my oop still bad.
    – mrfizh
    Oct 24, 2019 at 8:05
  • why don't you inject it to the constructor?
    – NID
    Oct 24, 2019 at 10:40
  • then the question: why can you return both? sounds like it should be split in 2 endpoints. can you provide us both interfaces? Oct 24, 2019 at 11:16
  • hi @PhilippSander okay I already edit my question, actually I want to integrate with other system and they ask me for provide 2 different format outputs when hits the endpoint url. do you think is possible to be like this on magento 2 ?
    – mrfizh
    Oct 25, 2019 at 8:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.