diff --git a/lib/actions/report-actions.js b/lib/actions/report-actions.js index 9b9eeae95..bb9d726fc 100644 --- a/lib/actions/report-actions.js +++ b/lib/actions/report-actions.js @@ -1,41 +1,43 @@ // @flow +import type { AuthMetadata } from '../shared/identity-client-context'; import type { ClientReportCreationRequest, ReportCreationResponse, } from '../types/report-types.js'; import { sendReports as callSendReports } from '../utils/reports-service.js'; const sendReportActionTypes = Object.freeze({ started: 'SEND_REPORT_STARTED', success: 'SEND_REPORT_SUCCESS', failed: 'SEND_REPORT_FAILED', }); const sendReport: ( request: ClientReportCreationRequest, -) => Promise = async request => { + authMetadata: ?AuthMetadata, +) => Promise = async (request, authMetadata) => { const { reportIDs: [id], - } = await callSendReports([request]); + } = await callSendReports([request], authMetadata); if (!id) { throw new Error('Server did not return report ID'); } return { id }; }; const sendReportsActionTypes = Object.freeze({ started: 'SEND_REPORTS_STARTED', success: 'SEND_REPORTS_SUCCESS', failed: 'SEND_REPORTS_FAILED', }); const queueReportsActionType = 'QUEUE_REPORTS'; export { sendReportActionTypes, sendReport, sendReportsActionTypes, queueReportsActionType, }; diff --git a/lib/utils/reports-service.js b/lib/utils/reports-service.js index 7b6fd440b..07e8db0fb 100644 --- a/lib/utils/reports-service.js +++ b/lib/utils/reports-service.js @@ -1,59 +1,67 @@ // @flow import { fetchWithTimeout } from './fetch-utils.js'; +import { createDefaultHTTPRequestHeaders } from './services-utils.js'; import { reportsServiceURL, sendReportEndpoint, } from '../facts/reports-service.js'; +import type { AuthMetadata } from '../shared/identity-client-context.js'; import type { ReportsServiceSendReportsRequest, ReportsServiceSendReportsResponse, ReportCreationRequest, ClientReportCreationRequest, } from '../types/report-types.js'; const REQUEST_TIMEOUT = 60 * 1000; // 60s async function sendReports( reports: ReportsServiceSendReportsRequest, + authMetadata?: ?AuthMetadata, ): Promise { const reportsArray: $ReadOnlyArray = Array.isArray(reports) ? reports : [reports]; if (reportsArray.length === 0) { return { reportIDs: [] }; } const requestBody: $ReadOnlyArray = reportsArray.map( clientReport => { const { id, ...report } = clientReport; return report; }, ); const url = `${reportsServiceURL}${sendReportEndpoint.path}`; + const defaultHeaders = + authMetadata?.userID && authMetadata?.accessToken && authMetadata?.deviceID + ? createDefaultHTTPRequestHeaders(authMetadata) + : {}; const response = await fetchWithTimeout(url, { method: sendReportEndpoint.method, body: JSON.stringify(requestBody), headers: { + ...defaultHeaders, 'Content-Type': 'application/json', }, timeout: REQUEST_TIMEOUT, }); if (!response.ok) { const { status, statusText } = response; let responseText, errorMessage; try { responseText = await response.text(); } finally { errorMessage = responseText || statusText || '-'; } // we cannot throw error inside `finally` block because eslint complains throw new Error(`Server responded with HTTP ${status}: ${errorMessage}`); } const { reportIDs } = await response.json(); return { reportIDs }; } export { sendReports };