Skip to content

Commit 3d1ef4f

Browse files
authored
Merge pull request #6 from ScrapingBee/update-sdk-retry
Add retry option and param interfaces
2 parents 4a0bca9 + ee1c529 commit 3d1ef4f

13 files changed

Lines changed: 366 additions & 297 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,22 @@ screenshot('https://httpbin-scrapingbee.cleverapps.io/html', './httpbin.png').ca
125125
console.log('A problem occurs : ' + e.message)
126126
);
127127
```
128+
129+
## Retries
130+
131+
The client includes a retry mechanism for 5XX responses.
132+
133+
```javascript
134+
const spb = require('scrapingbee');
135+
136+
async function get(url) {
137+
let client = new spb.ScrapingBeeClient('REPLACE-WITH-YOUR-API-KEY');
138+
let resp = await client.get({ url: url, params: { render_js: false }, retries: 5 });
139+
140+
let decoder = new TextDecoder();
141+
let text = decoder.decode(resp.data);
142+
console.log(text);
143+
}
144+
145+
get('https://httpbin-scrapingbee.cleverapps.io/html').catch((e) => console.log('A problem occured: ' + e.message));
146+
```

dist/index.d.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
import { AxiosPromise } from 'axios';
2+
export declare type SpbParams = {
3+
block_ads?: boolean;
4+
block_resources?: boolean;
5+
cookies?: string | Record<string, any>;
6+
country_code?: string;
7+
custom_google?: boolean;
8+
device?: string;
9+
extract_rules?: object | string;
10+
forward_headers?: boolean;
11+
forward_headers_pure?: boolean;
12+
js_scenario?: object | string;
13+
json_response?: boolean;
14+
own_proxy?: string;
15+
premium_proxy?: boolean;
16+
render_js?: boolean;
17+
return_page_source?: boolean;
18+
screenshot?: boolean;
19+
screenshot_full_page?: boolean;
20+
screenshot_selector?: string;
21+
session_id?: number;
22+
stealth_proxy?: boolean;
23+
timeout?: number;
24+
transparent_status_code?: boolean;
25+
wait?: number;
26+
wait_for?: string;
27+
window_height?: number;
28+
window_width?: number;
29+
} & {
30+
[key: string]: any;
31+
};
32+
export interface SpbConfig {
33+
url: string;
34+
headers?: Record<string, any>;
35+
cookies?: string | Record<string, any>;
36+
params?: SpbParams;
37+
data?: any;
38+
retries?: number;
39+
}
240
export declare class ScrapingBeeClient {
341
readonly api_key: string;
442
constructor(api_key: string);
543
private request;
6-
get(config?: Record<string, any>): AxiosPromise<any>;
7-
post(config?: Record<string, any>): AxiosPromise<any>;
44+
get(config: SpbConfig): AxiosPromise<any>;
45+
post(config: SpbConfig): AxiosPromise<any>;
846
}

dist/index.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,38 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.ScrapingBeeClient = void 0;
77
const axios_1 = __importDefault(require("axios"));
8-
const version_1 = require("./version");
8+
const axios_retry_1 = __importDefault(require("axios-retry"));
99
const utils_1 = require("./utils");
1010
const API_URL = 'https://app.scrapingbee.com/api/v1/';
1111
class ScrapingBeeClient {
1212
constructor(api_key) {
1313
this.api_key = api_key;
1414
}
15-
request(method, config = {}) {
15+
request(method, config) {
16+
var _a;
1617
let params = config.params || {};
17-
let raw_headers = config.headers || {};
18-
let headers = utils_1.process_headers(raw_headers);
19-
if (headers != {}) {
20-
params['forward_headers'] = true;
18+
// Headers
19+
let headers = utils_1.process_headers(config.headers);
20+
if (Object.keys((_a = config.headers) !== null && _a !== void 0 ? _a : {}).length > 0) {
21+
params.forward_headers = true;
2122
}
22-
headers["User-Agent"] = `ScrapingBee-Node/${version_1.LIB_VERSION}`;
23-
let cookies = config.cookies || null;
24-
if (cookies != null) {
25-
params['cookies'] = cookies;
26-
}
27-
let url = config.url;
28-
let data = config.data || {};
23+
// Cookies
24+
params.cookies = config.cookies;
25+
// Other query params
2926
params['api_key'] = this.api_key;
30-
params['url'] = url;
27+
params['url'] = config.url;
3128
params = utils_1.process_params(params);
3229
let axios_params = {
3330
method: method,
3431
headers: headers,
3532
params: params,
36-
data: data,
33+
data: config.data,
3734
responseType: 'arraybuffer',
3835
};
36+
// Retry policy
37+
if (config.retries !== undefined) {
38+
axios_retry_1.default(axios_1.default, { retries: config.retries });
39+
}
3940
return axios_1.default(API_URL, axios_params);
4041
}
4142
get(config) {

dist/utils.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export declare function is_empty(value: any): boolean;
12
export declare function process_params(params: Record<string, any>): Record<string, any>;
2-
export declare function process_headers(headers: Record<string, any>, prefix?: string): Record<string, any>;
3+
export declare function process_headers(headers?: Record<string, any>, prefix?: string): Record<string, any>;

dist/utils.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.process_headers = exports.process_params = void 0;
3+
exports.process_headers = exports.process_params = exports.is_empty = void 0;
44
const version_1 = require("./version");
5-
const DEFAULT_HEADERS = { 'User-Agent': 'ScrapingBee-Node/' + version_1.LIB_VERSION };
5+
const DEFAULT_HEADERS = { 'User-Agent': `ScrapingBee-Node/${version_1.LIB_VERSION}` };
66
function process_js_snippet(js_snippet) {
77
return Buffer.from(js_snippet).toString('base64');
88
}
@@ -21,15 +21,15 @@ function process_json_stringify_param(param) {
2121
return JSON.stringify(param);
2222
}
2323
function is_empty(value) {
24-
switch (typeof value) {
25-
case 'string':
26-
return value === '';
27-
case 'object':
28-
return value && Object.keys(value).length === 0 && value.constructor === Object;
29-
default:
30-
return false;
24+
if (typeof value === 'number') {
25+
return false;
3126
}
27+
if (typeof value === 'object' && value !== null) {
28+
return Object.keys(value).length === 0;
29+
}
30+
return !value;
3231
}
32+
exports.is_empty = is_empty;
3333
function process_params(params) {
3434
var clean_params = {};
3535
for (let key in params) {
@@ -45,7 +45,8 @@ function process_params(params) {
4545
break;
4646
case 'extract_rules':
4747
case 'js_scenario':
48-
clean_params[key] = process_json_stringify_param(params[key]);
48+
clean_params[key] =
49+
typeof params[key] === 'string' ? params[key] : process_json_stringify_param(params[key]);
4950
break;
5051
default:
5152
clean_params[key] = params[key];
@@ -54,7 +55,7 @@ function process_params(params) {
5455
return clean_params;
5556
}
5657
exports.process_params = process_params;
57-
function process_headers(headers, prefix = 'Spb-') {
58+
function process_headers(headers = {}, prefix = 'Spb-') {
5859
var new_headers = {};
5960
for (let key in headers) {
6061
new_headers[`${prefix}${key}`] = headers[key];

dist/version.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare const LIB_VERSION = "1.6.1";
1+
export declare const LIB_VERSION = "1.7.0";

dist/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.LIB_VERSION = void 0;
4-
exports.LIB_VERSION = "1.6.1";
4+
exports.LIB_VERSION = "1.7.0";

0 commit comments

Comments
 (0)