Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebData.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @flow
  2. export const ERROR_TYPE = {
  3. SUCCESS: 0,
  4. BAD_CREDENTIALS: 1,
  5. BAD_TOKEN: 2,
  6. NO_CONSENT: 3,
  7. TOKEN_SAVE: 4,
  8. TOKEN_RETRIEVE: 5,
  9. BAD_INPUT: 400,
  10. FORBIDDEN: 403,
  11. CONNECTION_ERROR: 404,
  12. SERVER_ERROR: 500,
  13. UNKNOWN: 999,
  14. };
  15. export type ApiDataLoginType = {
  16. token: string,
  17. };
  18. // eslint-disable-next-line flowtype/no-weak-types
  19. export type ApiGenericDataType = {[key: string]: any};
  20. type ApiResponseType = {
  21. error: number,
  22. data: ApiGenericDataType,
  23. };
  24. const API_ENDPOINT = 'https://www.amicale-insat.fr/api/';
  25. /**
  26. * Checks if the given API response is valid.
  27. *
  28. * For a request to be valid, it must match the response_format as defined in this file.
  29. *
  30. * @param response
  31. * @returns {boolean}
  32. */
  33. export function isApiResponseValid(response: ApiResponseType): boolean {
  34. return (
  35. response != null &&
  36. response.error != null &&
  37. typeof response.error === 'number' &&
  38. response.data != null &&
  39. typeof response.data === 'object'
  40. );
  41. }
  42. /**
  43. * Sends a request to the Amicale Website backend
  44. *
  45. * In case of failure, the promise will be rejected with the error code.
  46. * In case of success, the promise will return the data object.
  47. *
  48. * @param path The API path from the API endpoint
  49. * @param method The HTTP method to use (GET or POST)
  50. * @param params The params to use for this request
  51. * @returns {Promise<ApiGenericDataType>}
  52. */
  53. export async function apiRequest(
  54. path: string,
  55. method: string,
  56. params?: {...},
  57. ): Promise<ApiGenericDataType> {
  58. return new Promise(
  59. (
  60. resolve: (data: ApiGenericDataType) => void,
  61. reject: (error: number) => void,
  62. ) => {
  63. let requestParams = {};
  64. if (params != null) requestParams = {...params};
  65. fetch(API_ENDPOINT + path, {
  66. method,
  67. headers: new Headers({
  68. Accept: 'application/json',
  69. 'Content-Type': 'application/json',
  70. }),
  71. body: JSON.stringify(requestParams),
  72. })
  73. .then(async (response: Response): Promise<ApiResponseType> =>
  74. response.json(),
  75. )
  76. .then((response: ApiResponseType) => {
  77. if (isApiResponseValid(response)) {
  78. if (response.error === ERROR_TYPE.SUCCESS) resolve(response.data);
  79. else reject(response.error);
  80. } else reject(ERROR_TYPE.SERVER_ERROR);
  81. })
  82. .catch((): void => reject(ERROR_TYPE.CONNECTION_ERROR));
  83. },
  84. );
  85. }
  86. /**
  87. * Reads data from the given url and returns it.
  88. *
  89. * Only use this function for non API calls.
  90. * For Amicale API calls, please use the apiRequest function.
  91. *
  92. * If no data was found, returns an empty object
  93. *
  94. * @param url The urls to fetch data from
  95. * @return Promise<any>
  96. */
  97. // eslint-disable-next-line flowtype/no-weak-types
  98. export async function readData(url: string): Promise<any> {
  99. // eslint-disable-next-line flowtype/no-weak-types
  100. return new Promise((resolve: (response: any) => void, reject: () => void) => {
  101. fetch(url)
  102. // eslint-disable-next-line flowtype/no-weak-types
  103. .then(async (response: Response): Promise<any> => response.json())
  104. // eslint-disable-next-line flowtype/no-weak-types
  105. .then((data: any): void => resolve(data))
  106. .catch((): void => reject());
  107. });
  108. }