How to effectively translate Javascript fetch functions into Pyscript?

Question:

So recently I’ve been dealing with making a website using Pyscript. But i cannot get the asyncio in Python to work with the function I’ve written in JS. There is a way to directly write this function in Python but I’m not sure yet how to do it.(it can suposedly be done by either pyfetch or importing window, the main problem is that I don’t understand how to add the additional options)

async function catchJob(ean){
  const encodedParams = new URLSearchParams();
  encodedParams.append("source", "amazon");
  encodedParams.append("country", "de");
  encodedParams.append("values", ean);

  const options = {
    method: 'POST',
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      'X-RapidAPI-Key': '*****************',
      'X-RapidAPI-Host': '*************'
    },
    body: encodedParams
  };
  let obj
  const res = await fetch('URL', options);
  obj = await res.json();
  getPrice(obj.job_id)
}

I’ve tried to do something similar to this but to no awail:

async def getPrice(id):
    options = {
      method: 'GET',
      headers: {
        'X-RapidAPI-Key': '',
        'X-RapidAPI-Host': ''
      }
    }
    res = await pyfetch(f'https://price-analytics.p.rapidapi.com/poll-job/{id}', options) 
    print(res)

Also the bigger problem i have is not understanding how to initialize the function after I’ve written it.
If someone with expertise on this topic could help I’d appreciate it kindly! Thanks for the responses and have a good day!

Asked By: Ace-Of-Snakes

||

Answers:

As I understand it, while JavaScript’s ‘fetch’ can accept an options object as an argument, pyfetch accepts keyword arguments. In addition, your print(res) will print the response object, not the content of the response. Something like this will execute the call to the api and display the content of the response object. Note that I created a ‘result’ variable to hold the response json and then printed that.

<py-script>
import asyncio
from pyodide.http import pyfetch
...
async def getPrice(id):
    headers = {'X-RapidAPI-Key': '8ff35a5077msh736dbb57384b7d9p1c9baejsnab3d923caaeb',
        'X-RapidAPI-Host': 'price-analytics.p.rapidapi.com'}   
    res = await pyfetch(f'https://price-analytics.p.rapidapi.com/poll-job/{id}', headers=headers, method="GET") 
    result = await res.json()
    print(result)
...
</py-script>

Alternatively, if you want to keep your parameters in the options variable, you’ll need to pass ‘options’ as a keyword argument. Note also that the keys in your options dictionary have been changed to strings (‘method’, ‘headers’ vs. method, headers)

async def getPrice(id):
    options = {
      'method': 'GET',
      'headers': {
        'X-RapidAPI-Key': '8ff35a5077msh736dbb57384b7d9p1c9baejsnab3d923caaeb',
        'X-RapidAPI-Host': 'price-analytics.p.rapidapi.com'
      }
    }
    res = await pyfetch(f'https://price-analytics.p.rapidapi.com/poll-job/{id}', **options) 
    print(res)

As to your statement, "Also the bigger problem i have is not understanding how to initialize the function after I’ve written it", how should the page work? Is the ‘id’ parameter typed into an input field by the user who should then trigger the function, perhaps by clicking a submit button?

Also, as a quick aside, I’d be careful about posting your api key in a public forum. Cheers!

Answered By: mdlatt