Application Android et IOS pour l'amicale des élèves
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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. type response_format = {
  16. error: number,
  17. data: Object,
  18. }
  19. const API_ENDPOINT = "https://www.amicale-insat.fr/api/";
  20. /**
  21. * Sends a request to the Amicale Website backend
  22. *
  23. * In case of failure, the promise will be rejected with the error code.
  24. * In case of success, the promise will return the data object.
  25. *
  26. * @param path The API path from the API endpoint
  27. * @param method The HTTP method to use (GET or POST)
  28. * @param params The params to use for this request
  29. * @returns {Promise<R>}
  30. */
  31. export async function apiRequest(path: string, method: string, params: ?{ [key: string]: string }) {
  32. if (params === undefined || params === null)
  33. params = {};
  34. return new Promise((resolve, reject) => {
  35. fetch(API_ENDPOINT + path, {
  36. method: method,
  37. headers: new Headers({
  38. 'Accept': 'application/json',
  39. 'Content-Type': 'application/json',
  40. }),
  41. body: JSON.stringify({
  42. ...params
  43. })
  44. }).then(async (response) => response.json())
  45. .then((response: response_format) => {
  46. if (isResponseValid(response)) {
  47. if (response.error === ERROR_TYPE.SUCCESS)
  48. resolve(response.data);
  49. else
  50. reject(response.error);
  51. } else
  52. reject(ERROR_TYPE.SERVER_ERROR);
  53. })
  54. .catch(() => {
  55. reject(ERROR_TYPE.CONNECTION_ERROR);
  56. });
  57. });
  58. }
  59. /**
  60. * Checks if the given API response is valid.
  61. *
  62. * For a request to be valid, it must match the response_format as defined in this file.
  63. *
  64. * @param response
  65. * @returns {boolean}
  66. */
  67. export function isResponseValid(response: response_format) {
  68. let valid = response !== undefined
  69. && response.error !== undefined
  70. && typeof response.error === "number";
  71. valid = valid
  72. && response.data !== undefined
  73. && typeof response.data === "object";
  74. return valid;
  75. }
  76. /**
  77. * Reads data from the given url and returns it.
  78. *
  79. * Only use this function for non API calls.
  80. * For Amicale API calls, please use the apiRequest function.
  81. *
  82. * If no data was found, returns an empty object
  83. *
  84. * @param url The urls to fetch data from
  85. * @return {Promise<Object>}
  86. */
  87. export async function readData(url: string) {
  88. return new Promise((resolve, reject) => {
  89. fetch(url)
  90. .then(async (response) => response.json())
  91. .then((data) => {
  92. resolve(data);
  93. })
  94. .catch(() => {
  95. reject();
  96. });
  97. });
  98. }