Bitbucket API max_depth not working
Asked by Mart? Bastida Comas:

Hi,

I'm using the bitbucket API to fetch directory listings from a repo.

With this;

```
response_city=requests.get('https://api.bitbucket.org/2.0/repositories/company/repo/src/master/SomeDirectory/', auth=('user', 'pass'))
```

I successfully get a long JSON with the contents of SomeDirectory

But know I want to get the listings of SomeDirectory and at the same time the listings inside the directories in someDirectory.

So for example inside SomeDirectory I have:
> -SomeDirectory
>
> --Barcelona
>
> ---file1.hex
>
> ---file2.hex
>
> --Paris
>
> ---file3.hex
>
> ---file4.hex
>
> --Belgium
>
> ---file5.hex
>
> ---file6.hex

And I want to see not only the folders but also the files.

This should be easy with the max_depth=2 parameter. So that's what I tried:

```
response_city=requests.get('https://api.bitbucket.org/2.0/repositories/company/repo/src/master/SomeDirectory/', auth=('user', 'pass'), params={"max_depth":"2"})
```

I have tried both with

```
params={"max_depth":"2"} 
```

and

```
params={"max_depth":2}
```

and also max_depth=3.

But I still only see the first level of folders. Not the files inside.

What am I doing wrong?


Answers:
author: Igor Stoyanov
body: [@Mart? Bastida Comas](/t5/user/viewprofilepage/user-id/4658327) hi. Thanks for question.

\`max_depth\` is a query parameter so you should it like:

```
response_city=requests.get('https://api.bitbucket.org/2.0/repositories/company/repo/src/master/SomeDirectory?max_depth=2', auth=('user', 'pass'))
```

Regards, Igor

comments: author: Mart? Bastida Comas
body: Thank you [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492)

However, it's still not working. The API still only returns the folders on /someDirectory and not the files inside the folders of /someDirectory/

PD: Sorry for the late reply :)


author: Igor Stoyanov
body: [@Mart? Bastida Comas](/t5/user/viewprofilepage/user-id/4658327) hi. Try to add pagelen

```
response_city=requests.get('https://api.bitbucket.org/2.0/repositories/company/repo/src/master/SomeDirectory?max_depth=2\&pagelen=100
```

by default response contain only 10 items with \`next\` field in response.  

Regards, Igor.


author: Mart? Bastida Comas
body: Great, now it works.

On a side note. Would it be possible to simplify the information given? I just need the directories, not the hash, commit etc....


author: Igor Stoyanov
body: [@Mart? Bastida Comas](/t5/user/viewprofilepage/user-id/4658327) hi. Try to do it with

```
max_depth=3&pagelen=100&fields=values.path
```

<br />

Regards, Igor.


author: Mart? Bastida Comas
body: Great, exactly what I needed.

But, however since the response contains more than 100 directories I need the second page. But entering

```
response_city=requests.get("https://api.bitbucket.org/2.0/repositories/company/repo/src/master/SomeDirectory?max_depth=2&pagelen=100&page=2&fields=values.path", auth=('user', 'pwd'))
```

Returns error 500.

I'm pretty sure there has to be more pages because someDirectory contains 11 folders and each of these folders contains 13 files.

But I have to admit that I still haven't found:

*by default response contain only 10 items with \`next\` field in response.*  

Which I understand tells you if there's a seconds page or not


author: Igor Stoyanov
body: [@Mart? Bastida Comas](/t5/user/viewprofilepage/user-id/4658327) example in python below  
You can setup your own **pagelen** if you want, by default the chunk size is 10. Also refer to documentation you can tell api which fields you want in response.

```
import requests
import os
import json


def api_call():
    response = requests.get(
        'https://api.bitbucket.org/2.0/repositories/<path to your folder>?max_depth=2\&pagelen=100\&fields=next,values.path',
        auth=(os.environ['BITBUCKET_USERNAME'], os.environ['BITBUCKET_APP_PASSWORD'])
    )

    data = json.loads(resp.text)
    result = data['values']
    while 'next' in data:
        response = requests.get(
            data["next"],
            auth=(os.environ['BITBUCKET_USERNAME'], os.environ['BITBUCKET_APP_PASSWORD'])
        )
        data = json.loads(resp.text)
        result.extend(data['values'])

    print(result)


if __name__ == '__main__':
    api_call()
```

<br />

Regards, Igor


author: Mart? Bastida Comas
body: Wow, this is exactly what I needed. Really thank you for all the time invested.

For future people with the same problem, just take into account [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492) that you have to change resp.text by response.text :):

```
import requests
import os
import json


def api_call():
    response = requests.get(
        'https://api.bitbucket.org/2.0/repositories/<path to your folder>?max_depth=2\&pagelen=100\&fields=next,values.path',
        auth=(os.environ['BITBUCKET_USERNAME'], os.environ['BITBUCKET_APP_PASSWORD'])
    )

    data = json.loads(response.text)
    result = data['values']
    while 'next' in data:
        response = requests.get(
            data["next"],
            auth=(os.environ['BITBUCKET_USERNAME'], os.environ['BITBUCKET_APP_PASSWORD'])
        )
        data = json.loads(response.text)
        result.extend(data['values'])

    print(result)


if __name__ == '__main__':
    api_call()
```


Tags: api

bitbucket-cloud

cloud

repository

source