Skip to content

Commit

Permalink
choreL add api service
Browse files Browse the repository at this point in the history
  • Loading branch information
tangimds committed Jun 3, 2022
1 parent 9084db9 commit c1c5584
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 16 deletions.
13 changes: 0 additions & 13 deletions api/.vscode/settings.json

This file was deleted.

6 changes: 3 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "api_mano",
"version": "1.79.4",
"mobileAppVersion": "1.6.0",
"name": "api_monsuivipsy",
"version": "0.1.0",
"mobileAppVersion": "1.27.2",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"date-fns": "^2.16.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"fetch-retry": "^5.0.2",
"fs": "^0.0.1-security",
"ip-anonymize": "^0.1.0",
"prettier": "^2.5.1",
Expand All @@ -61,6 +62,7 @@
"react-native-swiper": "^1.6.0",
"react-native-tab-view": "^2.15.2",
"react-native-version-check": "^3.4.2",
"urijs": "^1.19.11",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
97 changes: 97 additions & 0 deletions app/src/services/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import URI from "urijs";
import { Alert, Linking, Platform } from "react-native";
import NetInfo from "@react-native-community/netinfo";
import fetchRetry from "fetch-retry";
import deviceInfoModule from "react-native-device-info";

// todo : clean 👇
import { SCHEME, MW_API_HOST } from "../config";
import matomo from "./matomo";

const checkNetwork = async (test = false) => {
const isConnected = await NetInfo.fetch().then((state) => state.isConnected);
if (!isConnected || test) {
await new Promise((res) => setTimeout(res, 1500));
Alert.alert("Pas de réseau", "Veuillez vérifier votre connexion");
return false;
}
return true;
};

class ApiService {
// todo : clean 👇
host = MW_API_HOST;
scheme = SCHEME;
fetch = fetchRetry(fetch);
getUrl = (path, query) => {
return new URI().host(this.host).scheme(this.scheme).path(path).setSearch(query).toString();
};
execute = async ({ method = "GET", path = "", query = {}, headers = {}, body = null }) => {
try {
const config = {
method,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
appversion: deviceInfoModule.getBuildNumber(),
appdevice: Platform.OS,
currentroute: this.navigation?.getCurrentRoute?.()?.name,
...headers,
},
body: body ? JSON.stringify(body) : null,
retries: 3,
retryDelay: 1000,
};

const url = this.getUrl(path, query);
console.log("url: ", url);
const canFetch = await checkNetwork();
if (!canFetch) return;

const response = await this.fetch(url, config);

if (response.json) {
const readableRes = await response.json();
if (readableRes.sendInApp) this.handleInAppMessage(readableRes.sendInApp);
return readableRes;
}

return response;
} catch (e) {
return {
ok: false,
error:
"Veuillez nous excuser, cette erreur est inattendue : l'équipe technique a été prévenue. Veuillez retenter dans quelques instants ou nous contacter si l'erreur persiste.",
};
}
};

get = async (args) => this.execute({ method: "GET", ...args });
post = async (args) => this.execute({ method: "POST", ...args });
put = async (args) => this.execute({ method: "PUT", ...args });
delete = async (args) => this.execute({ method: "DELETE", ...args });

handleInAppMessage = (inAppMessage) => {
const [title, subTitle, actions = [], options = {}] = inAppMessage;
if (!actions || !actions.length) return Alert.alert(title, subTitle);
const actionsWithNavigation = actions.map((action) => {
if (action.navigate) {
action.onPress = () => {
API.navigation.navigate(...action.navigate);
if (action.event) matomo.logEvent(action.event);
};
}
if (action.link) {
action.onPress = () => {
Linking.openURL(action.link);
if (action.event) matomo.logEvent(action.event);
};
}
return action;
});
Alert.alert(title, subTitle, actionsWithNavigation, options);
};
}

const API = new ApiService();
export default API;
10 changes: 10 additions & 0 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,11 @@ fbjs@^3.0.0:
setimmediate "^1.0.5"
ua-parser-js "^0.7.30"

fetch-retry@^5.0.2:
version "5.0.2"
resolved "https://registry.yarnpkg.com/fetch-retry/-/fetch-retry-5.0.2.tgz#4c55663a7c056cb45f182394e479464f0ff8f3e3"
integrity sha512-57Hmu+1kc6pKFUGVIobT7qw3NeAzY/uNN26bSevERLVvf6VGFR/ooDCOFBHMNDgAxBiU2YJq1D0vFzc6U1DcPw==

figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
Expand Down Expand Up @@ -7801,6 +7806,11 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"

urijs@^1.19.11:
version "1.19.11"
resolved "https://registry.yarnpkg.com/urijs/-/urijs-1.19.11.tgz#204b0d6b605ae80bea54bea39280cdb7c9f923cc"
integrity sha512-HXgFDgDommxn5/bIv0cnQZsPhHDA90NPHD6+c/v21U5+Sx5hoP8+dP9IZXBU1gIfvdRfhG8cel9QNPeionfcCQ==

urix@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
Expand Down

0 comments on commit c1c5584

Please sign in to comment.