-1

I have a function that adds API requests to a list, I would like to loop over the requests until the value of 'next_page' is None. My current code is returning the error:

TypeError: can only concatenate str (not "NoneType") to str

When I print out my results I see that my function is trying to concatenate a 'None' value - how can I stop it doing this? I've tried adding in another if statement if next_page != 'None': but I am still getting the same result.

Here is my function:

pages = []
def build_requests(request):
    request = 'https://api.performancehorizon.com' + request
    job = requests.get(request, headers=headers, params=params)
    req = job.json()

    hypermedia_np = req['hypermedia']['pagination']['next_page']

    pages.append(request)

    while hypermedia_np is not None or hypermedia_np != 'None':
        next_page = req['hypermedia']['pagination']['next_page']
        print(next_page)

        if next_page != 'None':
            pages.append(next_page)
        job = requests.get('https://api.performancehorizon.com' + next_page, headers=headers, params=params)
        req = job.json()
        hypermedia_np = req['hypermedia']['pagination']['next_page']
    print("Done!")
Ben P
  • 3,267
  • 4
  • 26
  • 53
  • just use `if next_page:`? You also need to push job variable under the `if` condition – Rakesh Aug 09 '19 at 08:42
  • Or use `if next_page is not None:`. With `if next_page:`, you will also omit the case where `next_page` is an empty string (`''`), which may or may not be what you want. – jdehesa Aug 09 '19 at 08:43
  • Why are you checking for a string with the word 'None' instead of the actual `None`? – Lev M. Aug 09 '19 at 08:44
  • @LevM. I am getting the same results using `if next_page is not None:` – Ben P Aug 09 '19 at 08:46
  • @BenP what line exactly do you get the error on? – Lev M. Aug 09 '19 at 08:50
  • I don't know what exactly you want to achieve here, but you should put the following lines into the if statement too. – Ignatius Aug 09 '19 at 08:53

1 Answers1

2

In your if statement hypermedia_np != 'None': and if next_page != 'None': , 'None' is a string of four characters. You are appending every next_page except the four-letter-string 'None'. The real None object (of NoneType) is accepted by the condition.

So either write:

if next_page is not None:

to compare next_page against a None object, (See this answer for the reason you should not use != to compare against None.)

or

if next_page:

if you also want to exclude an empty string('') from being accepted. This relies on the logic that both None and '' is converted to boolean False.


(Edit to address more)

Also, if you want to properly handle None values for next_page you should also put the following line(s) into the if statement. See your next line:

job = requests.get('https://api.performancehorizon.com' + next_page, headers=headers, params=params)

You don't want to add next_page to the URL string, if next_page is None, do you?

Ignatius
  • 2,745
  • 2
  • 20
  • 32