Git Product home page Git Product logo

Comments (5)

avenjamin avatar avenjamin commented on June 10, 2024

FYI - I have since tested extracting the $skiptoken parameter using the Spatie URL package and that worked.

However I'd still like to know if it's possible to change the full URL in the $request?

protected function getNextCursor(Response $response): int|string
{
    $url = Url::fromString($response->json('@odata.nextLink'));
    return $url->getQueryParameter('$skiptoken'); // <-- might need to check both $skip and $skiptoken
}

//...

protected function applyPagination(Request $request): Request
{
    if ($this->currentResponse instanceof Response) {
        $request->query()->add('$skiptoken', $this->getNextCursor($this->currentResponse)); // <-- might need to use $skip instead $skiptoken in some cases
    }

    if (isset($this->perPageLimit)) {
        $request->query()->add('$top', $this->perPageLimit);
    }

    return $request;
}

from saloon.

Sammyjo20 avatar Sammyjo20 commented on June 10, 2024

Hey @avenjamin firstly I want to apologise for being slow to reply to your message.

I'm going to see if there's a way I can introduce the ability to overwrite the URL on a per-request basis for situations like these. However I think you are on the right track with modifying the query parameters.

Do you have an example of how the @odata.nextLink value?

from saloon.

avenjamin avatar avenjamin commented on June 10, 2024

Hey @Sammyjo20 - no worries :)

That would be great if you could - I'm sure this won't be the only time someone will want this feature.

The original URL is:

https://graph.microsoft.com/v1.0/users?$select=id,mail,accountEnabled,displayName,department

And the @odata.nextLink URL is:

https://graph.microsoft.com/v1.0/users?$select=id%2cmail%2caccountEnabled%2cdisplayName%2cdepartment&$skiptoken=RFNwdAIAAQA....snip....GG5AAAAA

As you can see the URL is practically the same just with some URL escaping and the addition of the $skiptoken.

I'm only hesitant due to what Microsoft say in the docs which I'm reading as don't modify the URL basically and that in some instances they use $skip instead.

I suppose Microsoft could potentially set the $skiptoken to a different server hostname for load balancing. e.g. graph-na-01.microsoft.com but I haven't seen them do this yet:

Important
You should include the entire URL in the @odata.nextLink property in your request for the next page of results. Depending on the API that the query is being performed against, the @odata.nextLink URL value will contain either a $skiptoken or a $skip query parameter. The URL also contains all the other query parameters present in the original request. Do not try to extract the $skiptoken or $skip value and use it in a different request.

from saloon.

Sammyjo20 avatar Sammyjo20 commented on June 10, 2024

Hey @avenjamin

Thanks for the information - I've had a look into building a new url() API for Saloon's requests/connectors but I think it's going to be too difficult to retrofit it into v3 for everyone. I would recommend that you add your own setEndpoint() method on your request, which internally sets a property. When the endpoint is a full URL it'll be preferred over the base URL defined on the connector.

class YourRequest extends Request
{
     protected string $endpoint = '/default';

     protected function resolveEndpoint()
     {
         return $this->endpoint;
     }

     public function setEndpoint(string $endpoint): void
     {
         $this->endpoint = $endpoint;
     }
}


// ...

protected function applyPagination(Request $request): Request
{
    if ($this->currentResponse instanceof Response) {
        $request->setEndpoint($this->currentResponse->json('@odata.nextLink'));
    }

    // ...
}

from saloon.

avenjamin avatar avenjamin commented on June 10, 2024

Thanks @Sammyjo20 - maybe in v4 :)

I did as you suggested and it's working well so far.
I also just used the same technique for another API which has the page number as part of the URL (not a query parameter).

We can close this for now.

Cheers
Ben

from saloon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.