Skip to content

Commit a78721e

Browse files
authored
feat: add backup connection method (#74)
1 parent c5ba5cc commit a78721e

5 files changed

Lines changed: 15 additions & 29 deletions

File tree

scripts/bikes.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ let tripBeingRated = false;
77
// reserves the bike and returns a success boolean
88
async function reserveBike(serialNumber) {
99
const response = await makePostRequest(
10-
GIRA_GRAPHQL_ENDPOINT,
1110
JSON.stringify({
1211
operationName: "reserveBike",
1312
variables: { input: serialNumber },
@@ -21,7 +20,6 @@ async function reserveBike(serialNumber) {
2120
// cancels the bike reserve and returns a success boolean
2221
async function cancelBikeReserve() {
2322
const response = await makePostRequest(
24-
GIRA_GRAPHQL_ENDPOINT,
2523
JSON.stringify({
2624
operationName: "cancelBikeReserve",
2725
variables: {},
@@ -35,7 +33,6 @@ async function cancelBikeReserve() {
3533
// starts a trip and returns a success boolean
3634
async function startTrip() {
3735
const response = await makePostRequest(
38-
GIRA_GRAPHQL_ENDPOINT,
3936
JSON.stringify({
4037
operationName: "startTrip",
4138
variables: {},
@@ -49,7 +46,6 @@ async function startTrip() {
4946
// returns an int or float of the active trip cost
5047
async function getActiveTripCost() {
5148
const response = await makePostRequest(
52-
GIRA_GRAPHQL_ENDPOINT,
5349
JSON.stringify({
5450
operationName: "activeTripCost",
5551
variables: {},
@@ -63,7 +59,6 @@ async function getActiveTripCost() {
6359
// returns the activeTrip object
6460
async function getActiveTrip() {
6561
const response = await makePostRequest(
66-
GIRA_GRAPHQL_ENDPOINT,
6762
JSON.stringify({
6863
operationName: "activeTrip",
6964
variables: {},
@@ -77,7 +72,6 @@ async function getActiveTrip() {
7772
// returns success boolean
7873
async function rateTripAPI(tripCode, tripRating, tripComment) {
7974
const response = await makePostRequest(
80-
GIRA_GRAPHQL_ENDPOINT,
8175
JSON.stringify({
8276
operationName: "rateTrip",
8377
variables: {
@@ -98,7 +92,6 @@ async function rateTripAPI(tripCode, tripRating, tripComment) {
9892
// returns int? (0 for success)
9993
async function tripPayWithNoPoints(tripCode) {
10094
const response = await makePostRequest(
101-
GIRA_GRAPHQL_ENDPOINT,
10295
JSON.stringify({
10396
operationName: "tripPayWithNoPoints",
10497
variables: { input: tripCode },
@@ -112,7 +105,6 @@ async function tripPayWithNoPoints(tripCode) {
112105
// returns int? (0 for success)
113106
async function tripPayWithPoints(tripCode) {
114107
const response = await makePostRequest(
115-
GIRA_GRAPHQL_ENDPOINT,
116108
JSON.stringify({
117109
operationName: "tripPayWithPoints",
118110
variables: { input: tripCode },

scripts/requests.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const errorTranslations = {
3939
'Saldo negativo. Se isto se deve a uma viagem mal terminada, por favor contacte <a href="mailto:gira@emel.pt">gira@emel.pt</a>',
4040
};
4141

42-
async function makePostRequest(url, body, accessToken = null) {
42+
async function makePostRequest(body, accessToken = null) {
43+
const url = user.firebaseToken ? GIRA_GRAPHQL_ENDPOINT : GIRA_GRAPHQL_WS_ENDPOINT.replace("wss", "https");
4344
// Increment current request try
4445
currentRequestTry += 1;
4546

@@ -49,7 +50,9 @@ async function makePostRequest(url, body, accessToken = null) {
4950
"User-Agent": "Gira/3.4.3 (Android 34)",
5051
"Content-Type": "application/json",
5152
"X-Authorization": `Bearer ${accessToken}`,
52-
"X-Firebase-Token": await encryptFirebaseToken(user.firebaseToken, user.accessToken),
53+
...(user.firebaseToken
54+
? { "X-Firebase-Token": await encryptFirebaseToken(user.firebaseToken, user.accessToken) }
55+
: {}),
5356
},
5457
body: body,
5558
});
@@ -67,7 +70,7 @@ async function makePostRequest(url, body, accessToken = null) {
6770
// check if token refresh was successful and there's a firebase token
6871
if (typeof accessToken !== "undefined" && user.firebaseToken) {
6972
// try to make request again
70-
return await retryPostRequest(url, body, accessToken, "Erro da API (401)"); // be sure to use latest available token
73+
return await retryPostRequest(body, accessToken, "Erro da API (401)"); // be sure to use latest available token
7174
}
7275
} else if (response.ok) {
7376
const responseObject = await response.json();
@@ -100,7 +103,7 @@ async function makePostRequest(url, body, accessToken = null) {
100103
if (currentRequestTry < NUMBER_OF_RETRIES) {
101104
// Wait before making next request (reduce error rate)
102105
await delay(200);
103-
return await makePostRequest(url, body, accessToken);
106+
return await makePostRequest(body, accessToken);
104107
} else {
105108
// Warn user about the API error
106109
alert("Erro da API");
@@ -110,12 +113,12 @@ async function makePostRequest(url, body, accessToken = null) {
110113
} else if (response.status === 403) {
111114
// Common API processing error
112115
// try for x times to do the request, otherwise just error out
113-
return await retryPostRequest(url, body, accessToken, "Erro da API (403)");
116+
return await retryPostRequest(body, accessToken, "Erro da API (403)");
114117

115118
// if (currentRequestTry < NUMBER_OF_RETRIES) {
116119
// // Wait before making next request (reduce error rate)
117120
// await delay(200);
118-
// return await makePostRequest(url, body, accessToken);
121+
// return await makePostRequest(body, accessToken);
119122
// } else {
120123
// // Warn user about the API error
121124
// alert("Erro da API (403)");
@@ -357,11 +360,11 @@ function startWSConnection(force = false) {
357360
};
358361
}
359362

360-
async function retryPostRequest(url, body, accessToken, errorMessage) {
363+
async function retryPostRequest(body, accessToken, errorMessage) {
361364
if (currentRequestTry < NUMBER_OF_RETRIES) {
362365
// Wait before making next request (reduce error rate)
363366
await delay(200);
364-
return await makePostRequest(url, body, accessToken);
367+
return await makePostRequest(body, accessToken);
365368
} else {
366369
// Warn user about the API error
367370
alert(errorMessage);

scripts/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// returns Date and Time in ISO 8601(?) format
22
async function getServerTime() {
33
const response = await makePostRequest(
4-
GIRA_GRAPHQL_ENDPOINT,
54
JSON.stringify({
65
operationName: "getServerTime",
76
variables: {},

scripts/stations.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ let userClickedNavigateToStation = false;
55
// returns an array with all the bikes in a station
66
async function getBikes(stationID) {
77
const response = await makePostRequest(
8-
GIRA_GRAPHQL_ENDPOINT,
98
JSON.stringify({
109
operationName: "getBikes",
1110
variables: { input: stationID },
@@ -20,7 +19,6 @@ async function getBikes(stationID) {
2019
// returns an array with all the docks in a station
2120
async function getDocks(stationID) {
2221
const response = await makePostRequest(
23-
GIRA_GRAPHQL_ENDPOINT,
2422
JSON.stringify({
2523
operationName: "getDocks",
2624
variables: { input: stationID },
@@ -36,7 +34,6 @@ async function getDocks(stationID) {
3634
// returns an object with both getBikes and getDocks properties
3735
async function getBikesAndDocks(stationID) {
3836
const response = await makePostRequest(
39-
GIRA_GRAPHQL_ENDPOINT,
4037
JSON.stringify({
4138
query: `query {
4239
getBikes(input: "${stationID}") { battery, code, name, kms, serialNumber, type, parent }
@@ -51,7 +48,6 @@ async function getBikesAndDocks(stationID) {
5148
// sets the global array stationArray
5249
async function getStations() {
5350
const response = await makePostRequest(
54-
GIRA_GRAPHQL_ENDPOINT,
5551
JSON.stringify({
5652
operationName: "getStations",
5753
variables: {},
@@ -305,7 +301,6 @@ async function updateBikeList() {
305301
queryString += "}";
306302

307303
const response = await makePostRequest(
308-
GIRA_GRAPHQL_ENDPOINT,
309304
JSON.stringify({
310305
query: queryString,
311306
}),

scripts/user.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async function fetchFirebaseToken(accessToken) {
102102
token = await res.text();
103103
if (!res.ok) {
104104
console.error("Error fetching encrypted token: ", token);
105-
alert("Erro ao obter o token de verificação do dispositivo. A app pode não funcionar corretamente.");
105+
// alert("Erro ao obter o token de verificação do dispositivo. A app pode não funcionar corretamente.");
106106
return null;
107107
}
108108
const { exp } = getJWTPayload(token);
@@ -119,9 +119,6 @@ function getJWTPayload(token) {
119119
}
120120

121121
async function runStartupFunctions() {
122-
// Get all user details
123-
await getUserInformation();
124-
125122
// Check if update info should be shown
126123
showUpdateInfoIfNeeded();
127124

@@ -147,11 +144,13 @@ async function runStartupFunctions() {
147144

148145
// Show any messages from EMEL
149146
await validateLogin();
147+
148+
// Get all user details
149+
await getUserInformation();
150150
}
151151

152152
async function validateLogin() {
153153
const response = await makePostRequest(
154-
GIRA_GRAPHQL_ENDPOINT,
155154
JSON.stringify({
156155
query: `mutation {
157156
validateLogin(in: {
@@ -180,7 +179,6 @@ async function getUserInformation() {
180179

181180
// Make batch query for Gira client information, activeUserSubscriptions and tripHistory to speed up request
182181
response = await makePostRequest(
183-
GIRA_GRAPHQL_ENDPOINT,
184182
JSON.stringify({
185183
query: `query {
186184
client: client { code, type, balance, paypalReference, bonus, numberNavegante }
@@ -198,7 +196,6 @@ async function getUserInformation() {
198196
// get tripHistory
199197
async function getTripHistory(pageNum = 1, pageSize = TRIP_HISTORY_PAGE_SIZE) {
200198
response = await makePostRequest(
201-
GIRA_GRAPHQL_ENDPOINT,
202199
JSON.stringify({
203200
operationName: "tripHistory",
204201
variables: { in: { _pageNum: pageNum, _pageSize: pageSize } },

0 commit comments

Comments
 (0)