This repository was archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadaptation.ts
More file actions
70 lines (64 loc) · 2.67 KB
/
Copy pathadaptation.ts
File metadata and controls
70 lines (64 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { AdaptationEndpoint } from "./environment";
import fetch from "node-fetch";
import logger from "./logger";
import {
PageResult,
QueryExpansionRequest,
QueryExpansionResponse,
TailoredTextRequest,
TailoredTextResponse,
UserProfile
} from "./models";
/**
* An interface for the communications with the Adaptation module.
*/
export class Adaptation {
/**
* Perform a POST request to a specified endpoint with a custom json in the body.
*
* @param url Url (string) of the POST request. Must include protocol port if different from the default.
* @param body A JS object that will be stringified and sent as a JSON.
* @returns A promise resolved with the received JSON parsed as JS object of type T.
*/
private post<T = any>(url: string, body: any): Promise<T> {
return fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" }
})
.then(res => res.json());
}
/**
* Call the adaptation endpoint to get the query expansion.
*
* @param userProfile The user profile coming from the UI needed for the query expansion.
* @returns A promise resolved with the query expansion containing the keywords.
*/
public getKeywordExpansion(userProfile: UserProfile): Promise<QueryExpansionResponse> {
logger.debug("[adaptation.ts] Keyword expansion request", { url: AdaptationEndpoint.keywords, userProfile });
return this.post<QueryExpansionResponse>(
AdaptationEndpoint.keywords,
{ userProfile } as QueryExpansionRequest
).then(queryExpansionResponse => {
logger.debug("[adaptation.ts] Keyword expansion response", { queryExpansionResponse });
return queryExpansionResponse;
});
}
/**
* Pass the results of our module to adaptation.
*
* @param results The array of page results.
* @param userProfile The user profile coming from the UI needed for the adaptation.
* @returns A promise resolved with the tailored text when sent by the adaptation module.
*/
public getTailoredText(results: Array<PageResult>, userProfile: UserProfile): Promise<TailoredTextResponse> {
logger.debug("[adaptation.ts] Adaptation text request", { url: AdaptationEndpoint.text, results });
return this.post<TailoredTextResponse>(
AdaptationEndpoint.text,
{ userProfile, results } as TailoredTextRequest
).then(adaptationResponse => {
logger.debug("[adaptation.ts] Adaptation text response", { adaptationResponse });
return adaptationResponse;
});
}
}