                 }
         }
         return getRemainingMessages(after: nil)
-            .map { $0.messages }
-            .unwrap()
     }
 
     func getProfile(forUserId userId: String = "me") -> Observable<Profile> {


    // Each event returns a page of messages.
    // TODO: Generalize to arbitrary query. Need to copy queries each time they are executed.
    func getMessages(forUserId userId: String = "me") -> Observable<[Message]> {
        let query = MessageQuery.query(withUserId: userId)
        return getMessages(query: query)
    }

    func getMessages(query: MessageQuery) -> Observable<[Message]> {
        func getRemainingMessages(after previousPage: MessageResponse?) -> Observable<MessageResponse> {
            let nextPageToken = previousPage?.nextPageToken
            if previousPage != nil && nextPageToken == nil {

import GoogleAPIClientForREST
import RxSwift
import RxSwiftExt

// Protocols used to retroactively model all paged query types in the Gmail API.
protocol PagedQuery: NSCopying, RxGmail.Query {
    var pageToken: String? { get set }
}

protocol PagedResult {
    var nextPageToken: String? { get }
}

extension RxGmail.MessageResponse: PagedResult { }
extension RxGmail.MessageQuery: PagedQuery { }

class RxGmail {
    let GoogleClientID: String
