Search code examples
pythonfunctionpaginationgitlab-api

TypeError: '<=' not supported between instances of 'int' and 's


I'm using a function to grab some users from Gitlab API but I wish to paginate from the headers and store all users not just one page of 100 users, for some reason I need to add int somewhere in my code but I'm uncertain where, please can anyone assist:

    # Base URI of Gitlab API from our private Gitlab Instance
baseuri = "https://git.tools.dev.mycompany.net/api/v4"

# Function to grab users and put objects in S3 bucket:
def get_gitlab_users(access_token=access_token, baseuri=baseuri):
    next_page = 1
    result = []
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer {}".format(access_token),
    }

    # Paginate by using x-total-pages from the headers received in the response
    # https://docs.gitlab.com/ee/api/#pagination-link-header
    url = f"{baseuri}/users/?per_page=100&active=true&without_project_bots=true&page={next_page}"
    req = http.request(method="GET", url=url, headers=headers)
    result.extend(json.loads(req.data))

    while next_page <= req.headers["x-total-pages"]:
        url = f"{baseuri}/users/?per_page=100&active=true&without_project_bots=true&page={next_page}"
        req = http.request(method="GET", url=url, headers=headers)
        result.extend(json.loads(req.data))

This is where the error occurs:

while next_page <= req.headers["x-total-pages"]:
TypeError: '<=' not supported between instances of 'int' and 'str'

Solution

  • I assume req.headers["x-total-pages"] is a string, so cast it to int explicitly like this: int(req.headers["x-total-pages"]) and you should be fine (since you'll be comparing two ints and not an int to a str