Skip to content

Commit 146894b

Browse files
authored
Added actions (#17839)
1 parent 888ca47 commit 146894b

File tree

5 files changed

+660
-7
lines changed

5 files changed

+660
-7
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import app from "../../brave_search_api.app.mjs";
2+
3+
export default {
4+
key: "brave_search_api-web-search",
5+
name: "Web Search",
6+
description: "Query Brave Search and get back search results from the web. [See the documentation](https://api-dashboard.search.brave.com/app/documentation/web-search/get-started)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
q: {
12+
propDefinition: [
13+
app,
14+
"q",
15+
],
16+
},
17+
country: {
18+
propDefinition: [
19+
app,
20+
"country",
21+
],
22+
},
23+
searchLang: {
24+
propDefinition: [
25+
app,
26+
"searchLang",
27+
],
28+
},
29+
uiLang: {
30+
propDefinition: [
31+
app,
32+
"uiLang",
33+
],
34+
},
35+
safesearch: {
36+
propDefinition: [
37+
app,
38+
"safesearch",
39+
],
40+
},
41+
},
42+
async run({ $ }) {
43+
const allResults = [];
44+
const count = 20;
45+
for (let offset = 0; offset < 5; offset++) {
46+
const response = await this.app.webSearch({
47+
$,
48+
params: {
49+
q: this.q,
50+
country: this.country,
51+
search_lang: this.searchLang,
52+
ui_lang: this.uiLang,
53+
safesearch: this.safesearch,
54+
count,
55+
offset,
56+
},
57+
});
58+
59+
const results = response.web?.results;
60+
allResults.push(...results);
61+
62+
if (results.length < count) {
63+
break;
64+
}
65+
}
66+
67+
$.export("$summary", `Retrieved ${allResults.length} results`);
68+
return {
69+
allResults,
70+
};
71+
},
72+
};
Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
14
export default {
25
type: "app",
36
app: "brave_search_api",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
q: {
9+
type: "string",
10+
label: "Query",
11+
description: "Search query string",
12+
},
13+
country: {
14+
type: "string",
15+
label: "Country",
16+
description: "Country code to localize search results",
17+
optional: true,
18+
options: constants.COUNTRY_CODES,
19+
},
20+
searchLang: {
21+
type: "string",
22+
label: "Search Language",
23+
description: "Language to use for search results",
24+
optional: true,
25+
options: constants.LANGUAGE_CODES,
26+
},
27+
uiLang: {
28+
type: "string",
29+
label: "UI Language",
30+
description: "Language for the user interface",
31+
optional: true,
32+
options: constants.UI_CODES,
33+
},
34+
safesearch: {
35+
type: "string",
36+
label: "SafeSearch",
37+
description: "SafeSearch filter level",
38+
optional: true,
39+
options: constants.SEARCH_OPTIONS,
40+
},
41+
},
542
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
43+
_baseUrl() {
44+
return "https://api.search.brave.com/res/v1";
45+
},
46+
async _makeRequest(opts = {}) {
47+
const {
48+
$ = this,
49+
path,
50+
headers,
51+
...otherOpts
52+
} = opts;
53+
return axios($, {
54+
...otherOpts,
55+
url: this._baseUrl() + path,
56+
headers: {
57+
"x-subscription-token": `${this.$auth.api_key}`,
58+
"accept": "application/json",
59+
"accept-encoding": "gzip",
60+
...headers,
61+
},
62+
});
63+
},
64+
65+
async webSearch(args = {}) {
66+
return this._makeRequest({
67+
path: "/web/search",
68+
...args,
69+
});
970
},
1071
},
1172
};

0 commit comments

Comments
 (0)