google / google-api-objectivec-client-for-rest

Google APIs Client Library for Objective-C for REST
Apache License 2.0
776 stars 309 forks source link

Determining type of executeQuery() result object #79

Closed prat0088 closed 7 years ago

prat0088 commented 7 years ago

I have the following code that queries metadata about a spreadsheet

  func querySheets(workbookId:String, completed:@escaping ([QueriedSheet]?, Error?) -> Void) {
        let query = GTLRSheetsQuery_SpreadsheetsGet.query(withSpreadsheetId:workbookId)
        sheetsService.executeQuery(query) { ticket, result, error in
            if let sheet = result as? GTLRObject {
                print("GTLRObject")
            }
            else {
                print("Not GTLRObject")
            }
            if let sheet = result as? GTLRSheets_Sheet {
                print("GTLRSheets_Sheet")
            }
            else {
                print("Not GTLRSheets_Sheet")
            }
        }
    }

The output is

GTLRObject
Not GTLRSheets_Sheet

error is nil.

How can I determine the object type of "result" for a given query? I didn't see any reference or instruction in the documentation. I Just want to get the name of the document and names and IDs of any sheets it contains.

I was calling the Google Sheets v4 REST API directly. Although it takes a bit more code, the API is very well documented and I never got stuck. I'm attempting to use this wrapper because it would mean less code once I figure out how to use it.

prat0088 commented 7 years ago

My problem was that I was using GTLRSheets_Sheet instead of GTLRSheets_Spreadsheet. It makes sense as that is the prefix of the query.

thomasvl commented 7 years ago

The generated docs usually include what the result object should be; so for GTLRSheetsQuery_SpreadsheetsGet, https://github.com/google/google-api-objectivec-client-for-rest/blob/master/Source/GeneratedServices/Sheets/GTLRSheetsQuery.h#L242-L266 says it should be GTLRSheets_Spreadsheet.

Since ObjC does't have nested naming like Swift, the names end up having to be merged chunks into a single name. So the service you are talking to is Sheets (hence the GTLRSheetsQuery name on the front), the actually query is SpreadsheetsGet, which is sorta the "hint" that the result is a Spreadsheet object of some kind. The generated docs will have the info when the discovery document provides the details.

thomasvl commented 7 years ago

PS - in they link, the first part of the comment:

Fetches a @c GTLRSheets_Spreadsheet.

Is what calls out what will come back from executeQuery.