|
1 | 1 | import { gui_log } from "./gui_log"; |
2 | 2 | import { i18n } from "./localization"; |
3 | 3 | import { get as getStorage, set as setStorage } from "./SessionStorage"; |
4 | | -import $ from "jquery"; |
| 4 | +import CONFIGURATOR from "./data_storage.js"; |
5 | 5 |
|
6 | 6 | export default class BuildApi { |
7 | 7 | constructor() { |
8 | 8 | this._url = "https://build.betaflight.com"; |
9 | 9 | this._cacheExpirationPeriod = 3600 * 1000; |
10 | 10 | } |
11 | 11 |
|
12 | | - load(url, onSuccess, onFailure) { |
| 12 | + isSuccessCode(code) { |
| 13 | + return code === 200 || code === 201 || code === 202; |
| 14 | + } |
| 15 | + |
| 16 | + async fetchText(url) { |
| 17 | + const response = await fetch(url, { |
| 18 | + method: "GET", |
| 19 | + headers: { |
| 20 | + "User-Agent": navigator.userAgent, |
| 21 | + "X-CFG-VER": `${CONFIGURATOR.version}`, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + if (this.isSuccessCode(response.status)) { |
| 26 | + return await response.text(); |
| 27 | + } |
| 28 | + |
| 29 | + gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${response.status}`])); |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + async fetchJson(url) { |
| 34 | + const response = await fetch(url, { |
| 35 | + method: "GET", |
| 36 | + headers: { |
| 37 | + "User-Agent": navigator.userAgent, |
| 38 | + "X-CFG-VER": `${CONFIGURATOR.version}`, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + if (this.isSuccessCode(response.status)) { |
| 43 | + return await response.json(); |
| 44 | + } |
| 45 | + |
| 46 | + gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${response.status}`])); |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + async fetchCachedJson(url) { |
13 | 51 | const dataTag = `${url}_Data`; |
14 | 52 | const cacheLastUpdateTag = `${url}_LastUpdate`; |
15 | 53 |
|
16 | | - const result = getStorage([cacheLastUpdateTag, dataTag]); |
17 | | - const dataTimestamp = $.now(); |
18 | | - const cachedData = result[dataTag]; |
19 | | - const cachedLastUpdate = result[cacheLastUpdateTag]; |
20 | | - |
21 | | - const cachedCallback = () => { |
22 | | - if (cachedData) { |
23 | | - gui_log(i18n.getMessage("buildServerUsingCached", [url])); |
24 | | - } |
25 | | - |
26 | | - onSuccess(cachedData); |
27 | | - }; |
28 | | - |
29 | | - if (!cachedData || !cachedLastUpdate || dataTimestamp - cachedLastUpdate > this._cacheExpirationPeriod) { |
30 | | - $.get(url, function (info) { |
31 | | - // cache loaded info |
32 | | - const object = {}; |
33 | | - object[dataTag] = info; |
34 | | - object[cacheLastUpdateTag] = $.now(); |
35 | | - setStorage(object); |
36 | | - onSuccess(info); |
37 | | - }).fail((xhr) => { |
38 | | - gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${xhr.status}`])); |
39 | | - if (onFailure !== undefined) { |
40 | | - onFailure(); |
41 | | - } else { |
42 | | - cachedCallback(); |
43 | | - } |
44 | | - }); |
45 | | - } else { |
46 | | - cachedCallback(); |
| 54 | + const storageResult = getStorage([cacheLastUpdateTag, dataTag]); |
| 55 | + const dataTimestamp = Date.now(); |
| 56 | + const cachedData = storageResult[dataTag]; |
| 57 | + const cachedLastUpdate = storageResult[cacheLastUpdateTag]; |
| 58 | + |
| 59 | + if (cachedData && cachedLastUpdate && dataTimestamp - cachedLastUpdate < this._cacheExpirationPeriod) { |
| 60 | + gui_log(i18n.getMessage("buildServerUsingCached", [url])); |
| 61 | + return cachedData; |
47 | 62 | } |
| 63 | + |
| 64 | + const response = await fetch(url, { |
| 65 | + method: "GET", |
| 66 | + headers: { |
| 67 | + "User-Agent": navigator.userAgent, |
| 68 | + "X-CFG-VER": `${CONFIGURATOR.version}`, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + if (response.status === 500) { |
| 73 | + throw new Error(await response.text()); |
| 74 | + } |
| 75 | + |
| 76 | + if (response.status === 404) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + const result = await response.json(); |
| 81 | + |
| 82 | + const object = {}; |
| 83 | + object[dataTag] = result; |
| 84 | + object[cacheLastUpdateTag] = Date.now(); |
| 85 | + setStorage(object); |
| 86 | + return result; |
48 | 87 | } |
49 | 88 |
|
50 | | - loadTargets(callback) { |
| 89 | + async loadTargets() { |
51 | 90 | const url = `${this._url}/api/targets`; |
52 | | - this.load(url, callback); |
| 91 | + return await this.fetchCachedJson(url); |
53 | 92 | } |
54 | 93 |
|
55 | | - loadTargetReleases(target, callback) { |
| 94 | + async loadTargetReleases(target) { |
56 | 95 | const url = `${this._url}/api/targets/${target}`; |
57 | | - this.load(url, callback); |
| 96 | + return await this.fetchCachedJson(url); |
58 | 97 | } |
59 | 98 |
|
60 | | - loadTarget(target, release, onSuccess, onFailure) { |
| 99 | + async loadTarget(target, release) { |
61 | 100 | const url = `${this._url}/api/builds/${release}/${target}`; |
62 | | - this.load(url, onSuccess, onFailure); |
| 101 | + return await this.fetchCachedJson(url); |
63 | 102 | } |
64 | 103 |
|
65 | | - loadTargetHex(path, onSuccess, onFailure) { |
| 104 | + async loadTargetHex(path) { |
66 | 105 | const url = `${this._url}${path}`; |
67 | | - $.get(url, function (data) { |
68 | | - gui_log(i18n.getMessage("buildServerSuccess", [path])); |
69 | | - onSuccess(data); |
70 | | - }).fail((xhr) => { |
71 | | - gui_log(i18n.getMessage("buildServerFailure", [path, `HTTP ${xhr.status}`])); |
72 | | - if (onFailure !== undefined) { |
73 | | - onFailure(); |
74 | | - } |
75 | | - }); |
| 106 | + return await this.fetchText(url); |
76 | 107 | } |
77 | 108 |
|
78 | | - getSupportCommands(onSuccess, onFailure) { |
| 109 | + async getSupportCommands() { |
79 | 110 | const url = `${this._url}/api/support/commands`; |
80 | | - $.get(url, function (data) { |
81 | | - onSuccess(data); |
82 | | - }).fail((xhr) => { |
83 | | - gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${xhr.status}`])); |
84 | | - if (onFailure !== undefined) { |
85 | | - onFailure(); |
86 | | - } |
87 | | - }); |
| 111 | + return await this.fetchJson(url); |
88 | 112 | } |
89 | 113 |
|
90 | | - submitSupportData(data, onSuccess, onFailure) { |
| 114 | + async submitSupportData(data) { |
91 | 115 | const url = `${this._url}/api/support`; |
92 | | - $.ajax({ |
93 | | - url: url, |
94 | | - type: "POST", |
95 | | - data: data, |
96 | | - contentType: "text/plain", |
97 | | - dataType: "text", |
98 | | - |
99 | | - success: function (response) { |
100 | | - onSuccess(response); |
| 116 | + |
| 117 | + const response = await fetch(url, { |
| 118 | + method: "POST", |
| 119 | + headers: { |
| 120 | + "Content-Type": "text/plain", |
| 121 | + "User-Agent": navigator.userAgent, |
| 122 | + "X-CFG-VER": `${CONFIGURATOR.version}`, |
101 | 123 | }, |
102 | | - }).fail((xhr) => { |
103 | | - gui_log(i18n.getMessage("buildServerFailure", [`HTTP ${xhr.status}`])); |
104 | | - if (onFailure !== undefined) { |
105 | | - onFailure(); |
106 | | - } |
| 124 | + body: data, |
107 | 125 | }); |
| 126 | + |
| 127 | + if (response.status === 200) { |
| 128 | + return await response.text(); |
| 129 | + } |
| 130 | + |
| 131 | + gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${response.status}`])); |
| 132 | + return null; |
108 | 133 | } |
109 | 134 |
|
110 | | - requestBuild(request, onSuccess, onFailure) { |
| 135 | + async requestBuild(request) { |
111 | 136 | const url = `${this._url}/api/builds`; |
112 | | - $.ajax({ |
113 | | - url: url, |
114 | | - type: "POST", |
115 | | - data: JSON.stringify(request), |
116 | | - contentType: "application/json", |
117 | | - dataType: "json", |
118 | | - |
119 | | - success: function (response) { |
120 | | - onSuccess(response); |
| 137 | + |
| 138 | + const response = await fetch(url, { |
| 139 | + method: "POST", |
| 140 | + headers: { |
| 141 | + "Content-Type": "application/json", |
| 142 | + "User-Agent": navigator.userAgent, |
| 143 | + "X-CFG-VER": `${CONFIGURATOR.version}`, |
121 | 144 | }, |
122 | | - }).fail((xhr) => { |
123 | | - gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${xhr.status}`])); |
124 | | - if (onFailure !== undefined) { |
125 | | - onFailure(); |
126 | | - } |
| 145 | + body: JSON.stringify(request), |
127 | 146 | }); |
| 147 | + |
| 148 | + if (this.isSuccessCode(response.status)) { |
| 149 | + return await response.json(); |
| 150 | + } |
| 151 | + |
| 152 | + gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${response.status}`])); |
| 153 | + return null; |
128 | 154 | } |
129 | 155 |
|
130 | | - requestBuildStatus(key, onSuccess, onFailure) { |
| 156 | + async requestBuildStatus(key) { |
131 | 157 | const url = `${this._url}/api/builds/${key}/status`; |
132 | | - $.get(url, function (data) { |
133 | | - gui_log(i18n.getMessage("buildServerSuccess", [url])); |
134 | | - onSuccess(data); |
135 | | - }).fail((xhr) => { |
136 | | - gui_log(i18n.getMessage("buildServerFailure", [url, `HTTP ${xhr.status}`])); |
137 | | - if (onFailure !== undefined) { |
138 | | - onFailure(); |
139 | | - } |
140 | | - }); |
| 158 | + return await this.fetchJson(url); |
141 | 159 | } |
142 | 160 |
|
143 | | - requestBuildOptions(key, onSuccess, onFailure) { |
| 161 | + async requestBuildOptions(key) { |
144 | 162 | const url = `${this._url}/api/builds/${key}/json`; |
145 | | - $.get(url, function (data) { |
146 | | - onSuccess(data); |
147 | | - }).fail((xhr) => { |
148 | | - if (onFailure !== undefined) { |
149 | | - onFailure(); |
150 | | - } |
151 | | - }); |
| 163 | + return await this.fetchJson(url); |
152 | 164 | } |
153 | 165 |
|
154 | | - loadOptions(release, onSuccess, onFailure) { |
| 166 | + async loadOptions(release) { |
155 | 167 | const url = `${this._url}/api/options/${release}`; |
156 | | - this.load(url, onSuccess, onFailure); |
| 168 | + return await this.fetchJson(url); |
157 | 169 | } |
158 | 170 |
|
159 | | - loadOptionsByBuildKey(release, key, onSuccess, onFailure) { |
| 171 | + async loadOptionsByBuildKey(release, key) { |
160 | 172 | const url = `${this._url}/api/options/${release}/${key}`; |
161 | | - this.load(url, onSuccess, onFailure); |
| 173 | + return await this.fetchJson(url); |
162 | 174 | } |
163 | 175 |
|
164 | | - loadCommits(release, onSuccess, onFailure) { |
| 176 | + async loadCommits(release) { |
165 | 177 | const url = `${this._url}/api/releases/${release}/commits`; |
166 | | - this.load(url, onSuccess, onFailure); |
| 178 | + return await this.fetchJson(url); |
167 | 179 | } |
168 | 180 |
|
169 | | - loadConfiguratorRelease(type, onSuccess, onFailure) { |
| 181 | + async loadConfiguratorRelease(type) { |
170 | 182 | const url = `${this._url}/api/configurator/releases/${type}`; |
171 | | - this.load(url, onSuccess, onFailure); |
| 183 | + return await this.fetchJson(url); |
172 | 184 | } |
173 | 185 |
|
174 | | - loadSponsorTile(mode, page, onSuccess, onFailure) { |
| 186 | + async loadSponsorTile(mode, page) { |
175 | 187 | const url = `${this._url}/api/configurator/sponsors/${mode}/${page}`; |
176 | | - $.get(url, function (data) { |
177 | | - onSuccess(data); |
178 | | - }).fail((xhr) => { |
179 | | - if (onFailure !== undefined) { |
180 | | - onFailure(); |
181 | | - } |
182 | | - }); |
| 188 | + return await this.fetchText(url); |
183 | 189 | } |
184 | 190 | } |
0 commit comments