async sendData()

in marketing-analytics/activation/gmp-googleads-connector/src/api_handlers/cloud_function_invoke.js [145:200]


  async sendData(records, messageId, config) {
    this.logger.debug(`Init CF invoker with Debug Mode.`);
    const managedSend = this.getManagedSendFn(config);
    const { service: { url, args, vars, functionName } } = config;
    try {
      const token = await getIdTokenForFunction(url);
      const needMergeArgs = this.needMergeArgs_(config);
      const results = [];
      const getSingleInvokeFn = async (lines, batchId) => {
        const records = lines.map((line) => JSON.parse(line));
        /** @const {!BatchResult} */ const batchResult = {
          numberOfLines: records.length,
        };
        const source = this.getMergedData_(needMergeArgs, records);
        const options = {
          url,
          headers: { Authorization: `bearer ${token}` },
        }
        options.data = lodash.merge({ args, vars, functionName }, source);
        try {
          const responseData = await requestWithRetry(options, this.logger);
          if (!needMergeArgs) {
            results.push(lodash.merge(source, responseData));
          } else {
            responseData.result.forEach((result, index) => {
              results.push(lodash.merge(records[index], result));
            });
          }
          batchResult.result = true;
          return batchResult;
        } catch (error) {
          this.logger.error(`CF invoke[${batchId}] failed: ${lines[0]}`, error);
          batchResult.result = false;
          batchResult.errors = [error.message];
          batchResult.failedLines = lines;
          return batchResult;
        }
      };
      const batchResult =
        await managedSend(getSingleInvokeFn, records, messageId);
      const { bucket, folder, projectId } = config.output;
      const storageFile =
        StorageFile.getInstance(bucket, folder + messageId, { projectId });
      await storageFile.getFile().save(
        results.map((result) => JSON.stringify(result)).join('\n'));
      return batchResult;
    } catch (error) {
      this.logger.error(`CF invoke[${messageId}] failed: `, error);
      const batchResult = {
        result: false,
        numberOfLines: records.split('\n').length,
        errors: [error.message],
      };
      return batchResult;
    }
  }