From cbd6523a4fb8dc83ac67ad5665743fbc5ef5932d Mon Sep 17 00:00:00 2001 From: Arman R Date: Tue, 29 Sep 2020 22:36:28 -0700 Subject: [PATCH 1/4] Add basic API Wrapper --- client/package.json | 1 + client/src/utils/apiWrapper.js | 19 +++++++++++++++++++ client/yarn.lock | 9 ++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 client/src/utils/apiWrapper.js diff --git a/client/package.json b/client/package.json index f527540..edaa562 100644 --- a/client/package.json +++ b/client/package.json @@ -15,6 +15,7 @@ "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", + "axios": "^0.20.0", "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.3" diff --git a/client/src/utils/apiWrapper.js b/client/src/utils/apiWrapper.js new file mode 100644 index 0000000..f0d7df6 --- /dev/null +++ b/client/src/utils/apiWrapper.js @@ -0,0 +1,19 @@ +import axios from "axios"; + +const BASE_URL = "http://localhost:5000/"; + +export const getStudents = () => { + /** + * Returns all students + * Returns GET_STUDENTS_FAIL upon failure + */ + const requestString = `${BASE_URL}students`; + + return axios + .get(requestString, { + headers: { + "Content-Type": "application/JSON", + }, + }) + .catch((error) => error({ type: "GET_STUDENTS_FAIL", error })); +}; diff --git a/client/yarn.lock b/client/yarn.lock index 5eafb46..7b29536 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -2332,6 +2332,13 @@ axe-core@^3.5.4: resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-3.5.5.tgz#84315073b53fa3c0c51676c588d59da09a192227" integrity sha512-5P0QZ6J5xGikH780pghEdbEKijCTrruK9KxtPZCFWUpef0f6GipO+xEZ5GKCb020mmqgbiNO6TcA55CriL784Q== +axios@^0.20.0: + version "0.20.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.20.0.tgz#057ba30f04884694993a8cd07fa394cff11c50bd" + integrity sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA== + dependencies: + follow-redirects "^1.10.0" + axobject-query@^2.0.2, axobject-query@^2.1.2: version "2.2.0" resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be" @@ -4833,7 +4840,7 @@ flush-write-stream@^1.0.0: inherits "^2.0.3" readable-stream "^2.3.6" -follow-redirects@^1.0.0: +follow-redirects@^1.0.0, follow-redirects@^1.10.0: version "1.13.0" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db" integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA== From 9318b2f3d510d53322eb18a23020561d5863ea96 Mon Sep 17 00:00:00 2001 From: Arman R Date: Tue, 29 Sep 2020 22:42:02 -0700 Subject: [PATCH 2/4] Add sample API Wrapper --- client/src/utils/apiWrapper.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/client/src/utils/apiWrapper.js b/client/src/utils/apiWrapper.js index f0d7df6..8c24d33 100644 --- a/client/src/utils/apiWrapper.js +++ b/client/src/utils/apiWrapper.js @@ -1,6 +1,7 @@ import axios from "axios"; const BASE_URL = "http://localhost:5000/"; +const BASE_URL_TEST = "https://swapi.dev/api/"; // URL for Sample API export const getStudents = () => { /** @@ -15,5 +16,32 @@ export const getStudents = () => { "Content-Type": "application/JSON", }, }) - .catch((error) => error({ type: "GET_STUDENTS_FAIL", error })); + .catch((error) => + error({ + type: "GET_STUDENTS_FAIL", + error, + }) + ); +}; + +// Sample API Wrapper +export const getPeople = () => { + /** + * Returns all people + * Returns GET_PEOPLE_FAIL upon failure + */ + const requestString = `${BASE_URL_TEST}people`; + + return axios + .get(requestString, { + headers: { + "Content-Type": "application/JSON", + }, + }) + .catch((error) => + error({ + type: "GET_PEOPLE_FAIL", + error, + }) + ); }; From 4a8c888637a91414ff76b56d13dae013f4cd807c Mon Sep 17 00:00:00 2001 From: Arman R Date: Tue, 29 Sep 2020 22:46:07 -0700 Subject: [PATCH 3/4] Add sample API Request for demoing --- client/src/App.jsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/src/App.jsx b/client/src/App.jsx index 7b794e4..a3b03a5 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,6 +1,14 @@ import React from "react"; import logo from "./logo.svg"; import "./styles/App.css"; +import { getPeople } from "../src/utils/apiWrapper"; + +const testAPIWrapper = async () => { + const people = await getPeople(); + console.log(people); +}; + +testAPIWrapper(); function App() { return ( From c02b450707d5b3ba864cdd931c5ee44e286c44f9 Mon Sep 17 00:00:00 2001 From: Arman R Date: Thu, 1 Oct 2020 15:32:10 -0700 Subject: [PATCH 4/4] Remove sample API request --- client/src/App.jsx | 8 -------- client/src/utils/apiWrapper.js | 23 ----------------------- 2 files changed, 31 deletions(-) diff --git a/client/src/App.jsx b/client/src/App.jsx index a3b03a5..7b794e4 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,14 +1,6 @@ import React from "react"; import logo from "./logo.svg"; import "./styles/App.css"; -import { getPeople } from "../src/utils/apiWrapper"; - -const testAPIWrapper = async () => { - const people = await getPeople(); - console.log(people); -}; - -testAPIWrapper(); function App() { return ( diff --git a/client/src/utils/apiWrapper.js b/client/src/utils/apiWrapper.js index 8c24d33..5c1f4cb 100644 --- a/client/src/utils/apiWrapper.js +++ b/client/src/utils/apiWrapper.js @@ -1,7 +1,6 @@ import axios from "axios"; const BASE_URL = "http://localhost:5000/"; -const BASE_URL_TEST = "https://swapi.dev/api/"; // URL for Sample API export const getStudents = () => { /** @@ -23,25 +22,3 @@ export const getStudents = () => { }) ); }; - -// Sample API Wrapper -export const getPeople = () => { - /** - * Returns all people - * Returns GET_PEOPLE_FAIL upon failure - */ - const requestString = `${BASE_URL_TEST}people`; - - return axios - .get(requestString, { - headers: { - "Content-Type": "application/JSON", - }, - }) - .catch((error) => - error({ - type: "GET_PEOPLE_FAIL", - error, - }) - ); -};