Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/version/1.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	package.json
  • Loading branch information
drobnikj committed Aug 24, 2021
2 parents 6574085 + 3bd277d commit 0f312ac
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
push:
branches:
- master
# This will allow releasing beta versions from non-master releases.
# TODO: We need to release this version with other tags than beta. It can mess up the beta version as it will override it with older code.
- version/**
# A release via GitHub releases will deploy a latest version
release:
types: [ published ]
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
===================
- Laid out base for migration to TypeScript

1.4.1 / 2021/08/24
===================
- Changed passing token using request HTTP header instead of the request parameter for every API calls

1.3.0 / 2021/07/15
===================
- Added new method `.test()` to the `WebhookClient` class
Expand Down
7 changes: 7 additions & 0 deletions src/http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class HttpClient {
* @param {function[]} options.requestInterceptors
* @param {number} options.timeoutSecs
* @param {object} options.logger
* @param {string} [options.token]
*/
constructor(options) {
const { token } = options;
this.stats = options.apifyClientStats;
this.maxRetries = options.maxRetries;
this.minDelayBetwenRetriesMillis = options.minDelayBetweenRetriesMillis;
Expand Down Expand Up @@ -90,6 +92,11 @@ class HttpClient {
this.axios.defaults.headers['User-Agent'] = userAgent;
}

// Attach Authorization header for all requests if token was provided
if (token) {
this.axios.defaults.headers.Authorization = `Bearer ${token}`;
}

requestInterceptors.forEach((i) => this.axios.interceptors.request.use(i));
this.userProvidedRequestInterceptors.forEach((i) => this.axios.interceptors.request.use(i));
responseInterceptors.forEach((i) => this.axios.interceptors.response.use(i));
Expand Down
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class ApifyClient {

const tempBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, baseUrl.length - 1) : baseUrl;
this.baseUrl = `${tempBaseUrl}/v2`;
this.token = token;
this.stats = new Statistics();
this.logger = logger.child({ prefix: 'ApifyClient' });
this.httpClient = new HttpClient({
Expand All @@ -72,21 +71,19 @@ class ApifyClient {
requestInterceptors,
timeoutSecs,
logger: this.logger,
token,
});
}

/**
* @return {{httpClient: HttpClient, apifyClient: ApifyClient, baseUrl: string, params: {token: string}}}
* @return {{httpClient: HttpClient, apifyClient: ApifyClient, baseUrl: string}}
* @private
*/
_options() {
return {
baseUrl: this.baseUrl,
apifyClient: this,
httpClient: this.httpClient,
params: {
token: this.token,
},
};
}

Expand Down
15 changes: 9 additions & 6 deletions test/_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Browser {
return this.browser;
}

async getInjectedPage(baseUrl, DEFAULT_QUERY) {
async getInjectedPage(baseUrl, DEFAULT_OPTIONS) {
const page = await this.browser.newPage();
await Apify.utils.puppeteer.injectFile(page, `${__dirname}/../dist/bundle.js`);

Expand All @@ -20,7 +20,7 @@ class Browser {
maxRetries: 0,
...defaultQuery,
});
}, baseUrl, DEFAULT_QUERY);
}, baseUrl, DEFAULT_OPTIONS);
return page;
}

Expand All @@ -29,14 +29,13 @@ class Browser {
}
}

const DEFAULT_QUERY = {
const DEFAULT_OPTIONS = {
token: 'default-token',
};

const getExpectedQuery = (callQuery = {}) => {
const query = optsToQuery(callQuery);
return {
...DEFAULT_QUERY,
...query,
};
};
Expand All @@ -57,7 +56,11 @@ function optsToQuery(params) {
}, {});
}

const validateRequest = (query = {}, params = {}, body = {}, headers = {}) => {
const validateRequest = (query = {}, params = {}, body = {}, additionalHeaders = {}) => {
const headers = {
authorization: `Bearer ${DEFAULT_OPTIONS.token}`,
...additionalHeaders,
};
const request = mockServer.getLastRequest();
const expectedQuery = getExpectedQuery(query);
if (query !== false) expect(request.query).toEqual(expectedQuery);
Expand All @@ -74,6 +77,6 @@ const validateRequest = (query = {}, params = {}, body = {}, headers = {}) => {

module.exports = {
validateRequest,
DEFAULT_QUERY,
DEFAULT_OPTIONS,
Browser,
};
11 changes: 6 additions & 5 deletions test/actors.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ApifyClient = require('../src');
const { stringifyWebhooksToBase64 } = require('../src/utils');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Actor methods', () => {
let baseUrl;
Expand All @@ -23,11 +23,11 @@ describe('Actor methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down Expand Up @@ -273,8 +273,9 @@ describe('Actor methods', () => {
'log',
])('%s() works', async (method) => {
const actorId = 'some-actor-id';
const requestedStatus = 'SUCCEEDED';

const lastRunClient = client.actor(actorId).lastRun();
const lastRunClient = client.actor(actorId).lastRun({ status: requestedStatus });
const res = method === 'get'
? await lastRunClient.get()
: await lastRunClient[method]().get();
Expand All @@ -284,7 +285,7 @@ describe('Actor methods', () => {
} else {
expect(res.id).toEqual(`last-run-${method}`);
}
validateRequest({}, { actorId });
validateRequest({ status: requestedStatus }, { actorId });

const browserRes = await page.evaluate((aId, mthd) => {
const lrc = client.actor(aId).lastRun();
Expand Down
6 changes: 3 additions & 3 deletions test/builds.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Build methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Build methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/datasets.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Dataset methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Dataset methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/key_value_stores.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Readable } = require('stream');
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Key-Value Store methods', () => {
let baseUrl;
Expand All @@ -23,11 +23,11 @@ describe('Key-Value Store methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/logs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Log methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Log methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/request_queues.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Request Queue methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Request Queue methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/runs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Run methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Run methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/schedules.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Schedule methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Schedule methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/tasks.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ApifyClient = require('../src');
const { stringifyWebhooksToBase64 } = require('../src/utils');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Task methods', () => {
let baseUrl;
Expand All @@ -23,11 +23,11 @@ describe('Task methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/users.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ME_USER_NAME_PLACEHOLDER } = require('@apify/consts');
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('User methods', () => {
let baseUrl;
Expand All @@ -23,11 +23,11 @@ describe('User methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/webhook_dispatches.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ApifyClient = require('../src');
const mockServer = require('./mock_server/server');
const { Browser, validateRequest, DEFAULT_QUERY } = require('./_helper');
const { Browser, validateRequest, DEFAULT_OPTIONS } = require('./_helper');

describe('Webhook Dispatch methods', () => {
let baseUrl;
Expand All @@ -22,11 +22,11 @@ describe('Webhook Dispatch methods', () => {
let client;
let page;
beforeEach(async () => {
page = await browser.getInjectedPage(baseUrl, DEFAULT_QUERY);
page = await browser.getInjectedPage(baseUrl, DEFAULT_OPTIONS);
client = new ApifyClient({
baseUrl,
maxRetries: 0,
...DEFAULT_QUERY,
...DEFAULT_OPTIONS,
});
});
afterEach(async () => {
Expand Down
Loading

0 comments on commit 0f312ac

Please sign in to comment.