Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.

Commit b18cde2

Browse files
committed
just load uuid
1 parent e2bbc87 commit b18cde2

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

backend/aggregator.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ const fetchArticles = require("./lib/fetchArticles");
33

44
module.exports.main = async event => {
55
try {
6-
const result = await fetchArticles.search("brexit");
6+
const searchTerm = event.queryStringParameters.searchTerm;
7+
8+
const rawSapiResult = await fetchArticles.search(searchTerm);
9+
const uuids = rawSapiResult.sapiObj.results[0].results.map(obj => obj.id);
710
return {
811
statusCode: 200,
9-
body: JSON.stringify(result),
12+
body: JSON.stringify(uuids),
1013
headers: {
1114
"Access-Control-Allow-Origin": "*"
1215
}

backend/lib/fetchArticles.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fetch = require("node-fetch");
12
const CAPI_KEY = process.env.CAPI_KEY;
23
const SAPI_PATH = "https://api.ft.com/content/search/v1";
34

@@ -6,7 +7,6 @@ if (!CAPI_KEY) {
67
}
78

89
const searchBody = {
9-
queryString: 'topics:"UK Politics & Policy"',
1010
queryContext: {
1111
curations: ["ARTICLES"]
1212
},
@@ -86,11 +86,8 @@ function constructSAPIQuery(params) {
8686
function fetchResText(url, options) {
8787
return fetchWithTiming(url, options)
8888
.then(resWithTiming => {
89-
const method = options && options.method == "POST" ? "POST" : "GET";
9089
const res = resWithTiming.res;
9190
const resOk = res && res.ok;
92-
const timing = resWithTiming.timing;
93-
recordFetchTiming(method, timing, resOk, res.status, res.statusText);
9491
if (resOk) {
9592
return res;
9693
} else {
@@ -106,9 +103,12 @@ function fetchResText(url, options) {
106103
.then(res => res.text());
107104
}
108105

109-
function search(params) {
106+
function search(searchTerm) {
110107
const sapiUrl = `${SAPI_PATH}?apiKey=${CAPI_KEY}`;
111-
const sapiQuery = constructSAPIQuery(searchBody);
108+
const sapiQuery = constructSAPIQuery({
109+
...searchBody,
110+
queryString: `topics:"UK Politics & Policy"`
111+
});
112112
const options = {
113113
method: "POST",
114114
body: JSON.stringify(sapiQuery),
@@ -126,6 +126,7 @@ function search(params) {
126126
text=${text},
127127
params=${searchBody}`);
128128
}
129+
console.log(sapiObj);
129130
return {
130131
searchBody,
131132
sapiObj
@@ -137,6 +138,15 @@ function search(params) {
137138
});
138139
}
139140

141+
function fetchWithTiming(url, options = {}) {
142+
const startMillis = Date.now();
143+
return fetch(url, options).then(res => {
144+
const endMillis = Date.now();
145+
const timing = endMillis - startMillis;
146+
return { res, timing };
147+
});
148+
}
149+
140150
module.exports = {
141151
search
142152
};

backend/serverless.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ functions:
3030
events:
3131
- http:
3232
path: ftlabs-sentiment/{uuid}
33-
method: any
33+
method: get
3434
cors:
3535
origin: "*"
3636
aggregator:
3737
handler: aggregator.main
3838
events:
3939
- http:
40-
path: ftlabs-sentiment/{topic}
41-
method: any
40+
path: aggregator
41+
method: get
4242
cors:
4343
origin: "*"

0 commit comments

Comments
 (0)