cdaguerre/php-trello-api

Too many api calls when trying to get cards moved from a list to another

Opened this issue · 3 comments

Hi,

Here is a use case I needed to implement for generating BurnDown Chart using Trello actions.

Here is my algorithme :

  1. Get board
  2. Get lists "from and "to"
  3. Get all cards in the "to" list.
  4. Foreach cards of this list, get all actions of the card.
  5. Foreach action, check the type of action (update) and check from wich list and to wich list the card has been moved (in order to check if the card is coming from my "from" list)
  6. If the card is coming from the "from" list, get the name and preg_match ([0-9]*)

The global goal is to find all cards which have been moved from a list 1 to a list 2 during an interval of 2 dates.

Unfortunately, it seems that Trello API does not provide a web service to get all the moved card from or to a selected list.

Anyway, with your php-trello-api wrapper, it seems that the refresh method is called each time an object is instantiated... resulting an API call for each instantiation...

In my example it result 1865 API Calls...

Here is the Blackfire profile : https://blackfire.io/profiles/dff19c00-03a0-41b8-99f7-d36dcf542f3e/graph?settings%5Bdimension%5D=wt&settings%5Bdisplay%5D=landscape&settings%5BtabPane%5D=nodes&selected=&callname=main()

Hi @seblegall
As a quick solution: You could extend the models (Card, for instance) and override the __construct method to not refresh every object on load, but you would have to make sure you get all the data you need later on, ie. by manually calling $card->refresh() if you need..

namespace Your\Namespace\Model;

use Trello\Model\Card as BaseCard;
use Trello\ClientInterface;

class Card extends BaseCard
{
    /**
     * {@inheritdoc}
     */
    public function __construct(ClientInterface $client, $id = null)
    {
        $this->client = $client;
        $this->api    = $client->api($this->apiName);

        $this->fields = $this->api->getFields();

        if ($id) {
            $this->id = $id;
            // Comment the line below to avoid unnecessary api calls
            // $this->refresh();
        }
    }
}

You'd also have to extend the Manager to use your custom classes and use your own Manager.

namespace Your\Namespace;

use Trello\Manager as BaseManager;
use Your\Namespace\Model\Card;

class Manager extends BaseManager
{
    /**
     * {@inheritdoc}
     */
    public function getCard($id = null)
    {
        return new Card($this->client, $id);
    }
}

Hope that helps.
If you want, you could make a PR to make automatic refreshing configurable through the manager!

@seblegall By the way, having a look at your blackfire profile it seems you would need to override Card, Cardlist and Action...

@cdaguerre Yes, I will try that. Another way to improve api calls I implemented is to call the api directly.

Instead of

$lists = $board->getLists()

I use :

$lists = $this->client->api('board')->lists()->all($board->getId());

Then...

new Cardlist($this->client, $list['id']);