Skip to content

Commit ece6d4f

Browse files
committed
chore: use common function for backend http requests
Establish Util::call_api_keyman_com and Util::call_downloads_keyman_com as common functions for making http requests to these servers. Use curl instead of file_get_contents as it is more reliable and we have more control over parameters. The pattern for now could stand to be refactored further in the future to provide API functions for each endpoint and reduce repetition and fragility. Test-bot: skip
1 parent da2d7da commit ece6d4f

27 files changed

Lines changed: 43252 additions & 27 deletions

_content/developer/keymanweb/keyboards.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once _KEYMANCOM_INCLUDES . '/includes/template.php';
33
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
44
use Keyman\Site\Common\KeymanHosts;
5+
use Keyman\Site\com\keyman\Util;
56

67
// Required
78
head([
@@ -110,7 +111,7 @@
110111
<tbody>
111112

112113
<?php
113-
$data = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . '/cloud/4.0/keyboards?languageidtype=bcp47&version='.$stable_version);
114+
$data = Util::call_api_keyman_com('/cloud/4.0/keyboards?languageidtype=bcp47&version='.$stable_version, 'api.keyman.com-cloud_4.0_keyboards.json');
114115
if($data === FALSE) {
115116
// fallback if API is down, bad news anyway.
116117
$data = file_get_contents('keyboards.txt');

_content/developer/keymanweb/keymanweb-version.inc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
require_once _KEYMANCOM_INCLUDES . '/includes/servervars.php';
33

44
use Keyman\Site\Common\KeymanHosts;
5+
use Keyman\Site\com\keyman\Util;
56

67
function getKeymanWebHref()
78
{
8-
$json = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . "/version/web");
9+
$json = Util::call_api_keyman_com("/version/web", 'api.keyman.com-version_web.json');
910
if ($json) {
1011
$json = json_decode($json);
1112
}

_content/downloads/all-versions/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once _KEYMANCOM_INCLUDES . '/includes/template.php';
33
require_once _KEYMANCOM_INCLUDES . '/includes/ui/downloads.php';
44
use Keyman\Site\Common\KeymanHosts;
5+
use Keyman\Site\com\keyman\Util;
56

67
// Required
78
head([
@@ -14,7 +15,7 @@
1415
<h2 class="red underline large">Keyman downloads - all versions</h2>
1516

1617
<?php
17-
$data = json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/all'));
18+
$data = @json_decode(Util::call_downloads_keyman_com('/api/version/all', 'downloads.keyman.com-api_version_all.json'));
1819

1920
if ($data === NULL) {
2021
die('Error decoding JSON data.');

_content/downloads/releases/_version_downloads.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_once _KEYMANCOM_INCLUDES . '/includes/playstore.php';
66
use Keyman\Site\Common\KeymanHosts;
77
use Keyman\Site\com\keyman\Locale;
8+
use Keyman\Site\com\keyman\Util;
89

910
Locale::definePageScope('LOCALE_DOWNLOADS', 'downloads');
1011

@@ -31,8 +32,7 @@ function array_key_first(array $arr) {
3132
}
3233

3334
// note: we currently ignore the tier parameter
34-
35-
$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . "/api/version/2.0?targetVersion=$version"));
35+
$versions = @json_decode(Util::call_downloads_keyman_com("/api/version/2.0?targetVersion=$version", 'downloads.keyman.com-api_version_2.0.json'));
3636

3737
if(empty($versions->android))
3838
$tier = 'unknown';
@@ -47,8 +47,7 @@ function array_key_first(array $arr) {
4747
$versionTier = $version . ($tier == 'stable' ? "" : "-$tier");
4848

4949

50-
51-
$versionsData = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . "/api/version/all"));
50+
$versionsData = @json_decode(Util::call_downloads_keyman_com("/api/version/all", 'downloads.keyman.com-api_version_all.json'));
5251
if (!$versionsData) {
5352
die("Failed to retrieve or parse the API data.");
5453
}

_content/keyboards/install.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ protected static function LoadData() {
375375

376376
// Get Keyboard Metadata
377377

378-
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . '/keyboard/' . rawurlencode(self::$id));
378+
$s = Util::call_api_keyman_com('/keyboard/' . rawurlencode(self::$id), 'api.keyman.com-keyboard_sil_ipa.json');
379379
if ($s === FALSE) {
380380
// Will fail later in the script
381381
self::$error .= error_get_last()['message'] . "\n";
@@ -396,7 +396,7 @@ protected static function LoadData() {
396396

397397
// Get Program Download Versions and URLs
398398

399-
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/1.0');
399+
$s = Util::call_downloads_keyman_com('/api/version/1.0', 'downloads.keyman.com-api_version_1.0.json');
400400
if ($s === FALSE) {
401401
// Will fail later in the script
402402
self::$error .= error_get_last()['message'] . "\n";

_content/keyboards/keyboard.json.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once _KEYMANCOM_INCLUDES . '/includes/servervars.php';
33
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
44
use Keyman\Site\Common\KeymanHosts;
5+
use Keyman\Site\com\keyman\Util;
56

67
if(!isset($_REQUEST['id'])) {
78
header('HTTP/1.0 404 id parameter is required');
@@ -14,7 +15,7 @@
1415
header('Content-Type: application/json; charset=utf-8');
1516
header('Access-Control-Allow-Origin: *');
1617

17-
$kmw = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com . "/cloud/4.0/keyboards/$id?version=$version&languageidtype=bcp47");
18+
$kmw = Util::call_api_keyman_com("/cloud/4.0/keyboards/$id?version=$version&languageidtype=bcp47", 'api.keyman.com-cloud_4.0_keyboards_sil_ipa.json');
1819
if($kmw === FALSE) {
1920
header('HTTP/1.0 404 Keyboard not found');
2021
exit;

_content/keyboards/keyboard.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_once('./session.php');
55
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
66
use Keyman\Site\Common\KeymanHosts;
7+
use Keyman\Site\com\keyman\Util;
78

89
if(isset($_REQUEST['legacy'])) {
910
$id = find_id_by_legacy(clean_id($_REQUEST['legacy']));
@@ -27,7 +28,7 @@ function clean_id($id) {
2728
}
2829

2930
function find_id_by_legacy($legacy) {
30-
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/search/?q=k:legacy:'.rawurlencode($legacy));
31+
$s = Util::call_api_keyman_com('/search/?q=k:legacy:'.rawurlencode($legacy), 'api.keyman.com-search.json');
3132
if($s === FALSE) {
3233
return null;
3334
}

_content/keyboards/share.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_once _KEYMANCOM_INCLUDES . '/autoload.php';
66
use Keyman\Site\Common\KeymanHosts;
77
use Keyman\Site\com\keyman\Locale;
8+
use Keyman\Site\com\keyman\Util;
89

910
Locale::definePageScope('LOCALE_KEYBOARDS_SHARE', 'keyboards/share');
1011

@@ -24,7 +25,7 @@ function clean_id($id) {
2425
}
2526

2627
function find_keyboard($id) {
27-
$s = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com.'/keyboard/'.$id);
28+
$s = Util::call_api_keyman_com('/keyboard/'.$id, 'api.keyman.com-keyboard_sil_ipa.json');
2829
if($s === FALSE) {
2930
return null;
3031
}

_includes/2020/KeymanDownloadVersions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Keyman\Site\com\keyman;
55

66
use Keyman\Site\Common\KeymanHosts;
7+
use Keyman\Site\com\keyman\Util;
78

89
class KeymanDownloadVersions {
910
private static $versions;
@@ -18,7 +19,7 @@ class KeymanDownloadVersions {
1819

1920
static function getDownloadUrls() {
2021
if(empty(self::$versions))
21-
self::$versions = @json_decode(file_get_contents(KeymanHosts::Instance()->SERVER_downloads_keyman_com . '/api/version/2.0'));
22+
self::$versions = @json_decode(Util::call_downloads_keyman_com('/api/version/2.0', 'downloads.keyman.com-api_version_2.0.json'));
2223
return self::$versions;
2324
}
2425

_includes/2020/KeymanWebHost.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Keyman\Site\com\keyman;
66
use Keyman\Site\Common\KeymanHosts;
7+
use Keyman\Site\com\keyman\Util;
78

89
class KeymanWebHost {
910
static function getKeymanWebUrlBase() {
10-
$json = @file_get_contents(KeymanHosts::Instance()->SERVER_api_keyman_com."/version/web");
11+
$json = Util::call_api_keyman_com("/version/web", 'api.keyman.com-version_web.json');
1112
if($json) {
1213
$json = json_decode($json);
1314
}

0 commit comments

Comments
 (0)