Bitbucket API max_depth not working

Martí Bastida Comas February 2, 2022

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? 

1 answer

1 accepted

0 votes
Answer accepted
Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 4, 2022

@Martí Bastida Comas 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 

Martí Bastida Comas February 14, 2022

Thank you @Igor Stoyanov

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 :)

Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 14, 2022

@Martí Bastida Comas 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.

Martí Bastida Comas February 14, 2022

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....

Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 15, 2022

@Martí Bastida Comas  hi. Try to do it with 

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


Regards, Igor.

Martí Bastida Comas February 15, 2022

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

Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 15, 2022

@Martí Bastida Comas 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()


Regards, Igor 

Martí Bastida Comas February 15, 2022

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 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()
Like Igor Stoyanov likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events