Ahmet Yazıcı
Ahmet Yazıcı

Reputation: 35

Can not invoke the firebase cloud function where I have 3rd party api request

I have function to get vehicle images from a 3rd party api called evox.

The following is the function I use in my cloud function: EDITED VERSION:

const axios = require('axios').default;

/**
 * 
 * @param {Number} vifNum vif / evox number of the requested vehicle 
 * @returns {Promise} { image: '' }
 */
module.exports.makeImagesRequest = (vifNum) => {
  return new Promise(async (resolve, reject) => {
    try{
      var returnData = { image: "" }
      var productId = 0
      var productType = 0
      
      // Set headers for request
      var imagesReqUrl = 'http://api.evoximages.com/api/v1/vehicles/' + vifNum + '/products/' + productId + '/' + productType 
      const imagesRequestOptions = {
        url: imagesReqUrl,
        method: 'GET',
        headers: {
          'x-api-key': functions.config().evox.key,
          'Accept': 'application/json',
          'Content-Type': 'application/json;charset=UTF-8'
        }
      };

      // Send request and set the returned data
      const response = await axios(imagesRequestOptions);
      returnData.image =response.data.urls[0]
      resolve(returnData)
    }
    catch(error){
      reject({ error, helperMsg: "Vif number is not valid or connection problem" })
    }
  });
}

I use postman to send requests to api in the following format:

{
    "data" : {
        "vifNum": 99999
    }
}

Where I export the cloud function: EDITED VERSION:

module.exports.getVehicleImages = functions.https.onCall((data, context) => {
  return new Promise(async (resolve, reject) => {
    try{
      const val = await makeImagesRequest(data.vifNum)
      resolve(val)
    }
    catch(error){
      reject({ error, msg: "Error while getting vehicle image by evox" });
    }
  })
})

The weird thing is that, when I use emulators to test my functions, it works quite good, no error and the information is correct and consistent. But when I deployed my functions, and called that function with the api url from postman with the exact same data, now it gives an error inside this makeImagesRequest function

IMPORTANT EDIT: I tried using another 3rd party api VIN, and that api worked in cloud functions. How I built the evox and VIN functions is almost exactly same. The only notable difference is that, evox require that 'x-api-key' in the header, while VIN does not need any api-key. Could the error be related to this? PS: functions.config() is properly set, checked that out via pulling a .runtimeconfig.json file from firebase

Postman error:

enter image description here

Firebase error:

enter image description here

Upvotes: 1

Views: 208

Answers (1)

Ahmet Yazıcı
Ahmet Yazıcı

Reputation: 35

It seems that the problem was with my firebase.config(), even though I retrieved evox.key when I ran the command firebase functions:config:get > .runtimeconfig.json, in my cloud function it was not valid.

Upvotes: 0

Related Questions