From 99871aa54a5d56ca1217400f0d9cdd68fd68c74d Mon Sep 17 00:00:00 2001 From: BIT Student Date: Tue, 21 Nov 2017 15:16:17 +0100 Subject: [PATCH] zavrsen servis za api --- src/constants.js | 4 +- src/services/communicationService.js | 60 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/services/communicationService.js diff --git a/src/constants.js b/src/constants.js index ca54aec..e72a6b1 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1 +1,3 @@ -// export const SOME_GLOBAL_CONFIG = "value"; \ No newline at end of file +// export const SOME_GLOBAL_CONFIG = "value"; +export const FETCH_ADDRESS = "https://bitbookapi.azurewebsites.net/"; +export const API_KEY = "Y3dvKZsv"; \ No newline at end of file diff --git a/src/services/communicationService.js b/src/services/communicationService.js new file mode 100644 index 0000000..b2c9e49 --- /dev/null +++ b/src/services/communicationService.js @@ -0,0 +1,60 @@ +import { FETCH_ADDRESS } from "../constants"; +import { API_KEY } from "../constants"; +import axios from "axios"; + +class ApiCommunication { + constructor() { + + } + get(address, callbackSuccess, callbackFail) { + + axios.get(FETCH_ADDRESS + address, this.createRequest()) + .then((response) => { + response.json() + .then((response) => { + callbackSuccess(response); + }).catch((reason) => { + callbackFail(reason); + }); + }); + } + post(address, dataObj, callbackSuccess, callbackFail) { + axios.post(FETCH_ADDRESS + address, this.createRequest(dataObj)) + .then((response) => response.json()) + .then((response) => { + callbackSuccess(response); + + }).catch((reason) => { + callbackFail(reason); + }); + + } + getSessionID() { + return sessionStorage.getItem("sessionID"); + + } + createRequest(dataObj = null) { + const sesID = this.getSessionID(); + if (sesID) { + return { + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + "key": API_KEY, + "sessionID": sesID + }, + data: dataObj + }; + }else{ + return { + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + "key": API_KEY + }, + data: dataObj + }; + } + } +} +export default ApiCommunication; \ No newline at end of file