Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

Reading YAML file in Python, is this the best way?

To give you a little bit of background, I am creating a function to automate some tasks for a personal project. I have used a conf file in the YAML format to help structure data and make it more 'user friendly'.

Conf.yaml

---
Version: 0.0.1
CyberArk:
 PVWA:
  URL: https://cyberark.corp.local
 OnboardingAccount:
  AppID: RESTAPI
  Safe: cashrd
  Username: api
Requests:
 Auth: /PasswordVault/API/Auth/CyberArk/Logon
 AAM: /AIMWebService/api/Accounts?AppID={}&Safe={}&Username={}

MainFunctions.py

    def fetch_api_information():
    url = fetch_url()
    for i in configuration['CyberArk']:
        appid = configuration['CyberArk']['OnboardingAccount']['AppID']
        safe = configuration['CyberArk']['OnboardingAccount']['Safe']
        username = configuration['CyberArk']['OnboardingAccount']['Username']
        aam = configuration['CyberArk']['Requests']['AAM']
    url += aam.format(appid, safe, username)
    client = "RESTAPI.crt"
    key = "RESTAPI.key"
    ssl = "PVWA.pem"
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("GET", url, headers=headers, verify=ssl, cert=(client, key))
    apidetail = json.loads(response.text)
    return apidetail

Essentially what the code is make a API request based on the AppID, Safe and Username then returning a load of information about the account (metadata).

Is there a better way of looping through the information or am I on the right tracks?

Please go easy, this is my first post :D

question from:https://stackoverflow.com/questions/65599200/reading-yaml-file-in-python-is-this-the-best-way

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...