Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,458,610
Community Members
 
Community Events
176
Community Groups

Bitbucket API max_depth not working

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

@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 

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

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

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

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

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


Regards, Igor.

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

@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 

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

Atlassian Community Events