0

I am trying to access the jsreport api from a firebase cloud function to render a pdf template report I created and save to my database. The problem is that whenever I make the api call I get the following error:

{
    code: "ENOTFOUND",
    errno: "ENOTFOUND",
    host: "gabrielsch.jsreportonline.net",
    hostname: "gabrielsch.jsreportonline.net",
    port: 443,
    syscall: "getaddrinfo"
}

I am trying to access the template which is in my jsreport cloud account. Here's the code from the cloud function:

exports.generateReport = functions.https.onCall((data, context) => {
    if(!context.auth)
        return myError('custom/failed-auth', 'You must be authenticated to call this function')

    const { body, userId } = data

    const options = {
        method: 'POST',
        //strictSSL: false,
        headers: {
            //'Authorization': 'Basic ' + hash,
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(body),
        auth: {
            username,
            password
        }
    }

    console.log('calling report render')

    return requestify.request('https://gabrielsch.jsreportonline.net/api/report', options)
        .then(response => {
            console.log('got report stream')
            let myFile = fs.createWriteStream('myTestReport.pdf')
            console.log('pipping stream to file')
            response.pipe(myFile)
            console.log('saving to firebase')
            return admin.storage().ref(`reports/${userId}`).put(myFile)
        })
        .then(snapshot => {
            console.log('file saved to firebase')
            return snapshot.ref.getDownloadURL()
        })
        .then(downloadURL => {
            console.log('got downloadURL' + downloadURL)
            return { downloadURL }
        })
        .catch(error => {
            console.log('got error ' + error)
            return { error }
        })
})

And here's how I call this firebase function on the client:

const body = {
    template: { shortid: 'B1z8vSImQ'  },
    data: reportData
}

const userId = 'user123'

const options = { body, userId }
let generateReport = firebase.functions().httpsCallable('generateReport')

generateReport(options)
    .then(response => {
        console.log(response.data)
    })
    .catch(error => {
        console.log(error)
    })

When making the request, I have tried a few different things to try and make it work but with no success. I tried passing the Authorization header with a base64(username:password) hash like the docs state, I tried following the same syntax as the jsreport-client package, and even tried passing my template's name instead of the shortid, but the same error persists. I double checked my shortid and my request url and they are correct and I can't seem to find any resource on why this is happening or how to properly make an http call to jsreport api, since the docs are not quite complete in my opinion.

If anyone could help somehow I would appreciate it.

Note: I'm not sure if the code that would run after the request is complete is correct since it never got that far

Gabriel Schneider
  • 535
  • 1
  • 5
  • 12

0 Answers0