From 174262f024712082ef3d93a62aa1def352a4dd4b Mon Sep 17 00:00:00 2001
From: JorgeEcheva26 <jorge.echevarriauribarri.practicas@telefonica.es>
Date: Thu, 7 Nov 2024 12:29:32 +0100
Subject: [PATCH] Only one aef available

---
 pyproject.toml               |   2 +-
 sdk/api_schema_translator.py | 111 +++++++++++++++++++----------------
 setup.py                     |   2 +-
 3 files changed, 64 insertions(+), 51 deletions(-)

diff --git a/pyproject.toml b/pyproject.toml
index ca052da..c6151a2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
 
 [project]
 name = "opencapif_sdk"
-version = "0.1.8.2"
+version = "0.1.8.4"
 authors = [
     { name="JorgeEcheva", email="jorge.echevarriauribarri.practicas@telefonica.com" },
     { name="dgs-cgm", email="daniel.garciasanchez@telefonica.com" }
diff --git a/sdk/api_schema_translator.py b/sdk/api_schema_translator.py
index 5a3db19..7e29a76 100644
--- a/sdk/api_schema_translator.py
+++ b/sdk/api_schema_translator.py
@@ -2,6 +2,7 @@ import json
 import logging
 import os
 import re
+import yaml
 
 
 log_path = 'logs/builder_logs.log'
@@ -60,14 +61,26 @@ class api_schema_translator:
         self.logger.info(f"API description saved to {api_name}.json")
 
     def __load_api_file(self, api_file: str):
-        """Loads the configuration file."""
+        """Loads the Swagger API configuration file and converts YAML to JSON format if necessary."""
         try:
             with open(api_file, 'r') as file:
-                return json.load(file)
+                if api_file.endswith('.yaml') or api_file.endswith('.yml'):
+                    yaml_content = yaml.safe_load(file)
+                    return json.loads(json.dumps(yaml_content))  # Convert YAML to JSON format
+                elif api_file.endswith('.json'):
+                    return json.load(file)
+                else:
+                    self.logger.warning(
+                        f"Unsupported file extension for {api_file}. Only .yaml, .yml, and .json are supported.")
+                    return {}
         except FileNotFoundError:
             self.logger.warning(
                 f"Configuration file {api_file} not found. Using defaults or environment variables.")
             return {}
+        except (json.JSONDecodeError, yaml.YAMLError) as e:
+            self.logger.error(
+                f"Error parsing the configuration file {api_file}: {e}")
+            return {}
 
     def __validate_api_info(self):
         """Validates that all required components are present in the API specification."""
@@ -81,8 +94,8 @@ class api_schema_translator:
         """Builds the aefProfiles section based on the paths and components in the API info."""
         aef_profiles = []
 
+        resources = []
         for path, methods in self.api_info.get("paths", {}).items():
-            resources = []
             for method, details in methods.items():
                 resource = {
                     "resourceName": details.get("summary", "Unnamed Resource"),
@@ -94,52 +107,52 @@ class api_schema_translator:
                 }
                 resources.append(resource)
 
-            # Example profile creation based on paths, customize as needed
-            aef_profile = {
-                "aefId": "AEF07a01ccd74a160c195e69b4f116d66",  # Placeholder AEF ID
-                "versions": [
-                    {
-                        "apiVersion": "v1",
-                        "expiry": "2100-11-30T10:32:02.004Z",
-                        "resources": resources,
-                        "custOperations": [
-                            {
-                                "commType": "REQUEST_RESPONSE",
-                                "custOpName": "string",
-                                "operations": ["POST"],
-                                "description": "string"
-                            },
-                            {
-                                "commType": "REQUEST_RESPONSE",
-                                "custOpName": "check-authentication",
-                                "operations": [
-                                    "POST"
-                                ],
-                                "description": "Check authentication request."
-                            },
-                            {
-                                "commType": "REQUEST_RESPONSE",
-                                "custOpName": "revoke-authentication",
-                                "operations": [
-                                    "POST"
-                                ],
-                                "description": "Revoke authorization for service APIs."
-                            }
-                        ]
-                    }
-                ],
-                "protocol": "HTTP_1_1",
-                "dataFormat": "JSON",
-                "securityMethods": ["Oauth"],
-                "interfaceDescriptions": [
-                    {
-                        "ipv4Addr": ip,
-                        "port": port,
-                        "securityMethods": ["Oauth"]
-                    }
-                ]
-            }
-            aef_profiles.append(aef_profile)
+        # Example profile creation based on paths, customize as needed
+        aef_profile = {
+            "aefId": "",  # Placeholder AEF ID
+            "versions": [
+                {
+                    "apiVersion": "v1",
+                    "expiry": "2100-11-30T10:32:02.004Z",
+                    "resources": resources,
+                    "custOperations": [
+                        {
+                            "commType": "REQUEST_RESPONSE",
+                            "custOpName": "string",
+                            "operations": ["POST"],
+                            "description": "string"
+                        },
+                        {
+                            "commType": "REQUEST_RESPONSE",
+                            "custOpName": "check-authentication",
+                            "operations": [
+                                "POST"
+                            ],
+                            "description": "Check authentication request."
+                        },
+                        {
+                            "commType": "REQUEST_RESPONSE",
+                            "custOpName": "revoke-authentication",
+                            "operations": [
+                                "POST"
+                            ],
+                            "description": "Revoke authorization for service APIs."
+                        }
+                    ]
+                }
+            ],
+            "protocol": "HTTP_1_1",
+            "dataFormat": "JSON",
+            "securityMethods": ["Oauth"],
+            "interfaceDescriptions": [
+                {
+                    "ipv4Addr": ip,
+                    "port": port,
+                    "securityMethods": ["Oauth"]
+                }
+            ]
+        }
+        aef_profiles.append(aef_profile)
 
         return aef_profiles
 
diff --git a/setup.py b/setup.py
index d1df2b3..1290ead 100644
--- a/setup.py
+++ b/setup.py
@@ -5,5 +5,5 @@ from setuptools import setup, find_packages
 setup(
     name='opencapif_sdk',
     packages=find_packages(include=["sdk"]),
-    version="0.1.8.2",
+    version="0.1.8.4",
 )
\ No newline at end of file
-- 
GitLab