Skip to content

Commit 7e5de50

Browse files
committed
Proxy the data request to avoid 403
1 parent 03b2f0d commit 7e5de50

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/thoughtspot/thoughtspot-client.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const getThoughtSpotClient = (instanceUrl: string, bearerToken: string) =
1111
(client as any).instanceUrl = instanceUrl;
1212
token = bearerToken;
1313
addExportUnsavedAnswerTML(client, instanceUrl);
14+
addExportAnswerDataProxied(client, instanceUrl);
1415
return client;
1516
}
1617

@@ -34,12 +35,38 @@ mutation GetUnsavedAnswerTML($session: BachSessionIdInput!, $exportDependencies:
3435
}
3536
}`;
3637

38+
const PROXY_URL = "https://plugin-party-vercel.vercel.app/api/proxy";
39+
40+
function addExportAnswerDataProxied(client: any, instanceUrl: string) {
41+
(client as any).exportAnswerReportProxied = async ({ session_identifier, generation_number, file_format }: { session_identifier: string, generation_number: number, file_format: string }) => {
42+
const endpoint = "/api/rest/2.0/report/answer";
43+
const response = await fetch(PROXY_URL, {
44+
method: "POST",
45+
headers: {
46+
"Content-Type": "application/json",
47+
},
48+
body: JSON.stringify({
49+
token,
50+
clusterUrl: instanceUrl,
51+
endpoint,
52+
payload: {
53+
session_identifier,
54+
generation_number,
55+
file_format,
56+
}
57+
})
58+
});
59+
return response;
60+
}
61+
}
62+
63+
3764
// This is a workaround until we get the public API for this
3865
function addExportUnsavedAnswerTML(client: any, instanceUrl: string) {
3966
(client as any).exportUnsavedAnswerTML = async ({ session_identifier, generation_number }) => {
4067
const endpoint = "/prism/?op=GetUnsavedAnswerTML";
4168
// make a graphql request to `ThoughtspotHost/prism endpoint.
42-
const response = await fetch("https://plugin-party-vercel.vercel.app/api/proxy", {
69+
const response = await fetch(PROXY_URL, {
4370
method: "POST",
4471
headers: {
4572
"Content-Type": "application/json",

src/thoughtspot/thoughtspot-service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ export async function getRelevantQuestions(query: string, additionalContext: str
1616
return questions.decomposedQueryResponse?.decomposedQueries?.map((q) => q.query!) || [];
1717
}
1818

19-
async function getAnswerData({ session_identifier, generation_number, client }: { session_identifier: string, generation_number: number, client: ThoughtSpotRestApi }) {
19+
async function getAnswerData({ question, session_identifier, generation_number, client }: { question: string, session_identifier: string, generation_number: number, client: ThoughtSpotRestApi }) {
2020
try {
21-
const data = await client.exportAnswerReport({
21+
console.log("[DEBUG] Getting Data for question: ", question);
22+
// Proxy to avoid 403 from TS AWS WAF.
23+
const data = await (client as any).exportAnswerReportProxied({
2224
session_identifier,
2325
generation_number,
2426
file_format: "CSV",
@@ -33,8 +35,9 @@ async function getAnswerData({ session_identifier, generation_number, client }:
3335
}
3436
}
3537

36-
async function getAnswerTML({ session_identifier, generation_number, client }: { session_identifier: string, generation_number: number, client: ThoughtSpotRestApi }) {
38+
async function getAnswerTML({ question, session_identifier, generation_number, client }: { question: string, session_identifier: string, generation_number: number, client: ThoughtSpotRestApi }) {
3739
try {
40+
console.log("[DEBUG] Getting TML for question: ", question);
3841
const tml = await (client as any).exportUnsavedAnswerTML({
3942
session_identifier,
4043
generation_number,
@@ -55,15 +58,16 @@ export async function getAnswerForQuestion(question: string, shouldGetTML: boole
5558

5659
const { session_identifier, generation_number } = answer as any;
5760

58-
console.log("[DEBUG] Getting Data for question: ", question);
5961
const [data, tml] = await Promise.all([
6062
getAnswerData({
63+
question,
6164
session_identifier,
6265
generation_number,
6366
client
6467
}),
6568
shouldGetTML
6669
? getAnswerTML({
70+
question,
6771
session_identifier,
6872
generation_number,
6973
client

0 commit comments

Comments
 (0)