Constructor
new BaseRetailerService(configs)
Create a Retailer Service. Environment variables are highest priority.
For example, you set environment:
expoort GLOBAL_ID=abcd.environment
And in your code, you also set it manually:
new BaseRetailerService({
GLOBAL_ID:"abcd.manual"
})
So in this case, GLOBAL_ID
is abcd.environment
, not abcd.manual
.
Parameters:
Name | Type | Description |
---|---|---|
configs |
Configurations | Configurations also can set by call baseRetailerService.setConfigs(configs) or set as environment variables |
Members
app :Object
An Express application return by express()
.
Type:
- Object
logger :Object
Before you baseRetailerService.init(), logger
is a console
, afer you init , logger
will be a winston logger
Type:
- Object
Methods
connector(customConnectoropt)
Connector response to save data to disk, database or maybe publish to kafka. You can use this to create your own connector. Call this function after init
We have json
and mongodb
two connectors. By default will use json
, if you you can use CONNECTOR_TYPE
to change connector type.
After you call init, will base on CONNECTOR_TYPE
to create a connector for you.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
customConnector |
Object |
<optional> |
Your custom connector |
express(paramopt) → {BaseRetailerService}
Configure express application
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
param |
Object |
<optional> |
Properties
|
Returns:
- Type
- BaseRetailerService
generateTask(param) → {Task}
Based on passed url, priority, globalId and metadata generate an task object. You can find task schema from https://docs.bitsky.ai/api/bitsky-restful-api#request-body-array-item-schema
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
param |
Object |
Properties
|
Returns:
- Type
- Task
getConfigs() → {Configurations}
Get Configurations
Returns:
- Type
- Configurations
getHomeFolder() → {string}
Get path to the Retailer Home folder
Returns:
- Type
- string
(async) getRetailerConfiguration(baseURLopt, globalIdopt) → {Object|Error}
Get retailer configuration by global id. If you didn't pass, then it uses BITSKY_BASE_URL
and GLOBAL_ID
return by thi.getConfigs()
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
baseURL |
string |
<optional> |
BitSky Supplier server url |
globalId |
string |
<optional> |
Retailer Configuration Glogbal ID |
Returns:
- Type
- Object | Error
init()
Init BaseRetailerService, create BaseRetailerService#logger
(async) listen(portopt)
Start http server and listen to port, also producer start to watch tasks
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
port |
number |
<optional> |
port number. Default get from Configuration.PORT |
parse(parseFunopt) → {BaseRetailerService}
Get or set parse function
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
parseFun |
function |
<optional> |
Parse function, it can be
And |
Returns:
- Type
- BaseRetailerService
Example
const baseRetailerService = require("@bitskyai/retailer-sdk");
const cheerio = require("cheerio");
const parseFun = async function parse({ req, res }) {
try {
let collectedTasks = req.body;
// Tasks that need collected by Producer
let needCollectTasks = [];
// Collected data
let collectedData = [];
for (let i = 0; i < collectedTasks.length; i++) {
let item = collectedTasks[i];
// req.body - https://docs.bitsky.ai/api/bitsky-restful-api#request-body-array-item-schema
let data = item.dataset.data.content;
// You can find how to use cheerio from https://cheerio.js.org/
// cheerio: Fast, flexible & lean implementation of core jQuery designed specifically for the server.
let $ = cheerio.load(data);
let targetBaseURL = "http://exampleblog.bitsky.ai/";
if (item.metadata.type == "bloglist") {
// get all blogs url in blog list page
let blogUrls = $("div.post-preview a");
for (let i = 0; i < blogUrls.length; i++) {
let $blog = blogUrls[i];
$blog = $($blog);
let url = new URL($blog.attr("href"), targetBaseURL).toString();
needCollectTasks.push(
baseRetailerService.generateTask({
url,
priority: 2,
metadata: {
type: "blog",
},
})
);
}
let nextUrl = $("ul.pager li.next a").attr("href");
if (nextUrl) {
nextUrl = new URL(nextUrl, targetBaseURL).toString();
needCollectTasks.push(
baseRetailerService.generateTask({
url: nextUrl,
priority: 2,
metadata: {
type: "bloglist",
},
})
);
}
} else if (item.metadata.type == "blog") {
collectedData.push({
title: $("div.post-heading h1").text(),
author: $("div.post-heading p.meta span.author").text(),
date: $("div.post-heading p.meta span.date").text(),
content: $("div.post-container div.post-content").text(),
url: item.dataset.url,
});
} else {
console.error("unknown type");
}
}
return {
key: "blogs",
response: {
status: 200
},
data: collectedData,
tasks: needCollectTasks,
};
} catch (err) {
console.log(`parse error: ${err.message}`);
}
};
baseRetailerService.parse(parseFun)
routers(paramopt)
Configure express router
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
param |
Object |
<optional> |
Properties
|
sendTasksToSupplier(tasks) → {Promise}
Add tasks to BitSky application
Parameters:
Name | Type | Description |
---|---|---|
tasks |
array | Array of Task want to be added |
Returns:
- Type
- Promise
setConfigs(configs)
Set BaseRetailerService configurations
Parameters:
Name | Type | Description |
---|---|---|
configs |
Configurations | Configuraions you want to set |
(async) stop()
Destory this retailer service
trigger(triggerFunopt) → {BaseRetailerService}
Get or set trigger function
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
triggerFun |
function |
<optional> |
Trigger function, it can be
And |
Returns:
- Type
- BaseRetailerService
Example
const baseRetailerService = require("@bitskyai/retailer-sdk");
const triggerFun = async function trigger({ req, res }) {
return {
tasks: [
baseRetailerService.generateTask({
url: "http://exampleblog.bitsky.ai/",
priority: 1,
metadata: { type: "bloglist" },
}),
],
};
};
baseRetailerService.trigger(triggerFun)