#39 Moves API endpoints to a constant file
Merged 2 years ago by lbrabec. Opened 3 years ago by manishakanyal.
fedora-qa/ manishakanyal/landingpage EndpointConstant  into  master

file modified
+2 -2
@@ -1,4 +1,4 @@ 

  window.env = {

-     SUBDIR: '/',

-     ORACULUM_API_URL_v1: 'https://packager-dashboard.fedoraproject.org/api/v1/'

+   SUBDIR: "/",

+   ORACULUM_API_URL_v1: "https://packager-dashboard.fedoraproject.org/api/v1",

  };

file modified
+53 -56
@@ -4,76 +4,73 @@ 

    MAX_RETRY_LIMIT,

    FIRST_RETRY_GAP,

  } from "../constants/ProjectConstants"

+ import { endpoints } from "../constants/endpoints"

  

  export const loadDataResp = (payload) => ({

    type: ActionTypes.LOAD_DATA_RESP,

    payload: payload,

  })

  

- export const loadData = (

-   payload,

-   retryLimit = MAX_RETRY_LIMIT,

-   secondsToWait = FIRST_RETRY_GAP

- ) => (dispatch) => {

-   dispatch({

-     type: ActionTypes.LOAD_DATA,

-     payload: payload,

-   })

-   dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.STARTED))

-   fetch(window.env.ORACULUM_API_URL_v1 + "landing_page")

-     .then((blob) => blob.json())

-     .then((data) => {

-       dispatch(loadDataResp(data))

-       dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED))

+ export const loadData =

+   (payload, retryLimit = MAX_RETRY_LIMIT, secondsToWait = FIRST_RETRY_GAP) =>

+   (dispatch) => {

+     dispatch({

+       type: ActionTypes.LOAD_DATA,

+       payload: payload,

      })

-     .catch((error) => {

-       if (retryLimit > 0) {

-         dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.RETRYING))

-         console.log("Failed to fetch, retrying", MAX_RETRY_LIMIT - retryLimit)

-         window.setTimeout(() => {

-           dispatch(loadData(payload, retryLimit - 1, secondsToWait * 2))

-         }, secondsToWait * 1000)

-       } else {

-         dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.FAILED))

-         console.error("Error:", error)

-       }

-     })

- }

+     dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.STARTED))

+     fetch(endpoints.landingPage)

+       .then((blob) => blob.json())

+       .then((data) => {

+         dispatch(loadDataResp(data))

+         dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED))

+       })

+       .catch((error) => {

+         if (retryLimit > 0) {

+           dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.RETRYING))

+           console.log("Failed to fetch, retrying", MAX_RETRY_LIMIT - retryLimit)

+           window.setTimeout(() => {

+             dispatch(loadData(payload, retryLimit - 1, secondsToWait * 2))

+           }, secondsToWait * 1000)

+         } else {

+           dispatch(updateLandingPageApiCallStatus(API_CALL_STATUS_VALUES.FAILED))

+           console.error("Error:", error)

+         }

+       })

+   }

  

  export const loadWizardDataResp = (payload) => ({

    type: ActionTypes.LOAD_WIZARD_DATA_RESP,

    payload: payload,

  })

  

- export const loadWizardData = (

-   payload,

-   retryLimit = MAX_RETRY_LIMIT,

-   secondsToWait = FIRST_RETRY_GAP

- ) => (dispatch) => {

-   dispatch({

-     type: ActionTypes.LOAD_WIZARD_DATA,

-     payload: payload,

-   })

-   dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.STARTED))

-   fetch(window.env.ORACULUM_API_URL_v1 + "actions/all")

-     .then((blob) => blob.json())

-     .then((data) => {

-       dispatch(loadWizardDataResp(data))

-       dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED))

-     })

-     .catch((error) => {

-       if (retryLimit > 0) {

-         dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.RETRYING))

-         console.log("Failed to fetch, retrying", MAX_RETRY_LIMIT - retryLimit)

-         window.setTimeout(() => {

-           dispatch(loadWizardData(payload, retryLimit - 1, secondsToWait * 2))

-         }, secondsToWait * 1000)

-       } else {

-         dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.FAILED))

-         console.error("Error:", error)

-       }

+ export const loadWizardData =

+   (payload, retryLimit = MAX_RETRY_LIMIT, secondsToWait = FIRST_RETRY_GAP) =>

+   (dispatch) => {

+     dispatch({

+       type: ActionTypes.LOAD_WIZARD_DATA,

+       payload: payload,

      })

- }

+     dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.STARTED))

+     fetch(endpoints.allActions)

+       .then((blob) => blob.json())

+       .then((data) => {

+         dispatch(loadWizardDataResp(data))

+         dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.FULFILLED))

+       })

+       .catch((error) => {

+         if (retryLimit > 0) {

+           dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.RETRYING))

+           console.log("Failed to fetch, retrying", MAX_RETRY_LIMIT - retryLimit)

+           window.setTimeout(() => {

+             dispatch(loadWizardData(payload, retryLimit - 1, secondsToWait * 2))

+           }, secondsToWait * 1000)

+         } else {

+           dispatch(updateWizardApiCallStatus(API_CALL_STATUS_VALUES.FAILED))

+           console.error("Error:", error)

+         }

+       })

+   }

  

  export const updateLandingPageApiCallStatus = (status) => ({

    type: ActionTypes.UPDATE_LANDING_PAGE_API_CALL_STATUS,

@@ -0,0 +1,6 @@ 

+ export const API_URL = window.env.ORACULUM_API_URL_v1;

+ 

+ export const endpoints = {

+   landingPage: `${API_URL}/landing_page`,

+   allActions: `${API_URL}/actions/all`,

+ };

This PR moves API endpoints to a constant file for better reusability of the same endpoints and error-free consistency.

Metadata Update from @lbrabec:
- Pull-request tagged with: review done

2 years ago

rebased onto 1a1360236b2b74ff50c63cd2df7e5a901f7fafe7

2 years ago

@lbrabec, I have resolved the merge conflict, please have a look.

rebased onto 81dcd5b

2 years ago

Pull-Request has been merged by lbrabec

2 years ago