     typealias DraftsGetQuery = GTLRGmailQuery_UsersDraftsGet
     typealias DraftsListQuery = GTLRGmailQuery_UsersDraftsList
     typealias DraftsListResponse = GTLRGmail_ListDraftsResponse
+    typealias GmailCollection = GTLRCollectionObject
 
     fileprivate func createRequest(observer: AnyObserver<Any?>, query: Query) -> ServiceTicket {
         let serviceTicket = service.executeQuery(query) { ticket, object, error in

import GoogleAPIClientForREST
import RxSwift
import RxSwiftExt

class RxGmail {
    let GoogleClientID: String


    // 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<MessageResponse> {
        let query = MessageQuery.query(withUserId: userId)
        return getMessages(query: query)
    }

    // TODO: Refactor to use extensions on the collection and query, then constraint the parameters to these protocols.
    func loadPages<C: PagedResult, Q: PagedQuery> (query: Q) -> Observable<C> {
        func getRemainingPages(after previousPage: C?) -> Observable<C> {
            let nextPageToken = previousPage?.nextPageToken
            if previousPage != nil && nextPageToken == nil {
                return .empty()
            }
            let query = query.copy() as! Q
            query.pageToken = nextPageToken
            return execute(query: query)
                .map { $0! as! C }
                .flatMap { page -> Observable<C> in
                    let first = Observable.just(page)
                    let rest = getRemainingPages(after: page)
                    return first.concat(rest)
                }
        }
        return getRemainingPages(after: nil)
    }

    // TODO: Replace getMessages with this generic implementation.
    func getMessages2(query: MessageQuery) -> Observable<MessageResponse> {
        return loadPages(query: query)
    }

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