Skip to content

Commit

Permalink
Tested and working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin McCormick committed Nov 5, 2016
1 parent 7097baa commit 8d3ad0b
Showing 1 changed file with 91 additions and 91 deletions.
182 changes: 91 additions & 91 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,105 @@ const axios = require('axios');

module.exports = class Autopilot {
constructor(apiKey) {
this.api = axios.create({
const api = axios.create({
baseURL: 'https://api2.autopilothq.com/v1/',
headers: {
autopilotapikey: apiKey,
Accept: 'application/json'
}
});

this.contacts = {
upsert: (data, callback) => {
const key = Array.isArray(data) ? 'contacts' : 'contact';

return this.handle(api.post(key, { [key]: data }), callback);
},
list: (bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(api.get(`contacts${append}`), callback);
},
get: (contactId, callback) => {
return this.handle(api.get(`contact/${contactId}`), callback);
},
delete: (contactId, callback) => {
return this.handle(api.delete(`contact/${contactId}`), callback);
},
unsubscribe: (contactId, callback) => {
return this.handle(api.post(`contact/${contactId}/unsubscribe`), callback);
},
fields: () => {
return this.handle(api.get(`contacts/custom_fields`), callback);
}
};

this.journeys = {
list: (callback) => {
return this.handle(api.get('triggers'), callback);
},
add: (triggerId, contactId, callback) => {
return this.handle(api.post(`trigger/${triggerId}/contact/${contactId}`), callback);
}
};

this.lists = {
list: (callback) => {
return this.handle(api.get('lists'), callback);
},
insert: (name, callback) => {
return this.handle(api.post('list', { name }), callback);
},
roster: (listId, bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(api.get(`list/${listId}/contacts${append}`), callback);
},
has: (listId, contactId, callback) => {
return this.handle(api.get(`list/${listId}/contact/${contactId}`), callback);
},
add: (listId, contactId, callback) => {
return this.handle(api.post(`list/${listId}/contact/${contactId}`), callback);
},
remove: (listId, contactId, callback) => {
return this.handle(api.delete(`list/${listId}/contact/${contactId}`), callback);
}
};

this.smartSegments = {
list: (callback) => {
return this.handle(api.get('smart_segments'), callback);
},
roster: (smartSegmentId, bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(api.get(`smart_segments/${smartSegmentId}/contacts${append}`), callback);
}
};

this.account = {
get: (callback) => {
return this.handle(api.delete(`account`), callback);
}
};
}

handle(callback, result) {
handle(result, callback) {
if (callback) {
result
.then(res => callback(null, res))
Expand All @@ -26,93 +115,4 @@ module.exports = class Autopilot {

return result;
}

contacts = {
upsert: (data, callback) => {
const key = Array.isArray(data) ? 'contacts' : 'contact';

return this.handle(this.api.post(key, { [key]: data }), callback);
},
list: (bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(this.api.get(`contacts${append}`), callback);
},
get: (contactId, callback) => {
return this.handle(this.api.get(`contact/${contactId}`), callback);
},
delete: (contactId, callback) => {
return this.handle(this.api.delete(`contact/${contactId}`), callback);
},
unsubscribe: (contactId, callback) => {
return this.handle(this.api.post(`contact/${contactId}/unsubscribe`), callback);
},
fields: () => {
return this.handle(this.api.get(`contacts/custom_fields`), callback);
}
};

journeys = {
list: (callback) => {
return this.handle(this.api.get('triggers'), callback);
},
add: (triggerId, contactId, callback) => {
return this.handle(this.api.post(`trigger/${triggerId}/contact/${contactId}`), callback);
}
};

lists = {
list: (callback) => {
return this.handle(this.api.get('lists'), callback);
},
insert: (name, callback) => {
return this.handle(this.api.post('list', { name }), callback);
},
roster: (listId, bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(this.api.get(`list/${listId}/contacts${append}`), callback);
},
has: (listId, contactId, callback) => {
return this.handle(this.api.get(`list/${listId}/contact/${contactId}`), callback);
},
add: (listId, contactId, callback) => {
return this.handle(this.api.post(`list/${listId}/contact/${contactId}`), callback);
},
remove: (listId, contactId, callback) => {
return this.handle(this.api.delete(`list/${listId}/contact/${contactId}`), callback);
}
};

smartSegments = {
list: (callback) => {
return this.handle(this.api.get('smart_segments'), callback);
},
roster: (smartSegmentId, bookmark, callback) => {
if (typeof bookmark === 'function') {
callback = bookmark;
bookmark = undefined;
}

const append = bookmark ? `/${bookmark}` : '';

return this.handle(this.api.get(`smart_segments/${smartSegmentId}/contacts${append}`), callback);
}
};

account = {
get: (callback) => {
return this.handle(this.api.delete(`account`), callback);
}
};
};

0 comments on commit 8d3ad0b

Please sign in to comment.