Rev 16 | Rev 18 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 16 | Rev 17 | ||
---|---|---|---|
Line 315... | Line 315... | ||
315 | function TVtsCurConv.GetJsonRaw(HistoricDate: TDate=0): string; |
315 | function TVtsCurConv.GetJsonRaw(HistoricDate: TDate=0): string; |
316 | 316 | ||
317 | procedure _HandleKeyInvalidOrMissing(cacheFileName: string; msg: string; out doRetry: boolean; out json: string); |
317 | procedure _HandleKeyInvalidOrMissing(cacheFileName: string; msg: string; out doRetry: boolean; out json: string); |
318 | resourcestring |
318 | resourcestring |
319 | S_ENTER_NEW_KEY = 'Do you want to enter a new one?'; |
319 | S_ENTER_NEW_KEY = 'Do you want to enter a new one?'; |
- | 320 | S_NO_CACHE_EXISTING = 'Cannot get the data online, and no cache exists. Please check your internet connection and API key.'; |
|
320 | begin |
321 | begin |
321 | if FallBackToCache then |
322 | if FallBackToCache then |
322 | begin |
323 | begin |
323 | if not InteractiveAPIKeyInput then |
324 | if not InteractiveAPIKeyInput then |
324 | begin |
325 | begin |
- | 326 | if not FileExists(cacheFileName) then |
|
- | 327 | begin |
|
- | 328 | raise Exception.Create(S_NO_CACHE_EXISTING); |
|
- | 329 | end; |
|
325 | json := FileGetContents(cacheFileName); |
330 | json := FileGetContents(cacheFileName); |
326 | doRetry := false; |
331 | doRetry := false; |
327 | end |
332 | end |
328 | else |
333 | else |
329 | begin |
334 | begin |
Line 332... | Line 337... | ||
332 | QueryAPIKey; |
337 | QueryAPIKey; |
333 | doRetry := true; |
338 | doRetry := true; |
334 | end |
339 | end |
335 | else |
340 | else |
336 | begin |
341 | begin |
- | 342 | if not FileExists(cacheFileName) then |
|
- | 343 | begin |
|
- | 344 | raise Exception.Create(S_NO_CACHE_EXISTING); |
|
- | 345 | end; |
|
337 | json := FileGetContents(cacheFileName); |
346 | json := FileGetContents(cacheFileName); |
338 | doRetry := false; |
347 | doRetry := false; |
339 | end; |
348 | end; |
340 | end; |
349 | end; |
341 | end |
350 | end |
Line 351... | Line 360... | ||
351 | doRetry := true; |
360 | doRetry := true; |
352 | end; |
361 | end; |
353 | end; |
362 | end; |
354 | end; |
363 | end; |
355 | 364 | ||
- | 365 | function protocol: string; |
|
- | 366 | begin |
|
- | 367 | if Secure then result := 'https' else result := 'http'; |
|
- | 368 | end; |
|
- | 369 | ||
- | 370 | function url: string; |
|
356 | var |
371 | var |
357 | sJSON, msg, protocol: string; |
- | |
358 | xRoot: TlkJSONobject; |
- | |
359 | xSuccess: TlkJSONboolean; |
- | |
360 | keyInvalid, doRetry: boolean; |
- | |
361 | sDate: string; |
372 | sDate: string; |
362 | url: string; |
373 | begin |
363 | cacheDirName, cacheFileName: string; |
374 | if HistoricDate = 0 then |
364 | needDownload: boolean; |
375 | begin |
365 | mTime: TDateTime; |
376 | sDate := ''; |
- | 377 | result := protocol + '://www.apilayer.net/api/live?access_key=' + ReadAPIKey; |
|
- | 378 | end |
|
- | 379 | else |
|
- | 380 | begin |
|
- | 381 | DateTimeToString(sDate, 'YYYY-MM-DD', HistoricDate); |
|
- | 382 | result := protocol + '://www.apilayer.net/api/historical?date=' + sDate + '&access_key=' + ReadAPIKey; |
|
- | 383 | end; |
|
- | 384 | end; |
|
- | 385 | ||
- | 386 | function cacheFileName: string; |
|
366 | resourcestring |
387 | resourcestring |
367 | S_CANNOT_CREATE_DIR = 'Cannot create directory %s'; |
388 | S_CANNOT_CREATE_DIR = 'Cannot create directory %s'; |
368 | S_INVALID_MAXAGE = 'Invalid maxage'; |
389 | var |
369 | S_NO_API_KEY_PROVIDED = 'No API key provided.'; |
- | |
370 | S_DOWNLOAD_QUERY = 'Download %s to %s ?'; |
390 | sDate: string; |
371 | S_JSON_FILE_INVALID = 'JSON file invalid'; |
- | |
372 | S_UNKNOWN_SUCCESS = 'Cannot determinate status of the query.'; |
- | |
373 | S_JSON_UNKNOWN_ERROR = 'Unknown error while loading JSON.'; |
- | |
374 | S_API_KEY_INVALID = 'API key invalid.'; |
391 | cacheDirName: string; |
375 | begin |
392 | begin |
376 | try |
- | |
377 | {$REGION 'Determinate file location and URL'} |
- | |
378 | // cacheDirName := IncludeTrailingPathDelimiter(GetSpecialPath(CSIDL_PROGRAM_FILES_COMMON)) + 'ViaThinkSoft\CurrencyConverter\'; |
393 | // cacheDirName := IncludeTrailingPathDelimiter(GetSpecialPath(CSIDL_PROGRAM_FILES_COMMON)) + 'ViaThinkSoft\CurrencyConverter\'; |
379 | cacheDirName := IncludeTrailingPathDelimiter(GetTempDir) + 'ViaThinkSoft\CurrencyConverter\'; |
394 | cacheDirName := IncludeTrailingPathDelimiter(GetTempDir) + 'ViaThinkSoft\CurrencyConverter\'; |
380 | if not ForceDirectories(cacheDirName) then |
395 | if not ForceDirectories(cacheDirName) then |
381 | begin |
396 | begin |
382 | raise EVtsCurConvException.CreateFmt(S_CANNOT_CREATE_DIR, [cacheDirName]); |
397 | raise EVtsCurConvException.CreateFmt(S_CANNOT_CREATE_DIR, [cacheDirName]); |
383 | end; |
398 | end; |
384 | 399 | ||
385 | if Secure then protocol := 'https' else protocol := 'http'; |
- | |
386 | if HistoricDate = 0 then |
400 | if HistoricDate = 0 then |
387 | begin |
401 | begin |
388 | sDate := ''; |
402 | sDate := ''; |
389 | url := protocol + '://www.apilayer.net/api/live?access_key=' + ReadAPIKey; |
- | |
390 | cacheFileName := IncludeTrailingPathDelimiter(cacheDirName) + 'live.json'; |
403 | result := IncludeTrailingPathDelimiter(cacheDirName) + 'live.json'; |
391 | end |
404 | end |
392 | else |
405 | else |
393 | begin |
406 | begin |
394 | DateTimeToString(sDate, 'YYYY-MM-DD', HistoricDate); |
407 | DateTimeToString(sDate, 'YYYY-MM-DD', HistoricDate); |
395 | url := protocol + '://www.apilayer.net/api/historical?date=' + sDate + '&access_key=' + ReadAPIKey; |
- | |
396 | cacheFileName := IncludeTrailingPathDelimiter(cacheDirName) + 'historical-' + sDate + '.json'; |
408 | result := IncludeTrailingPathDelimiter(cacheDirName) + 'historical-' + sDate + '.json'; |
- | 409 | end; |
|
397 | end; |
410 | end; |
398 | {$ENDREGION} |
- | |
399 | 411 | ||
- | 412 | var |
|
- | 413 | sJSON, msg: string; |
|
- | 414 | xRoot: TlkJSONobject; |
|
- | 415 | xSuccess: TlkJSONboolean; |
|
- | 416 | keyInvalid, doRetry: boolean; |
|
- | 417 | needDownload: boolean; |
|
- | 418 | mTime: TDateTime; |
|
- | 419 | resourcestring |
|
- | 420 | S_INVALID_MAXAGE = 'Invalid maxage'; |
|
- | 421 | S_NO_API_KEY_PROVIDED = 'No API key provided.'; |
|
- | 422 | S_DOWNLOAD_QUERY = 'Download %s to %s ?'; |
|
- | 423 | S_JSON_FILE_INVALID = 'JSON file invalid'; |
|
- | 424 | S_UNKNOWN_SUCCESS = 'Cannot determinate status of the query.'; |
|
- | 425 | S_JSON_UNKNOWN_ERROR = 'Unknown error while loading JSON.'; |
|
- | 426 | S_API_KEY_INVALID = 'API key invalid.'; |
|
- | 427 | begin |
|
- | 428 | try |
|
400 | {$REGION 'Determinate if we need to download or not'} |
429 | {$REGION 'Determinate if we need to download or not'} |
401 | if HistoricDate = 0 then |
430 | if HistoricDate = 0 then |
402 | begin |
431 | begin |
403 | needDownload := true; |
432 | needDownload := true; |
404 | if MaxAgeSeconds < -1 then |
433 | if MaxAgeSeconds < -1 then |
Line 426... | Line 455... | ||
426 | begin |
455 | begin |
427 | needDownload := not FileExists(cacheFileName) |
456 | needDownload := not FileExists(cacheFileName) |
428 | end; |
457 | end; |
429 | {$ENDREGION} |
458 | {$ENDREGION} |
430 | 459 | ||
431 | if not needDownload then |
460 | if not needDownload and FileExists(cacheFileName) then |
432 | begin |
461 | begin |
433 | sJSON := FileGetContents(cacheFileName); |
462 | sJSON := FileGetContents(cacheFileName); |
434 | end |
463 | end |
435 | else |
464 | else |
436 | begin |
465 | begin |
Line 451... | Line 480... | ||
451 | {$REGION 'Download and check if everything is OK'} |
480 | {$REGION 'Download and check if everything is OK'} |
452 | repeat |
481 | repeat |
453 | {$REGION 'Confirm web access?'} |
482 | {$REGION 'Confirm web access?'} |
454 | if ConfirmWebAccess and (MessageDlg(Format(S_DOWNLOAD_QUERY, [url, cacheFileName]), mtConfirmation, mbYesNoCancel, 0) <> ID_YES) then |
483 | if ConfirmWebAccess and (MessageDlg(Format(S_DOWNLOAD_QUERY, [url, cacheFileName]), mtConfirmation, mbYesNoCancel, 0) <> ID_YES) then |
455 | begin |
484 | begin |
456 | if FallBackToCache then |
485 | if FallBackToCache and FileExists(cacheFileName) then |
457 | begin |
486 | begin |
458 | result := FileGetContents(cacheFileName); |
487 | result := FileGetContents(cacheFileName); |
459 | Exit; |
488 | Exit; |
460 | end |
489 | end |
461 | else Abort; |
490 | else Abort; |
Line 499... | Line 528... | ||
499 | begin |
528 | begin |
500 | _HandleKeyInvalidOrMissing(cacheFileName, S_API_KEY_INVALID, doRetry, sJSON); |
529 | _HandleKeyInvalidOrMissing(cacheFileName, S_API_KEY_INVALID, doRetry, sJSON); |
501 | end |
530 | end |
502 | else // if not keyInvalid then |
531 | else // if not keyInvalid then |
503 | begin |
532 | begin |
504 | if FallBackToCache then |
533 | if FallBackToCache and FileExists(cacheFileName) then |
505 | begin |
534 | begin |
506 | result := FileGetContents(cacheFileName); |
535 | result := FileGetContents(cacheFileName); |
507 | Exit; |
536 | Exit; |
508 | end |
537 | end |
509 | else |
538 | else |