Skip to content

Commit

Permalink
Use a wrapper function for Gmail API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed May 14, 2024
1 parent 734c1e6 commit 44cda34
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
2 changes: 2 additions & 0 deletions lib/api-client/base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class BaseClient {
this.accountObject = this.options.accountObject;
this.accountLogger = this.options.accountLogger;
this.redis = this.options.redis;

this.call = this.options.call;
this.logger = this.options.logger || logger;

this.secret = this.options.secret;
Expand Down
49 changes: 29 additions & 20 deletions lib/api-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ class GmailClient extends BaseClient {
super(account, options);
}

async request(...args) {
let result, accessToken;
try {
accessToken = await this.getToken();
} catch (err) {
console.log(err);
throw err;
}

try {
result = await await this.oAuth2Client.request(accessToken, ...args);
} catch (err) {
console.log(err);
throw err;
}

return result;
}

// PUBLIC METHODS

async listMailboxes(options) {
Expand All @@ -186,15 +205,15 @@ class GmailClient extends BaseClient {
console.log(1);
const accessToken = await this.getToken();
console.log(2, accessToken);
let labelsResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels`);
let labelsResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels`);
console.log(3, labelsResult);
let labels = labelsResult.labels.filter(label => !SKIP_LABELS.includes(label.id));

let resultLabels;
if (options && options.counters) {
resultLabels = [];
for (let label of labels) {
let labelResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels/${label.id}`);
let labelResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels/${label.id}`);
resultLabels.push(labelResult);
}
} else {
Expand Down Expand Up @@ -291,7 +310,7 @@ class GmailClient extends BaseClient {
.join('/')
.replace(/^INBOX(\/|$)/gi, 'INBOX');

let labelsResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/labels`);
let labelsResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/labels`);
let label = labelsResult.labels.find(entry => entry.name === path || entry.id === path);
if (!label) {
return false;
Expand All @@ -305,7 +324,7 @@ class GmailClient extends BaseClient {
if (query.search.emailId) {
// Return only a single matching email

let messageEntry = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${query.search.emailId}`, 'get', {
let messageEntry = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${query.search.emailId}`, 'get', {
format: 'full'
});
if (messageEntry) {
Expand Down Expand Up @@ -394,7 +413,7 @@ class GmailClient extends BaseClient {

console.log('REQUEST QUERY', requestQuery);

let listingResult = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages`, 'get', requestQuery);
let listingResult = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages`, 'get', requestQuery);
let messageCount = listingResult.resultSizeEstimate;

let pages = Math.ceil(messageCount / pageSize);
Expand Down Expand Up @@ -445,24 +464,20 @@ class GmailClient extends BaseClient {
async getRawMessage(messageId) {
await this.prepare();

const accessToken = await this.getToken();

const requestQuery = {
format: 'raw'
};
const result = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);
const result = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

return result?.raw ? Buffer.from(result?.raw, 'base64url') : null;
}

async deleteMessage(messageId /*, force*/) {
await this.prepare();

const accessToken = await this.getToken();

// Move to trash
const url = `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}/trash`;
const result = await this.oAuth2Client.request(accessToken, url, 'post', Buffer.alloc(0));
const result = await this.request(url, 'post', Buffer.alloc(0));

return {
deleted: result && result.labelIds?.includes('TRASH'),
Expand All @@ -478,8 +493,6 @@ class GmailClient extends BaseClient {

await this.prepare();

const accessToken = await this.getToken();

const requestQuery = {
format: 'minimal'
};
Expand All @@ -502,7 +515,7 @@ class GmailClient extends BaseClient {
label = path;
}

const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);
const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

console.log({ label, p: path.toUpperCase(), s: SYSTEM_NAMES[path.toUpperCase()] });

Expand Down Expand Up @@ -554,12 +567,10 @@ class GmailClient extends BaseClient {
options = options || {};
await this.prepare();

const accessToken = await this.getToken();

const requestQuery = {
format: 'full'
};
const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);
const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

let result = this.formatMessage(messageData, { extended: true, textType: options.textType });

Expand All @@ -575,8 +586,6 @@ class GmailClient extends BaseClient {
options = options || {};
await this.prepare();

const accessToken = await this.getToken();

const [messageId, textParts] = msgpack.decode(Buffer.from(textId, 'base64url'));

const bodyParts = new Map();
Expand All @@ -592,7 +601,7 @@ class GmailClient extends BaseClient {
const requestQuery = {
format: 'full'
};
const messageData = await this.oAuth2Client.request(accessToken, `${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);
const messageData = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${messageId}`, 'get', requestQuery);

const response = {};

Expand Down
3 changes: 0 additions & 3 deletions lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ class IMAPConnection extends BaseClient {
this.isClosing = false;
this.isClosed = false;

this.call = options.call;

this.logger = this.getLogger();

this.imapConfig = {
Expand All @@ -122,7 +120,6 @@ class IMAPConnection extends BaseClient {

this.mailboxes = new Map();
this.untaggedExistsTimer = false;
this.redis = options.redis;

this.notifyQueue = options.notifyQueue;
this.submitQueue = options.submitQueue;
Expand Down
3 changes: 2 additions & 1 deletion lib/routes-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ function formatAccountData(account) {
case 'init':
account.stateLabel = {
type: 'info',
name: 'Initializing'
name: 'Initializing',
spinner: true
};
break;

Expand Down
3 changes: 2 additions & 1 deletion static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ document.addEventListener('DOMContentLoaded', () => {
case 'init':
stateLabel = {
type: 'info',
name: 'Initializing'
name: 'Initializing',
spinner: true
};
break;
case 'connecting':
Expand Down

0 comments on commit 44cda34

Please sign in to comment.