UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 9.58 kB
"use strict";var request=require("request");const queryString=require("query-string");var ConfHelper=require("./conf/conf_helper");const GrafoErrorGlossary=require("./errors/GrafoErrorGlossary"),NonExistentEntityError=require("./errors/NonExistentEntityError");function DocSummaryApi(e,o,r){this.config=e,this.confHelper=new ConfHelper(e),this.sessionCookie=o,this.logger=r}module.exports=DocSummaryApi;const DOCUMENT_NOT_FOUND_ERR_MSG1="Document could not be found",DOCUMENT_NOT_FOUND_ERR_MSG2="Document Not Found";DocSummaryApi.prototype.createDocument=function(e){var o=this;return this.logger.debug("Creating Doc "+e.title+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents",method:"POST",headers:n,form:{parent:e.parent,title:e.title,iri:e.iri,iriPrefix:e.iriPrefix,description:e.description,needForceDirectedLayout:e.needForceDirectedLayout}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in creating Document ",r,t)})})},DocSummaryApi.prototype.getDocument=function(e){var o=this;return this.logger.debug("Fetching Doc Summary for "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/document/"+e,method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching Document ",r,t)})})},DocSummaryApi.prototype.deleteDocument=function(e){var o=this;return new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e,method:"DELETE",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in deleting Document ",r,t)})})},DocSummaryApi.prototype.listDocuments=function(e){var o=this;return this.logger.debug("Grafo Client: List all documents"),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents?"+queryString.stringify(e.getQueryMap()),method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching document list ",r,t)})})},DocSummaryApi.prototype.listSharedWithMeDocuments=function(e){var o=this;return this.logger.debug("Grafo Client: List all shared with me documents"),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/sharedWithMeDocuments?"+queryString.stringify(e.getQueryMap()),method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching shared with me document list ",r,t)})})},DocSummaryApi.prototype.getSharedWithMeDocById=function(e){var o=this;return this.logger.debug("Fetching Shared Doc for "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/sharedWithMeDocuments/"+e,method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching shared document ",r,t)})})},DocSummaryApi.prototype.getDocumentById=function(e){var o=this;return this.logger.debug("Fetching Doc for "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e,method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching document ",r,t)})})},DocSummaryApi.prototype.updateDocument=function(e){var o=this;return this.logger.debug("Update Doc : "+e.id+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e.id,method:"PUT",headers:n,form:{title:e.title,iri:e.iri,iriPrefix:e.iriPrefix,description:e.description}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in updating document ",r,t)})})},DocSummaryApi.prototype.listFolders=function(e){var o=this;return new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/folders?"+queryString.stringify(e.getQueryMap()),method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching folders list",r,t)})})},DocSummaryApi.prototype.getFolder=function(e){var o=this;return this.logger.debug("Fetching Folder for "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/folders/"+e,method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching folder ",r,t)})})},DocSummaryApi.prototype.createFolder=function(e){var o=this;return this.logger.debug("Creating Folder "+e.title+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/folders",method:"POST",headers:n,form:{parent:e.parent,title:e.title}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in creating Folder ",r,t)})})},DocSummaryApi.prototype.updateFolder=function(e){var o=this;return this.logger.debug("Updating Folder "+e.title+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/folders/"+e.id,method:"PUT",headers:n,form:{title:e.title}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in updating folder ",r,t)})})},DocSummaryApi.prototype.deleteFolder=function(e){var o=this;return this.logger.debug("Deleting Folder "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/folders/"+e,method:"DELETE",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in deleting folder ",r,t)})})},DocSummaryApi.prototype.listSharedWithMeFolders=function(e){var o=this;return this.logger.debug("Grafo Client: List all shared with me folders"),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/sharedWithMeFolders?"+queryString.stringify(e.getQueryMap()),method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching all shared with me Folders ",r,t)})})},DocSummaryApi.prototype.getShareFolder=function(e){var o=this;return this.logger.debug("Fetching Share Folder for "+e+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/shareFolders/"+e,method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching Share Folder ",r,t)})})},DocSummaryApi.prototype.createShareFolder=function(e){var o=this;return this.logger.debug("Creating Share Folder "+e.title+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/shareFolders",method:"POST",headers:n,form:{parent:e.parent,title:e.title}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in creating Folder ",r,t)})})},DocSummaryApi.prototype.updateShareFolder=function(e){var o=this;return this.logger.debug("Updating Share Folder "+e.title+" ..."),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/shareFolders/"+e.id,method:"PUT",headers:n,form:{title:e.title}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in Updating Share Folder ",r,t)})})},DocSummaryApi.prototype.listDocumentShare=function(e){var o=this;return this.logger.debug("List shared document"+e),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e+"/shares",method:"GET",headers:n};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in fetching shared documents list ",r,t)})})},DocSummaryApi.prototype.getDocumentShare=function(e,o){var r=this;return this.logger.debug("Get shared document"+e),new Promise(function(t,n){var i={Cookie:r.sessionCookie},s={url:r.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e+"/shares/"+o,method:"GET",headers:i};request(s,function(e,o,i){handleResponse(r,e,o,i,"Problem in fetching shared document ",t,n)})})},DocSummaryApi.prototype.createDocumentShare=function(e,o){var r=this;return this.logger.debug("Sharing document: "+e),new Promise(function(t,n){var i={Cookie:r.sessionCookie},s={url:r.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e+"/shares",method:"POST",headers:i,form:{sharedWithEmail:o.sharedWithEmail,permissions:o.permissions,message:o.message}};request(s,function(e,o,i){handleResponse(r,e,o,i,"Problem in sharing document ",t,n)})})},DocSummaryApi.prototype.updateDocumentShare=function(e){var o=this;return this.logger.debug("Update shared document"+e.documentId),new Promise(function(r,t){var n={Cookie:o.sessionCookie},i={url:o.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e.documentId+"/shares/"+e.id,method:"PUT",headers:n,form:{sharedWithEmail:e.sharedWithEmail,permissions:e.permissions}};request(i,function(e,n,i){handleResponse(o,e,n,i,"Problem in updating share document ",r,t)})})},DocSummaryApi.prototype.deleteDocumentShare=function(e,o){var r=this;return this.logger.debug("Unshare doc"+e),new Promise(function(t,n){var i={Cookie:r.sessionCookie},s={url:r.confHelper.getBaseRestUrl()+"/api/v1/documents/"+e+"/shares/"+o,method:"DELETE",headers:i};request(s,function(e,o,i){handleResponse(r,e,o,i,"Problem in unsharing document ",t,n)})})};var handleResponse=function(e,o,r,t,n,i,s){if(o)e.logger.error(o.stack),s(new Error(n));else if(200===r.statusCode){i(JSON.parse(r.body))}else{var a=JSON.parse(r.body).message?JSON.parse(r.body).message:JSON.parse(r.body).error;if(a.includes(DOCUMENT_NOT_FOUND_ERR_MSG1)||a.includes("Document Not Found")){var u=new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,DOCUMENT_NOT_FOUND_ERR_MSG1);e.logger.error(u),s(u)}else{var l="HTTP error code: "+r.statusCode+", Message: "+a;e.logger.error(l),s(new Error(l))}}};