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?
@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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Martí Bastida Comas hi. Try to do it with
max_depth=3&pagelen=100&fields=values.path
Regards, Igor.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.