all files / src/ Client.js

21.62% Statements 8/37
0% Branches 0/6
0% Functions 0/9
21.62% Lines 8/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                             
'use strict';
 
var log = require('./Log.js').Logger('libZotero:Client');
 
var Fetcher = require('./Fetcher.js');
 
var Client = function(apiKey=''){
	this._apiKey = apiKey;
	this.net = require('./Net.js');
};
 
Client.prototype.getUserGroups = function(userID) {
	var aparams = {
		'target':'userGroups',
		'libraryType':'user',
		'libraryID': userID,
		'order':'title'
	};
	
	if(this._apiKey){
		aparams['key'] = this._apiKey;
	}
	
	return Zotero.ajaxRequest(aparams)
	.then(function(response){
		log.debug('fetchUserGroups proxied callback', 3);
		let groupJson = response.data;
		let groups = groupJson.map(function(groupObj){
			return new Zotero.Group(groupObj);
		});
		
		var fetchedGroups = groups.addGroupsFromJson(response.data);
		response.fetchedGroups = fetchedGroups;
		return response;
	});
};
 
Client.prototype.getUserPublications = function(userID, config={}) {
	log.debug('Zotero.Client.loadPublications', 3);
	
	let defaultConfig = {
		target:'publications',
		start: 0,
		limit: 50,
		order: Zotero.config.defaultSortColumn,
		sort: Zotero.config.defaultSortOrder,
		include: 'bib'
	};
	
	let urlconfig = Z.extend({}, defaultConfig, config, {
		'target':'publications',
		'libraryType':'user',
		'libraryID':userID
	});
	
	let fetcher = new Fetcher(urlconfig);
	return fetcher.fetchAll().then((results)=>{
		return results.map(function(itemObj){
			return new Zotero.Item(itemObj);
		});
	});
};
 
Client.prototype.getKeyPermissions = function(key=false) {
	if(!key){
		return false;
	}
	
	let urlconfig = {'target':'key', 'apiKey':key, 'libraryType':''};
	
	return Zotero.ajaxRequest(urlconfig)
	.then(function(response){
		let keyObject = JSON.parse(response.data);
		return keyObject;
	});
};
 
Client.prototype.deleteKey = function(key=false) {
	if(!key){
		return false;
	}
 
	return this.net.ajax();
};
 
module.exports = Client;