简体   繁体   中英

Python request response - if elif condition not working

Hi there I am having trouble with my code on if else json response and I'm pretty sure I am doing the correct way but the code doesn't continue on the else statement.

import requests
import psycopg2
        conn = psycopg2.connect(dbname='databasename',user='postgres',password='12345',host='x.x.x.x', port='xxxx') # works correctly
        print('success db')
        cur = conn.cursor()

        cur.execute("SELECT mobile_number FROM users WHERE status='ACTIVE'") # this is my query 
        conn.commit();
        row_count = cur.rowcount
        print('This is the number of rows: ', row_count)
        result = [mobileNum[0] for mobileNum in cur.fetchall()]

        for mobileNum in result:
            print('Current Number: ', mobileNum)
            pushnotif = push_notif(mobileNum) # This is a function for http request
            
            if pushnotif['success'] == 1 and pushnotif['failure'] == 0 : # This line of code is working and continue the for loop
                print('Success!!!')
            elif pushnotif['success'] == 0 and pushnotif['failure'] == 1: # This line of code is working but only when I'm printing a sentence like faileedddd!
                print(pushnotif['results']['error']) # This doesn't print the result error on http response and exits the for loop
            elif pushnotif['message'] == "Invalid number": # This line doesn't work and end the for loops
                print(pushnotif['message'])
            else: # The code doesn't go here whenever the response is not 200 OK or push_notif['message'] or push_notif['results''error']
                print('error')

Below is a sample of response:

{
    "multicast_id": xxx,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "xxx"
        }
    ]
}

or

{
    "multicast_id": xxxx,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "invalid"
        }
    ]
}

or

{
    "message": "Invalid number"
}

I'm not sure what the function push_notif returns, but if it returns a regular dict this may be a problem as you are assuming that the keys (success, failure and result) are present in the dict, which is not the case for the last response example you provided. so typically an exception will raise and stop your program, or at least if caught, ends the for loop and exit the function.

Let's say, for example, you got initially the below responses.

{
    "message": "Invalid number"
}

Here your dictionary has only one key named message .So, when you are checking your response with keys like success , failure , your program will stop executing as these keys are not present in your current response and your else statement will never be executed.

pushnotif = push_notif(mobileNum)
pushlen = len(pushnotif)

if pushlen == 5:
     print("This will print if API response is equal to 5", pushnotif)
elif pushlen == 1:
     print("This will print if API response is equal to 1", pushnotif)
else:
     print("This will print unexpected response", pushnotif)

This is the logic thank you again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM