|
| 1 | + |
| 2 | +function fetchResponse(uri, options, sucessStatusCode) { |
| 3 | + return fetch(uri, options |
| 4 | + ).then(response => { |
| 5 | + return response.json() |
| 6 | + .then(parsedBody => { |
| 7 | + if (response.status == sucessStatusCode) return parsedBody |
| 8 | + parsedBody.statusCode = response.status; |
| 9 | + throw parsedBody |
| 10 | + }) |
| 11 | + }) |
| 12 | +} |
| 13 | + |
| 14 | +function buildOptions(method, body) { |
| 15 | + const toReturn = {} |
| 16 | + toReturn.method = method |
| 17 | + toReturn.body = JSON.stringify(body) |
| 18 | + toReturn.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } |
| 19 | + return toReturn |
| 20 | +} |
| 21 | + |
| 22 | +function CiborgApiUris() { |
| 23 | + const baseUri = "http://localhost:8080/ciborg/api/" |
| 24 | + const baseUriAuth = `${baseUri}auth/` |
| 25 | + |
| 26 | + this.getPopularGamesUri = () => `${baseUri}games` |
| 27 | + this.getSpecificGameWithNameUri = (name) => `${baseUri}games/${name}` |
| 28 | + this.getGroupsUri = () => `${baseUriAuth}groups` |
| 29 | + this.getSpecificGroupWithIdUri = (id) => `${baseUriAuth}groups/${id}` |
| 30 | + this.putGameIntoGroupUri = (groupId) => `${baseUriAuth}groups/${groupId}/games` |
| 31 | + this.deleteGroupGameUri = (groupId, gameId) => `${baseUriAuth}groups/${groupId}/games/${gameId}` |
| 32 | + this.groupGamesBetweenTimeUri = (queryString, groupId) => `${baseUriAuth}groups/${groupId}/games${queryString}` |
| 33 | + this.createUser = () => `${baseUri}users` |
| 34 | + this.login = () => `${baseUri}login` |
| 35 | + this.logout = () => `${baseUriAuth}logout` |
| 36 | + this.session = () => `${baseUri}session` |
| 37 | +} |
| 38 | + |
| 39 | +const apiUris = new CiborgApiUris(); |
| 40 | + |
| 41 | +module.exports = { |
| 42 | + getPopularGames: function () { |
| 43 | + return fetchResponse(apiUris.getPopularGamesUri(), {}, 200) |
| 44 | + }, |
| 45 | + getSpecificGameWithName: function (name) { |
| 46 | + return fetchResponse(apiUris.getSpecificGameWithNameUri(name), {}, 200) |
| 47 | + }, |
| 48 | + getGroups: function () { |
| 49 | + return fetchResponse(apiUris.getGroupsUri(), {}, 200) |
| 50 | + }, |
| 51 | + getSpecificGroupWithId: function (id) { |
| 52 | + return fetchResponse(apiUris.getSpecificGroupWithIdUri(id), {}, 200) |
| 53 | + .then(data => { return { data, groupId: id } }) |
| 54 | + }, |
| 55 | + postGroup: function (groupsInfo) { |
| 56 | + const options = buildOptions("POST", { name: groupsInfo.name, description: groupsInfo.description }) |
| 57 | + return fetchResponse(apiUris.getGroupsUri(), options, 201); |
| 58 | + }, |
| 59 | + putGameIntoGroup: function (gameGroupInfo) { |
| 60 | + const options = buildOptions("PUT", { idGame: gameGroupInfo.gameId }) |
| 61 | + return fetchResponse(apiUris.putGameIntoGroupUri(gameGroupInfo.groupId), |
| 62 | + options, 201).then(data => gameGroupInfo) |
| 63 | + |
| 64 | + }, |
| 65 | + editGroup: function (groupInfo) { |
| 66 | + const options = buildOptions("PUT", { name: groupInfo.name, description: groupInfo.description }) |
| 67 | + return fetchResponse(apiUris.getSpecificGroupWithIdUri(groupInfo.groupId), |
| 68 | + options, 200) |
| 69 | + .then(data => groupInfo) |
| 70 | + }, |
| 71 | + deleteGroup: function (groupId) { |
| 72 | + const options = buildOptions("DELETE", {}) |
| 73 | + return fetchResponse(apiUris.getSpecificGroupWithIdUri(groupId), options, 200) |
| 74 | + }, |
| 75 | + deleteGroupGame: function (gameGroupInfo) { |
| 76 | + const options = buildOptions("DELETE", {}) |
| 77 | + return fetchResponse(apiUris.deleteGroupGameUri(gameGroupInfo.groupId, gameGroupInfo.gameId), |
| 78 | + options, 200 |
| 79 | + ).then(data => gameGroupInfo) |
| 80 | + }, |
| 81 | + groupGamesBetweenTime: function (queryString,groupId) { |
| 82 | + return fetchResponse(apiUris.groupGamesBetweenTimeUri(queryString, groupId), |
| 83 | + buildOptions("GET"), 200) |
| 84 | + .then(games => fetchResponse(apiUris.getSpecificGroupWithIdUri(groupId), buildOptions("GET"), 200) |
| 85 | + .then(r => { |
| 86 | + r.games = games.games |
| 87 | + return r |
| 88 | + }) |
| 89 | + ).then(data => { return { data, groupId } }) |
| 90 | + }, |
| 91 | + createUser: function (userInfo) { |
| 92 | + const options = buildOptions("POST", { username: userInfo.username, password: userInfo.password }) |
| 93 | + return fetchResponse(apiUris.createUser(), options, 201) |
| 94 | + }, |
| 95 | + login: function (username, password) { |
| 96 | + const options = buildOptions("POST", { username, password }) |
| 97 | + return fetchResponse(apiUris.login(), options, 200) |
| 98 | + |
| 99 | + }, |
| 100 | + logout: function () { |
| 101 | + const options = buildOptions("PUT") |
| 102 | + return fetchResponse(apiUris.logout(), options, 200) |
| 103 | + .then( data => data ) |
| 104 | + }, |
| 105 | + isSessionActive : function () { |
| 106 | + return fetchResponse(apiUris.session(),{},200).then(data => data.status ) |
| 107 | + } |
| 108 | +} |
0 commit comments