@@ -154,7 +154,7 @@ setRefClass(
154
154
endDate <- max(dates )
155
155
}
156
156
157
- if (.self $ api_key == " " ) {
157
+ if (.self $ getApiKey() == " " ) {
158
158
.self $ loginUSGSApiKey(verbose = verbose )
159
159
}
160
160
attempts <- 5
@@ -170,7 +170,7 @@ setRefClass(
170
170
)
171
171
if (verbose ) message(paste0(" Landsat_query: " ,query ))
172
172
173
- jsonres <- .self $ postApiUSGS(query $ url , query $ json , .self $ api_key )
173
+ jsonres <- .self $ postApiUSGS(query $ url , query $ json , .self $ getApiKey() )
174
174
attempts <- attempts - 1
175
175
if (is.null(jsonres $ errorCode )) {
176
176
break
@@ -294,6 +294,13 @@ setRefClass(
294
294
}
295
295
.self $ api_key
296
296
},
297
+ setApiKey = function (api_key ,verbose = FALSE ){
298
+ if (is.null(api_key )){
299
+ .self $ api_key <- " "
300
+ }else {
301
+ .self $ api_key <- api_key
302
+ }
303
+ },
297
304
loginUSGSApiKey = function (verbose = FALSE ) {
298
305
jsonquery <- list (
299
306
" username" = .self $ username ,
@@ -310,8 +317,9 @@ setRefClass(
310
317
if (! is.null(res $ errorCode )) {
311
318
stop(res $ errorMessage )
312
319
}
313
- if (verbose ) message(" Logged into EE API." )
314
- .self $ api_key <- res $ data
320
+ if (verbose ) message(paste0(" Logged into EE API. Api key:" ,res $ data ))
321
+ .self $ setApiKey(res $ data ,verbose )
322
+ if (is.null(res $ data )) stop(" API key is null." )
315
323
},
316
324
postApiUSGS = function (url , body , key ) {
317
325
names(key ) <- " X-Auth-Token"
@@ -330,7 +338,7 @@ setRefClass(
330
338
},
331
339
getUSGSdatasetID = function (product , verbose = FALSE ) {
332
340
url <- paste0(.self $ m2m_server , " /dataset" )
333
- key <- .self $ api_key
341
+ key <-
334
342
names(key ) <- " X-Auth-Token"
335
343
body <- paste0(' {"datasetName":"' , product , ' "}' )
336
344
post.res <- POST(
@@ -345,7 +353,7 @@ setRefClass(
345
353
return (content(post.res )$ data )
346
354
},
347
355
postDownloadUSGS = function (url , verbose = FALSE ) {
348
- key <- .self $ api_key
356
+ key <- .self $ getApiKey()
349
357
names(key ) <- " X-Auth-Token"
350
358
body <- paste0(" {}" )
351
359
post.res <- POST(
@@ -360,7 +368,7 @@ setRefClass(
360
368
return (content(post.res )$ data )
361
369
},
362
370
logoutUSGSAPI = function () {
363
- jsonquery <- list (" apikey" = .self $ api_key )
371
+ jsonquery <- list (" apikey" = .self $ getApiKey() )
364
372
if (! is.null(jsonquery $ apikey )) {
365
373
post.res <- POST(
366
374
url = paste0(.self $ m2m_server , " /logout" ),
@@ -370,7 +378,7 @@ setRefClass(
370
378
res <- content(post.res )
371
379
if (res $ error != " " ) {
372
380
message(" Logged out from USGS API." )
373
- .self $ api_key <- NULL
381
+ .self $ setApiKey( " " , verbose )
374
382
} else {
375
383
message(" You are not logged in USGS API." )
376
384
stop(res $ error )
0 commit comments