abhiburk
abhiburk

Reputation: 2320

Pagination in Gmail API (Previous Token)

I have implemented GMail API which gets Emails for me. Now I am trying to add pagination to it I have succeed in getting next records but now I also want to have Previous option(which required previous token).

I am unable to get into it below is what I tried so far.

public function paginate(Request $request){
        $label =  $request->input("label");
        $nextToken =  $request->input("next");
        $prevToken =  $request->input("prev");
        $messages = LaravelGmail::message();
        $msg = $messages->take(3)->in($label)->all($nextToken);
        $nextToken_New = $messages->pageToken;

        return view('gmail.load_mails', ['messages' => $msg, 'nextPageToken' => $nextToken_New,
        'prevPageToken' => $nextToken]);
}

Now In the above function nextPageToken is passed in view as $nextToken_New and for prevPageToken I am unable to set previous page token.(In code I have set last nextPageToken to prevPageToken which is not working)

Remember prevPageToken will be used to set on back key.

Upvotes: 2

Views: 1759

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117166

The Gmail api does not support prevous page token. Its not going to return the value to you.

Your first option would be to save these tokens on your server and then when ever you want to go back a page simply supply the token you want to the page token field

The second option and the one that i personally feel would be the most logical. Would be to cache the data returned by these requests on your server so that

  1. you dont have to make extra http calls to the server.
  2. you are not eating addictal quota making a call you have already made before.

APIs were not meant for you to use to implement pagination in your application. You should only be requesting data once its your job to then cache that data so that you wont need to make the same request twice.

Upvotes: 3

Related Questions