200-901 Question 133
Single answerYou are tasked with creating a Python script to interact with a network device using a REST API. The script includes a section where you need to store the API base URL and authentication token for reuse throughout the script. Which approach should you take to define these variables correctly while adhering to best practices?
- A
Define the API base URL and token as global variables at the top of the script.
- B
Hardcode the API base URL and token directly into each function that requires them.
- C
Store the API base URL and token in a configuration file and load them into variables within the script.
- D
Prompt the user to input the API base URL and token each time the script is executed.
Show answer and explanation
Correct answer: C
Explanation
The best practice is to store sensitive information, such as API tokens and URLs, in a configuration file. This approach ensures the variables can be reused throughout the script without hardcoding them directly, improving maintainability and security. Configuration files can also be excluded from version control to protect sensitive data.
- A. Incorrect.
Defining the API base URL and token as global variables at the top of the script can expose sensitive information if the script is shared or stored in a public repository. It is not considered a secure practice.
- B. Incorrect.
Hardcoding the API base URL and token into each function leads to redundancy and makes the script difficult to maintain. Additionally, it can expose sensitive information.
- C. Correct.
Storing the API base URL and token in a configuration file and loading them into variables is a secure and reusable approach. It allows for better script maintainability and ensures sensitive information is not directly embedded in the script.
- D. Incorrect.
Prompting the user to input the API base URL and token each time the script is executed is inefficient and can lead to user errors. It is not practical for automation or scalable solutions.