Pagination doesn't work with API Version 2.0 Calls

Issue #76 invalid
Ayman Reda Bedair created an issue

The pagination breaks on the call of next / previous pages as the value bitbucket returns for those contains the full API endpoint including the version and the default version of the HTTP Client is still set to '1.0'.

References:

  • \Bitbucket\API\Http\Response\Pager::fetchNext()
  • \Bitbucket\API\Http\Response\Pager::fetchPrevious()

Pagination calls were fixed when I changed the default api_version of the \Bitbucket\API\Http\Client::$options to 2.0 instead of 1.0.

I am not sure if this will break the few old version 1.0 API calls, I am not using any of those in my project

On the other hand, if the full endpoint is provided we can bypass the http client own version prefixing mechanism and it would then work for both versions.

Comments (9)

  1. Ayman Reda Bedair reporter

    Well I would then need a public repository with more than 10 Active pull requests:

    $page = new Pager(
        $this->getPullRequests()->getClient()->setApiVersion('2.0'),
        $this->getPullRequests()->all('gentlero','bitbucket-api')
    );
    
    $response = $page->fetchAll();
    

    The fetch all finds the first 10 results and as soon as it attempts the next page call you will get:

    Resource not found. There is no API hosted at this URL.

  2. Alexandru Guzinschi

    Given a quick test with PullRequests, seems to work fine:

    <?php
    $pr = new \Bitbucket\API\Repositories\PullRequests();
    $pr->getClient()->addListener(
        new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
    );
    
    $page = new \Bitbucket\API\Http\Response\Pager($pr->getClient(), $pr->all('gentlero', 'bitbucket-api', array('state' => 'MERGED', 'page' => 2)));
    
    $response = $page->getCurrent();
    echo $pr->getClient()->getLastRequest()->getResource().PHP_EOL;
    
    $response = $page->fetchNext();
    echo $pr->getClient()->getLastRequest()->getResource().PHP_EOL;
    
    $response = $page->fetchPrevious();
    echo $pr->getClient()->getLastRequest()->getResource().PHP_EOL;
    
    /** output:
     * /2.0/repositories/gentlero/bitbucket-api/pullrequests?state=MERGED&page=2
     * /2.0/repositories/gentlero/bitbucket-api/pullrequests?state=MERGED&page=3
     * /2.0/repositories/gentlero/bitbucket-api/pullrequests?state=MERGED&page=2
     */
    

    and

    <?php
    $pr = new \Bitbucket\API\Repositories\PullRequests();
    $pr->getClient()->addListener(
        new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
    );
    
    $page = new \Bitbucket\API\Http\Response\Pager($pr->getClient(), $pr->all('gentlero', 'bitbucket-api', array('state' => 'MERGED')));
    
    $data = json_decode($page->fetchAll()->getContent(), true);
    echo count($data['values']).PHP_EOL;
    
    // output: 21
    

    Can you give it a test with one of the these code samples and see if anything changes ?

  3. Ayman Reda Bedair reporter

    This works, had to investigate a bit what was the difference and it turned out to be my fault.

    My method "getPullRequests()" used to return a new instantiated object of the "PullRequests" class every time I call it (cached property glitch) and this seems to be the only difference. So my code basically did something like (if I hat to break my method into repeated code:

    <?php
    $pr = new \Bitbucket\API\Repositories\PullRequests();
    $pr->getClient()->addListener(
        new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
    );
    
    $client  = $pr->getClient();
    
    $pr = new \Bitbucket\API\Repositories\PullRequests();
    $pr->getClient()->addListener(
        new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
    );
    
    $page = new \Bitbucket\API\Http\Response\Pager($client, $pr->all('gentlero', 'bitbucket-api', array('state' => 'MERGED')));
    
    $data = json_decode($page->fetchAll()->getContent(), true);
    echo count($data['values']).PHP_EOL;
    

    Since the client version setting in the "all()" method of the Pull requests didn't affect the HTTP Client already injected to the pager I got this inconsistency and had to force 2.0 to my injected http client too.

    Sorry for this invalid report and your time. I appreciate you quick response and efforts.

  4. Log in to comment