iterate Multiple URLs one by one in Same API

Issue

This route handle three request at a time but upon massive traffic this method fail. Is there any method to handle massive traffic



@app.route('/v1.1/userRegistor', methods=['POST', 'GET'])
def job():
   count = 0
   data = request.get_json()
   numbers = data['number']

   if count == 0:
       api_url = f'https://app.simplywhatsapp.com/api/send.php?number={numbers}&type=text&message=Your OTP is %20{valuepi1}&instance_id=instance1&access_token=access_token1'
       r = requests.get(api_url).text
       count += 1
       return str(count)
   if count == 1:
       api1_url = f'https://app.simplywhatsapp.com/api/send.php?number={numbers}&type=text&message=Your OTP is %20{value}&instance_id=instance2&access_token=access_token2'
       r = requests.get(api1_url).text
       count += 1
       return str(count)

   if count == 2:
       api2_url = f'https://app.simplywhatsapp.com/api/send.php?number={numbers}&type=text&message=Your OTP is %20{value}&instance_id=instance3&access_token=access_token3'
       r = requests.get(api2_url).text
       count = 0
       return str(count)
   

Solution

Choosing the random from both API responses it’s a bad approach. You can randomly choose the URL and fetch the API response from the selected URL.

@app.route('/v1.1/userRegistor', methods=['POST','GET'])
def job():
    data = request.get_json()
    numbers=data['number']
    api_url =f'https://app.simplywhatsapp.com/api/send.php?number={numbers}&type=text&message=Your OTP is %20{value}&instance_id=api1&access_token=api1'
    api1_url=f'https://app.simplywhatsapp.com/api/send.php?number={numbers}&type=text&message=Your OTP is %20{value}&instance_id=api2&access_token=api2'
    url = random.choice([api_url, api1_url])
    
    return requests.get(url).text

Answered By – Mubeenuddin Abbasi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published