英文:
How to get a YAML file from the Bitbucket Server API?
问题
以下是翻译好的部分:
我正在尝试在Python脚本中使用API调用获取位于我们Bitbucket上的YAML文件,并以YAML格式呈现。到目前为止,我唯一能够获取到任何数据的方式是在API调用的末尾使用.json()
,然后使用yaml.dump()
对其进行处理。
由于我对Python不熟悉,我不知道这是否是一个愚蠢的错误,但我查看了一些文档,但尚未找到答案。
请注意,出于安全原因,我已经用虚拟值替换了API调用中的某些数据,我确信API调用本身不是问题,因为我收到了正确的数据,但格式不对。因此,我正在寻找一种要么以YAML格式请求数据,要么将其转换为YAML格式的方法。
import requests
import yaml
composefile = yaml.dump(requests.get('https://our-company-bitbucket-url.io/rest/api/latest/projects/our-project/repos/our-repo/browse/reporting-distribution-resources/src/main/resources/docker/docker-compose.yml', headers={'Authorization': 'Bearer SUPERSECRETAUTHENTICATIONTOKEN69'}).json())
print(composefile)
打印命令打印以下内容:
isLastPage: true
limit: 500
lines:
- text: 'version: ''2.0'''
- text: 'services:'
- text: ' installer:'
- text: ' image: url-to-image'
- text: ' labels:'
- text: ' io.portainer.accesscontrol.public: "public"'
...
英文:
I am trying to get a YAML file, located on our Bitbucket, in YAML format, using an API call in a Python script. The only way I have been able to get any data so far, is by using .json()
at the end of the API call, and then using yaml.dump()
on it.
As I am new to Python, I don't know if it's a stupid mistake or not, but I have looked at some documentation and not found the answer yet.
Please note that I have replaced some data with dummy values in the API call to for security reasons, and I am sure the API call itself is not the problem, since I am receiving the right data, but in the wrong format. Thus, I am looking for a way to either request it in YAML format, or convert it to YAML format.
import requests
import yaml
composefile = yaml.dump(requests.get('https://our-company-bitbucket-url.io/rest/api/latest/projects/our-project/repos/our-repo/browse/reporting-distribution-resources/src/main/resources/docker/docker-compose.yml', headers={'Authorization': 'Bearer SUPERSECRETAUTHENTICATIONTOKEN69'}).json())
print(composefile)
The print command prints the following:
isLastPage: true
limit: 500
lines:
- text: 'version: ''2.0'''
- text: 'services:'
- text: ' installer:'
- text: ' image: url-to-image'
- text: ' labels:'
- text: ' io.portainer.accesscontrol.public: "public"'
...
答案1
得分: 1
JSON
响应中的lines
值似乎已经是您想要的YAML文件。
response = requests.get('https://...docker-compose.yml', headers={'Authorization': 'Bearer SUPERSECRETAUTHENTICATIONTOKEN69'}).json()
composeFile = response['lines']
如果lines
(从您的YAML编码中似乎可能如此)是一个字符串列表,它们需要重新连接成一个单个字符串。
composeFile = '\n'.join(composeFile)
然后,可以将其解码为单个YAML文档。
d = yaml.load(composeFile, loader=YAML.Loader)
英文:
The lines
value in the JSON response appears to already be the YAML file you want.
response = requests.get('https://...docker-compose.yml', headers={'Authorization': 'Bearer SUPERSECRETAUTHENTICATIONTOKEN69'}).json())
composeFile = response.json()['lines']
If lines
is (as seems possible from your YAML encoding) a list of strings, they would need to be rejoined into a single string.
composeFile = '\n'.join(composeFile)
That could then be decoded as a single YAML document.
d = yaml.load(composeFile, loader=YAML.Loader)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论