googledrive-easy 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e56a24359754fc58fb6a56cfc7197c4bd96834d8a4918e944385d2642ab900e
4
- data.tar.gz: 8963a1f491e20b479814a5da6495e5654f0627a80f4520627e5b2ad9357b319a
3
+ metadata.gz: bde25654cb7729aa6b698cb13142bea88ed013117cdf4af338e1c4fa65d96593
4
+ data.tar.gz: dd6e91d0cfb1a7f54fc265b8da75d043b3c2582aaf6b907e77289c4aade92d06
5
5
  SHA512:
6
- metadata.gz: b57117941614a74a66ad3f787d20e92356931e20d266bac57eb51bd07d29814af36014306bdf4c70e6076399c338ef15bc7d41d5317ea96b541e9cbc9b9a1e9a
7
- data.tar.gz: f058b48cbce3e482b682676150ff6f51c76a0dd35339b0bce16dc51abfdc45b14a11578938eb0458c36b3f7d74fcc1a8e5790b7604482d93af8be3af38eafbc9
6
+ metadata.gz: 7eca3c50d329df741fece7ec1ddc0188bd938f9e1826374e887192ae405741516e91d794c86d6bd15e68135bda8fbc1c131d6a3e4b28d68e1d2237894fc5b8e6
7
+ data.tar.gz: e02255813a9a3e15bdf013eaeed3e71b42767fce79af7c44eda1ecba4ed304bcc5d59e015be2959597b1256dd899676aeec2959134c88c8fdeaeb2e6c681317d
data/bin/googledrive CHANGED
@@ -46,7 +46,7 @@ def print_hash_for_env_use(keys)
46
46
  end
47
47
 
48
48
  def handle_auth(options)
49
- drive = GoogleDrive::new(raise_error: false, loglevel: Logger::ERROR)
49
+ drive = GoogleDrive::new(raise_error: true, loglevel: Logger::ERROR)
50
50
 
51
51
  # Handle special service mode cases
52
52
  case options[:authmode]
@@ -57,11 +57,16 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
57
57
  def test_loading_api_vars_from_env_success
58
58
  configure_environment
59
59
 
60
- result = @drive.load_api_keys_from_env
61
- assert(result, "load_api_keys_from_env should return true")
62
- assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
63
- assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
64
- assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
60
+ 2.times do |i|
61
+ result = @drive.load_api_keys_from_env
62
+ assert(result, "load_api_keys_from_env should return true")
63
+ assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
64
+ assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
65
+ assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
66
+
67
+ # Enable raise error for second run.
68
+ @drive.set_raise_error
69
+ end
65
70
  end
66
71
 
67
72
  def test_loading_api_vars_from_env_missing_fail
@@ -74,6 +79,10 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
74
79
  assert_equal(nil, @drive.instance_variable_get(:@client_id), "Client ID should not be loaded")
75
80
  assert_equal(nil, @drive.instance_variable_get(:@client_secret), "Client Secret should not be loaded")
76
81
  assert_equal(nil, @drive.instance_variable_get(:@refresh_token), "Refresh Token should not be loaded")
82
+
83
+ @drive.set_raise_error
84
+ exception = assert_raise(RuntimeError) { @drive.load_api_keys_from_env }
85
+ assert_equal("DRIVEAPI_CLIENT_SECRET export variable not set.", exception.message)
77
86
  end
78
87
 
79
88
  def test_loading_api_vars_from_file_success
@@ -82,13 +91,16 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
82
91
 
83
92
  # Override default key file
84
93
  # @drive.instance_variable_set(:@api_key_file, @env_file.path)
85
-
86
- # Attempt load
87
- result = @drive.load_api_keys_from_file(path:@env_file.path)
88
- assert(result, "load_api_keys_from_file should return true")
89
- assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
90
- assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
91
- assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
94
+ 2.times do |i|
95
+ # Attempt load
96
+ result = @drive.load_api_keys_from_file(path:@env_file.path)
97
+ assert(result, "load_api_keys_from_file should return true")
98
+ assert_equal(@client_id, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
99
+ assert_equal(@client_secret, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
100
+ assert_equal(@refresh_token, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
101
+ # Enable raise error for second run.
102
+ @drive.set_raise_error
103
+ end
92
104
  end
93
105
 
94
106
  def test_loading_api_vars_from_file_not_found
@@ -101,6 +113,10 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
101
113
  assert_equal(nil, @drive.instance_variable_get(:@client_id), "Client ID should be loaded")
102
114
  assert_equal(nil, @drive.instance_variable_get(:@client_secret), "Client Secret should be loaded")
103
115
  assert_equal(nil, @drive.instance_variable_get(:@refresh_token), "Refresh Token should be loaded")
116
+
117
+ @drive.set_raise_error
118
+ exception = assert_raise(RuntimeError) { @drive.load_api_keys_from_file }
119
+ assert_equal("Cannot find /tmp/bogons", exception.message)
104
120
  end
105
121
 
106
122
  ###########################
@@ -111,9 +127,12 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
111
127
  @drive.instance_variable_set(:@client_id, @client_id)
112
128
  @drive.instance_variable_set(:@client_secret, @client_secret)
113
129
 
114
- api_hash = @drive.generate_api_secret_hash
115
- assert_equal(@client_id, api_hash['installed']['client_id'], 'Hash should contain client id')
116
- assert_equal(@client_secret, api_hash['installed']['client_secret'], 'Hash should contain client secret')
130
+ 2.times do
131
+ api_hash = @drive.generate_api_secret_hash
132
+ assert_equal(@client_id, api_hash['installed']['client_id'], 'Hash should contain client id')
133
+ assert_equal(@client_secret, api_hash['installed']['client_secret'], 'Hash should contain client secret')
134
+ @drive.set_raise_error
135
+ end
117
136
  end
118
137
 
119
138
  def test_generate_token_yaml_success
@@ -123,11 +142,13 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
123
142
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
124
143
  token_file_user = @drive.instance_variable_get(:@token_file_user)
125
144
 
126
- yaml_string = @drive.generate_token_yaml
127
- assert(yaml_string.include?(token_file_user), "Result should contain token_file_user")
128
- assert(yaml_string.include?(@client_id), "Result should contain client_id")
129
- assert(yaml_string.include?(@refresh_token), "Result should contain refresh_token")
130
-
145
+ 2.times do
146
+ yaml_string = @drive.generate_token_yaml
147
+ assert(yaml_string.include?(token_file_user), "Result should contain token_file_user")
148
+ assert(yaml_string.include?(@client_id), "Result should contain client_id")
149
+ assert(yaml_string.include?(@refresh_token), "Result should contain refresh_token")
150
+ @drive.set_raise_error
151
+ end
131
152
  end
132
153
 
133
154
  def test_generate_token_yaml_fail
@@ -135,6 +156,9 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
135
156
  # @drive_wrapper.instance_variable_set(:@client_id, @client_id)
136
157
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
137
158
  assert(!@drive.generate_token_yaml, "Generation of token yaml should fail with partial config")
159
+
160
+ @drive.set_raise_error
161
+ assert_raise(RuntimeError) { @drive.generate_token_yaml }
138
162
  end
139
163
 
140
164
  def test_generate_temp_token_file
@@ -143,11 +167,14 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
143
167
  @drive.instance_variable_set(:@refresh_token, @refresh_token)
144
168
  token_file_user = @drive.instance_variable_get(:@token_file_user)
145
169
 
146
- tmp_file_path = @drive.generate_temp_token_file
147
- tmp_file_contents = File.read(tmp_file_path)
148
- assert(tmp_file_contents.include?(token_file_user), "Result should contain token_file_user")
149
- assert(tmp_file_contents.include?(@client_id), "Result should contain client_id")
150
- assert(tmp_file_contents.include?(@refresh_token), "Result should contain refresh_token")
170
+ 2.times do
171
+ tmp_file_path = @drive.generate_temp_token_file
172
+ tmp_file_contents = File.read(tmp_file_path)
173
+ assert(tmp_file_contents.include?(token_file_user), "Result should contain token_file_user")
174
+ assert(tmp_file_contents.include?(@client_id), "Result should contain client_id")
175
+ assert(tmp_file_contents.include?(@refresh_token), "Result should contain refresh_token")
176
+ @drive.set_raise_error
177
+ end
151
178
  end
152
179
 
153
180
  #########################################
@@ -162,34 +189,43 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
162
189
  # Use this, or just set the path directly.
163
190
  @drive.load_api_keys_from_file(path:@env_file.path)
164
191
 
165
- # Authorize
166
- result = @drive.create_service(mode: 'manual')
167
- assert(result, "Authorization from configured environment should succeed.")
168
- assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
169
- assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
192
+ 2.times do
193
+ # Authorize
194
+ result = @drive.create_service(mode: 'manual')
195
+ assert(result, "Authorization from configured environment should succeed.")
196
+ assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
197
+ assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
198
+ @drive.set_raise_error
199
+ end
170
200
  end
171
201
 
172
202
  def test_create_service_environment
173
203
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
174
204
  configure_environment
175
205
 
176
- # Authorize
177
- result = @drive.create_service(mode: 'environment')
178
- assert(result, "Authorization from configured environment should succeed.")
179
- assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
180
- puts @drive.instance_variable_get(:@credentials)
181
- assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
206
+ 2.times do
207
+ # Authorize
208
+ result = @drive.create_service(mode: 'environment')
209
+ assert(result, "Authorization from configured environment should succeed.")
210
+ assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
211
+ puts @drive.instance_variable_get(:@credentials)
212
+ assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
213
+ @drive.set_raise_error
214
+ end
182
215
  end
183
216
 
184
217
  def test_create_service_manual
185
218
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
186
219
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
187
220
 
188
- # Authorize
189
- result = @drive.create_service(mode: 'manual')
190
- assert(result, "Authorization from manual configuration should succeed.")
191
- assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
192
- assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
221
+ 2.times do
222
+ # Authorize
223
+ result = @drive.create_service(mode: 'manual')
224
+ assert(result, "Authorization from manual configuration should succeed.")
225
+ assert(@drive.instance_variable_get(:@credentials), "Credentials should be set")
226
+ assert_instance_of(Google::Apis::DriveV3::DriveService, result, "Should return instance of Google Drive Service")
227
+ @drive.set_raise_error
228
+ end
193
229
  end
194
230
 
195
231
  ###########################################
@@ -216,18 +252,65 @@ class GoogleDriveEasyTest < Test::Unit::TestCase
216
252
 
217
253
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
218
254
  @drive.create_service(mode:'manual')
219
- res = @drive.get_all_files(name: "rxgos*")
220
- assert_instance_of(Array, res, "Returns list")
255
+
256
+ 2.times do
257
+ res = @drive.get_all_files(name: "rxgos*")
258
+ assert_instance_of(Array, res, "Returns list")
259
+ @drive.set_raise_error
260
+ end
221
261
  end
222
262
 
223
263
  def test_get_file_info
224
264
  omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
225
265
  @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
226
266
  @drive.create_service(mode:'manual')
227
- res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQuRz")
228
- assert_instance_of(Hash, res, 'Should return a Hash')
229
- assert_equal("rxg", res[:name], "Name should be 'rxg'")
230
- # assert_instance_of(Google::Apis::DriveV3::File, res, 'Should be instance of Google::Apis::DriveV3::File')
267
+
268
+ 2.times do
269
+ res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQuRz")
270
+ assert_instance_of(Hash, res, 'Should return a Hash')
271
+ assert_equal("rxg", res[:name], "Name should be 'rxg'")
272
+ # assert_instance_of(Google::Apis::DriveV3::File, res, 'Should be instance of Google::Apis::DriveV3::File')
273
+ @drive.set_raise_error
274
+ end
231
275
  end
232
276
 
277
+ def test_get_file_info_nofile
278
+ omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
279
+ @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
280
+ @drive.create_service(mode:'manual')
281
+
282
+ res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQZZZ")
283
+ assert_false(res, "Result should be false")
284
+
285
+ # Doesn't raise on not-found
286
+ end
287
+
288
+ def test_find_directory_id
289
+ omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
290
+ @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
291
+ @drive.create_service(mode:'manual')
292
+
293
+ 2.times do
294
+ res = @drive.find_directory_id("rxg")
295
+ assert_equal("10l2ahCetnm1_147rRcuaObTwrntqQuRz", res)
296
+ # assert_instance_of(Google::Apis::DriveV3::File, res, 'Should be instance of Google::Apis::DriveV3::File')
297
+ res = @drive.get_file_info("10l2ahCetnm1_147rRcuaObTwrntqQuRz")
298
+ @drive.set_raise_error
299
+ end
300
+ end
301
+
302
+ def test_find_directory_id_nodir
303
+ omit("Test Environment not correctly configured for live tests") unless @test_env_vars_defined
304
+ @drive.set_api_keys(client_id: @client_id, client_secret: @client_secret, refresh_token: @refresh_token)
305
+ @drive.create_service(mode:'manual')
306
+
307
+ res = @drive.find_directory_id("nonexistantdirectory")
308
+ assert_false(res, "Result should be false")
309
+
310
+ @drive.set_raise_error
311
+ assert_raise(RuntimeError) { res = @drive.find_directory_id("nonexistantdirectory") }
312
+ # Doesn't raise on not-found
313
+ end
314
+
315
+
233
316
  end
@@ -85,16 +85,20 @@ class GoogleDrive
85
85
  TEXT
86
86
  end
87
87
 
88
- def log_error_and_raise(msg)
88
+ def set_raise_error(raise_error=true)
89
+ @raise_error = raise_error
90
+ end
91
+
92
+ def log_error_and_raise(msg, raise_error=@raise_error)
89
93
  @logger.error(msg)
90
- raise "msg" if @raise_error
94
+ raise "#{msg}" if raise_error
95
+ false
91
96
  end
92
97
 
93
- def set_api_keys(client_id:nil, client_secret:nil, refresh_token:nil, access_token:nil, require_refresh_token:true)
98
+ def set_api_keys(client_id:nil, client_secret:nil, refresh_token:nil, access_token:nil, require_refresh_token:true, raise_error: @raise_error )
94
99
  # Recommended service mode if manually called: manual
95
100
  unless client_id && client_secret && (refresh_token || !require_refresh_token)
96
- log_error_and_raise("Not all tokens provided.")
97
- return false
101
+ return log_error_and_raise("Not all tokens provided.", raise_error)
98
102
  end
99
103
  @client_id = client_id if client_id
100
104
  @client_secret = client_secret if client_secret
@@ -103,15 +107,14 @@ class GoogleDrive
103
107
  return true
104
108
  end
105
109
 
106
- def load_api_keys_from_file(path:nil, require_refresh_token:true)
110
+ def load_api_keys_from_file(path:nil, require_refresh_token:true, raise_error: @raise_error)
107
111
  @loaded_api_key_file = path ? path : @api_key_file
108
112
  if(File.exist?(@loaded_api_key_file))
109
113
  @logger.info("API key file #{@loaded_api_key_file} exists")
110
114
  begin
111
115
  api_hash = JSON.parse(File.read(@loaded_api_key_file))
112
116
  rescue => error
113
- log_error_and_raise("Error opening api key file: " + error.inspect)
114
- return false
117
+ return log_error_and_raise("Error opening api key file: " + error.inspect, raise_error)
115
118
  end
116
119
 
117
120
  @logger.debug("api_hash: " + api_hash.inspect)
@@ -124,38 +127,41 @@ class GoogleDrive
124
127
  client_id:api_hash["CLIENT_ID"],
125
128
  client_secret:api_hash["CLIENT_SECRET"],
126
129
  refresh_token:api_hash["REFRESH_TOKEN"],
127
- require_refresh_token:require_refresh_token
130
+ require_refresh_token:require_refresh_token,
131
+ raise_error: false
128
132
  )
129
133
  @logger.info("Using Google Drive API information from #{@loaded_api_key_file}")
130
134
  else
131
- log_error_and_raise("Not all API keys were in file #{@loaded_api_key_file}.")
132
- return false
135
+ return log_error_and_raise("Not all API keys were in file #{@loaded_api_key_file}.", raise_error)
133
136
  end
134
137
  else
135
- log_error_and_raise("Cannot find #{@loaded_api_key_file}")
136
- return false
138
+ return log_error_and_raise("Cannot find #{@loaded_api_key_file}", raise_error)
137
139
  end
138
140
  true
139
141
  end
140
142
 
141
- def load_api_keys_from_env(require_refresh_token:true)
143
+ def load_api_keys_from_env(require_refresh_token:true, raise_error: @raise_error)
142
144
  # Google Drive Credentials from ENV if not derived from JSON file in home.
143
145
  vars = [ENV_KEYS[:id], ENV_KEYS[:secret]]
144
146
  vars << ENV_KEYS[:refresh] if require_refresh_token
145
147
  vars.each do |v|
146
148
  if ENV[v].nil?
147
- log_error_and_raise("#{v} export variable not set.")
148
- return false
149
+ return log_error_and_raise("#{v} export variable not set.", raise_error)
149
150
  end
150
151
  end
151
152
 
152
153
  # Set
153
- return set_api_keys(
154
+ if set_api_keys(
154
155
  client_id:ENV[ENV_KEYS[:id]],
155
156
  client_secret:ENV[ENV_KEYS[:secret]],
156
157
  refresh_token:ENV[ENV_KEYS[:refresh]],
157
- require_refresh_token:require_refresh_token
158
+ require_refresh_token:require_refresh_token,
159
+ raise_error: raise_error
158
160
  )
161
+ @logger.info("Using Google Drive API information from environment.")
162
+ else
163
+ return log_error_and_raise("Not all API keys were in environment.", raise_error)
164
+ end
159
165
  end
160
166
 
161
167
  def generate_api_secret_hash
@@ -182,12 +188,11 @@ class GoogleDrive
182
188
  end
183
189
 
184
190
  # Generates token yaml
185
- def generate_token_yaml
191
+ def generate_token_yaml(raise_error: @raise_error)
186
192
  unless @client_id && @refresh_token
187
193
  api_config = {"client_id" => @client_id, "refresh_token" => @refresh_token }
188
194
  @logger.debug("API Config: #{api_config}")
189
- log_error_and_raise("Some required API config for token hasn't been configured yet")
190
- return false
195
+ return log_error_and_raise("Some required API config for token hasn't been configured yet", raise_error)
191
196
  end
192
197
 
193
198
  drive_token_hash = {
@@ -201,12 +206,10 @@ class GoogleDrive
201
206
  end
202
207
 
203
208
  # Generates temp token file and return path to it
204
- def generate_temp_token_file
209
+ def generate_temp_token_file(raise_error: @raise_error)
205
210
  tmpfile = Tempfile.new('drive_token')
206
- token_yaml = generate_token_yaml
207
- unless token_yaml
208
- return false
209
- end
211
+ token_yaml = generate_token_yaml(raise_error: raise_error)
212
+ return false unless token_yaml
210
213
  tmpfile.write(token_yaml)
211
214
  tmpfile.flush # flush file contents before continuing.
212
215
  return @drive_token_path = File.expand_path(tmpfile.path)
@@ -226,7 +229,6 @@ class GoogleDrive
226
229
  # from the machine running the authenticating browser to the machine running this script,
227
230
  # likely via `ssh user@host -L @oauth_port:@oauth_address:@oauth_port`
228
231
  def get_oauth_credentials_via_loopback(authorizer)
229
- # TODO: This a raw copy, refactor
230
232
  # Print instructions and URL for user to click
231
233
  # TODO: Should this be puts or @logger? it's a user interaction...
232
234
  puts "Listening on #{@oauth_address}:#{@oauth_port}."
@@ -270,7 +272,6 @@ class GoogleDrive
270
272
  end
271
273
 
272
274
  def get_oauth_credentials_via_input(authorizer)
273
- # TODO: This a raw copy, refactor
274
275
  puts 'Follow this url and complete the sign-in process. The login will result in an error, do not close it.'
275
276
  puts 'Instead, copy and paste the value of the `code` parameter (begins with 4/)'
276
277
  puts authorizer.get_authorization_url(base_url: "http://localhost:1")
@@ -289,8 +290,7 @@ class GoogleDrive
289
290
  @logger.debug("Passed authorization mode: #{mode}")
290
291
 
291
292
  unless %w(environment loopback input manual).include?(mode)
292
- log_error_and_raise("Unknown authorization mode")
293
- return false
293
+ return log_error_and_raise("Unknown authorization mode")
294
294
  end
295
295
 
296
296
  # TODO: Figure out balance between always requiring env config and just reading from the token store if it already exists
@@ -301,7 +301,10 @@ class GoogleDrive
301
301
  if mode == "manual"
302
302
  key_load_result = false
303
303
  else
304
- key_load_result = (load_api_keys_from_env(require_refresh_token:!interactive_mode) || load_api_keys_from_file(require_refresh_token:!interactive_mode))
304
+ key_load_result = (
305
+ load_api_keys_from_env(require_refresh_token:!interactive_mode, raise_error: false) ||
306
+ load_api_keys_from_file(require_refresh_token:!interactive_mode, raise_error: false)
307
+ )
305
308
  end
306
309
 
307
310
  if interactive_mode && !key_load_result
@@ -311,22 +314,19 @@ class GoogleDrive
311
314
  # If environment, we need key load to have succeeded completely
312
315
  # If manual, it should pull from memory.
313
316
  if %w(environment manual).include?(mode)
314
- tmp_token_path = generate_temp_token_file
317
+ tmp_token_path = generate_temp_token_file(raise_error: false)
315
318
  unless tmp_token_path
316
- log_error_and_raise("Failed to generate temporary token file")
317
- return false
319
+ return log_error_and_raise("Failed to generate temporary token file")
318
320
  end
319
321
  @logger.debug(File.read(tmp_token_path))
320
322
  unless key_load_result || mode=='manual'
321
- log_error_and_raise("Unable to load api keys from environment")
322
- return false
323
+ return log_error_and_raise("Unable to load api keys from environment")
323
324
  end
324
325
  token_store = Google::Auth::Stores::FileTokenStore.new(file: tmp_token_path)
325
326
  else
326
327
  # Otherwise, just the ID and secret are enough
327
328
  unless @client_secret && @client_id
328
- log_error_and_raise("Client Secret or ID missing.")
329
- return false
329
+ return log_error_and_raise("Client Secret or ID missing.")
330
330
  end
331
331
  token_store = Google::Auth::Stores::FileTokenStore.new(file: @drive_token_path)
332
332
  end
@@ -352,8 +352,7 @@ class GoogleDrive
352
352
 
353
353
  # Final cred check
354
354
  if @credentials.nil?
355
- @logger.error('Unable to retrieve credentials')
356
- return false
355
+ return log_error_and_raise(@logger.error('Unable to retrieve credentials') )
357
356
  end
358
357
 
359
358
  # Update internal credentials based on credentials loaded
@@ -380,11 +379,8 @@ class GoogleDrive
380
379
  # parentfolderid: "root" gets the root directory. Not all folders are under the root. Has to do with permissions
381
380
  # and how Google Drive works.
382
381
  # https://developers.google.com/drive/api/v3/reference/query-ref
383
- def get_all_files(justfiles: false, justfolders: false, parentfolderid: nil, name: nil)
384
- unless @drive_service
385
- log_error_and_raise("Drive service not initialized.")
386
- return false
387
- end
382
+ def get_all_files(justfiles: false, justfolders: false, parentfolderid: nil, name: nil, raise_error: @raise_error)
383
+ return log_error_and_raise("Drive service not initialized.") unless @drive_service
388
384
 
389
385
  # Number of files/directories to be returned each call to /files.
390
386
  # multiple page sizes are handled with the pageToken return value.
@@ -405,12 +401,16 @@ class GoogleDrive
405
401
 
406
402
  files = [ ]
407
403
  next_page_token = nil
408
- loop do
409
- # TODO: Should this be converted to block form and then use that to gracefully handle failure and errors?
410
- files_page = @drive_service.list_files(page_size: page_size, q: query, page_token: next_page_token, fields: fields)
411
- files_page.files.each {|f| files << f}
412
- next_page_token = files_page.next_page_token
413
- break unless next_page_token
404
+ begin
405
+ loop do
406
+ # TODO: Should this be converted to block form and then use that to gracefully handle failure and errors?
407
+ files_page = @drive_service.list_files(page_size: page_size, q: query, page_token: next_page_token, fields: fields)
408
+ files_page.files.each {|f| files << f}
409
+ next_page_token = files_page.next_page_token
410
+ break unless next_page_token
411
+ end
412
+ rescue => e
413
+ return log_error_and_raise("Error retrieving files: #{e}", raise_error)
414
414
  end
415
415
 
416
416
  # Process the returned files
@@ -437,45 +437,40 @@ class GoogleDrive
437
437
  end
438
438
 
439
439
  # returns all files by default in all folders
440
- def find_files(name = "*", parentfolderid: nil)
441
- return get_all_files(justfiles: true, parentfolderid: parentfolderid, name: name)
440
+ def find_files(name = "*", parentfolderid: nil, raise_error: @raise_error)
441
+ return get_all_files(justfiles: true, parentfolderid: parentfolderid, name: name, raise_error: raise_error)
442
442
  end
443
443
 
444
- def get_file_info(file_id)
445
- unless @drive_service
446
- log_error_and_raise("Drive service not initialized.")
447
- return false
444
+ def get_file_info(file_id, raise_error: @raise_error)
445
+ return log_error_and_raise("Drive service not initialized.") unless @drive_service
446
+ begin
447
+ # TODO: Maybe convert this to block format and handle errors like in other places
448
+ file = @drive_service.get_file(file_id, fields: "#{@file_fields}")
449
+ return process_file(file)
450
+ rescue => e
451
+ return log_error_and_raise("Error getting file info: #{e}.", raise_error)
448
452
  end
449
-
450
- # TODO: Maybe convert this to block format and handle errors like in other places
451
- file = @drive_service.get_file(file_id, fields: "#{@file_fields}")
452
- return process_file(file)
453
453
  end
454
454
 
455
- def find_directory_id(directory_name, parentfolderid: nil)
456
- file_list = get_all_files(justfolders: true, name: directory_name, parentfolderid: parentfolderid)
455
+ def find_directory_id(directory_name, parentfolderid: nil, raise_error: @raise_error)
456
+ file_list = get_all_files(justfolders: true, name: directory_name, parentfolderid: parentfolderid, raise_error: raise_error)
457
457
 
458
458
  if !file_list || (file_list.count == 0)
459
- log_error_and_raise("Directory not found.")
460
- return false
459
+ return log_error_and_raise("Directory not found.", raise_error)
461
460
  end
462
461
 
463
462
  return file_list.first[:id]
464
463
  end
465
464
 
466
465
  def upload_file(file, directory_id: nil)
467
- unless @drive_service
468
- log_error_and_raise("Drive service not initialized.")
469
- return false
470
- end
466
+ return log_error_and_raise("Drive service not initialized.") unless @drive_service
471
467
 
472
468
  file_basename = File.basename(file)
473
469
  # TODO: If no parent directory is passed, it will deny upload if a file by that name exists in any visible folder on any visible drive. How to fix?
474
470
  # see if file exists on Drive
475
471
  file_list = self.get_all_files(justfiles: true, parentfolderid: directory_id, name: file_basename)
476
472
  if file_list.count > 0
477
- log_error_and_raise("ERROR: File '#{file_basename}' already exists.")
478
- return false
473
+ return log_error_and_raise("ERROR: File '#{file_basename}' already exists.")
479
474
  end
480
475
 
481
476
  file_obj = Google::Apis::DriveV3::File.new(name: file_basename)
@@ -486,24 +481,19 @@ class GoogleDrive
486
481
  fields: @file_fields
487
482
  ) do |resfile, err|
488
483
  if err
489
- log_error_and_raise("Error: #{err}.")
490
- return false
484
+ return log_error_and_raise("Error uploading file: #{err}.")
491
485
  end
492
486
 
493
487
  # check that name = filename
494
488
  # check that kind = drive#file
495
489
  if !resfile.name # name key does not exist
496
- log_error_and_raise("no name key specified in response.")
497
- return false
490
+ return log_error_and_raise("no name key specified in response.")
498
491
  elsif !resfile.kind # kind key does not exist
499
- log_error_and_raise("no kind key specified in response.")
500
- return false
492
+ return log_error_and_raise("no kind key specified in response.")
501
493
  elsif resfile.kind != "drive#file" # Not of file type
502
- log_error_and_raise("kind is of non-file type.")
503
- return false
494
+ return log_error_and_raise("kind is of non-file type.")
504
495
  elsif resfile.name != file_basename # file name mismatch
505
- log_error_and_raise("file name mismatch.")
506
- return false
496
+ return log_error_and_raise("file name mismatch.")
507
497
  end
508
498
  # TODO: Add MD5 check, since we're now capable.
509
499
  end
@@ -512,15 +502,11 @@ class GoogleDrive
512
502
 
513
503
  # returns full path of downloaded file
514
504
  def download_file(file_name_or_id, parentfolderid: nil, file_path: nil)
515
- unless @drive_service
516
- log_error_and_raise("Drive service not initialized.")
517
- return false
518
- end
505
+ return log_error_and_raise("Drive service not initialized.") unless @drive_service
519
506
 
520
507
  # if file path passed, check it is valid.
521
508
  if file_path && !Dir.exist?(file_path)
522
- log_error_and_raise("File path '#{file_path}' does not exist.")
523
- return false
509
+ return log_error_and_raise("File path '#{file_path}' does not exist.")
524
510
  elsif !file_path # no path passed, use current directory
525
511
  file_path = Dir.getwd
526
512
  end
@@ -533,13 +519,11 @@ class GoogleDrive
533
519
  if files && (files.count == 1)
534
520
  file_info = files.first
535
521
  elsif files && (files.count > 1)
536
- log_error_and_raise("Multiple files with name '#{file_name_or_id}' exist. download_file() can only handle a single filename.")
537
- return false
522
+ return log_error_and_raise("Multiple files with name '#{file_name_or_id}' exist. download_file() can only handle a single filename.")
538
523
  else # either files is false or count is 0. assume file_name_or_id is an id.
539
524
  file_info = get_file_info(file_name_or_id)
540
525
  if !file_info
541
- log_error_and_raise("No file with ID '#{file_name_or_id}' exist.")
542
- return false
526
+ return log_error_and_raise("No file with ID '#{file_name_or_id}' exist.")
543
527
  end
544
528
  end
545
529
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: googledrive-easy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Bullock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-10-26 00:00:00.000000000 Z
12
+ date: 2022-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: googleauth