Ah sheet, here we go again!
A minimalistic wrapper for google api services which implements some common intermediate-level functionality which may be useful for wide range of projects.
In your Package.swift
update the list of global dependencies which is written using the property dependencies
. It must contain the following entry:
.package(url: "https://github.com/zeionara/ahsheet.git", .branch("oauth"))
In the same file include the dependency into the local list of dependencies corresponding to your app configuration (for instance, the path to this property may look like targets.executableTarget.dependencies
). As a result of modification, this list have to include at least this entry:
.product(name: "ahsheet", package: "ahsheet")
Api wrappers from this category do not require creation of a wrapper instance and are instead purely based on authorization keys provided via environment variables. Hence, this approach is the most lightweight, but at the same time it requires a slighltly more complicated setup.
For being able to work with a specific google spreadsheet ensure you've done the following things:
- Open access to the spreadsheet for anyone with the link;
- Set the
spreadsheet id
to be the value of theAH_SHEET_ID
environment variable; - Generate an
api key
in the google console with access to the sheets api; - Set the obtained
api key
to be the value of theAH_SHEET_API_KEY
environment variable.
For being able to not only read data from a spreadsheet, but to update as well the following additional steps are required:
- Open the official web-page for testing sheets api, send a couple of queries, open developer's console and save following values from the queries (these steps are necessary because for an unclear reason google sheets api doesn't support write operations using only api key - in any case it requires OAuth 2.0 token, even if a spreadsheet is freely available for modification):
- value of the
key
attribute from the inline url arguments put into the environment variableAH_SHEET_WRITE_API_KEY
; - value of the
Authorization
header put into the environment variableAH_SHEET_TOKEN
.
- value of the
Example of making a request for obtaining spreadsheet metadata using this interface:
print("Sheets:")
for sheet in try getSphreadsheetMeta().sheets {
print(sheet)
}
Expected Output:
Sheets:
foo aka 0
bar aka 291892989
baz aka 210100302
Api wrappers of this kind are the easiest in the question of setup, but require creation of a shared wrapper instance which may consume additional memory and complicate the source code in general.
To provide the service with all required data you need to ensure that the following measures are taken:
- The
spreadsheet id
is written to theAH_SHEET_ID
environment variable; - There is an OAuth 2.0 client id created in the google platform console's credentials section;
- The mentioned client id is provided with all required privilidges to
read / modify / manage
respective resources in the google cloud; - The appropriate
json
file containing crendentials which correspond to the client id is downloaded and put somewhere on your device, it's location is written to the environment variableAH_SHEET_CREDS_PATH
.
That's it! During program execution you will be asked to log into your google account and all the required tokens will be generated and refreshed internally.
Example of making a request for obtaining spreadsheet metadata using this interface:
let wrapper = try GoogleApiSessionWrapper()
print("Sheets:")
for sheet in try wrapper.getSpreadsheetMeta().sheets {
print(sheet)
}
Expected output is equivalent to the presented earlier:
Sheets:
foo aka 0
bar aka 291892989
baz aka 210100302