1. import argparse
  2. import json
  3. def decode_api_endpoint(api_endpoint, dry_run=False):
  4. """
  5. Decodes an API endpoint string, handling staging environment variables and dry-run mode.
  6. Args:
  7. api_endpoint (str): The API endpoint string.
  8. dry_run (bool): If True, only print the decoded endpoint without executing.
  9. Returns:
  10. str: The decoded API endpoint.
  11. """
  12. # Define staging environment variables (replace with your actual variables)
  13. staging_env = {
  14. "API_BASE_URL": "https://staging.example.com",
  15. "API_VERSION": "v2",
  16. "AUTH_TOKEN": "staging_token"
  17. }
  18. decoded_endpoint = api_endpoint
  19. # Replace placeholders with values from the staging environment
  20. for key, value in staging_env.items():
  21. decoded_endpoint = decoded_endpoint.replace(f"{{{{{key}}}", value)
  22. # Add API version if not already present
  23. if "v" not in decoded_endpoint and "api" in decoded_endpoint.lower():
  24. decoded_endpoint = f"{decoded_endpoint} {staging_env['API_VERSION']}"
  25. if dry_run:
  26. print(f"Dry Run: Decoded endpoint: {decoded_endpoint}")
  27. return decoded_endpoint # Return for proper usage
  28. else:
  29. print(f"Decoded endpoint: {decoded_endpoint}")
  30. return decoded_endpoint
  31. if __name__ == "__main__":
  32. parser = argparse.ArgumentParser(description="Decode API endpoints for staging environments.")
  33. parser.add_argument("endpoint", help="The API endpoint to decode.")
  34. parser.add_argument("--dry_run", action="store_true", help="Perform a dry run (print decoded endpoint without executing).")
  35. args = parser.parse_args()
  36. decode_api_endpoint(args.endpoint, args.dry_run)

Add your comment