Skip to content

Commit

Permalink
feat: enable openapi spec from url in api register
Browse files Browse the repository at this point in the history
  • Loading branch information
frankqianms committed Oct 11, 2024
1 parent ac58b53 commit 41cc197
Show file tree
Hide file tree
Showing 5 changed files with 1,805 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/apic-extension/azext_apic_extension/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
text: |
az apic api register -g api-center-test -n contosoeuap --api-location "examples/cli-examples/spec-examples/openai.json" --environment-id public
az apic api register -g api-center-test -n contosoeuap --api-location "examples/cli-examples/spec-examples/openai.yml" --environment-id public
- name: Register api by providing spec url.
text: |
az apic api register -g api-center-test -n contosoeuap --api-location "https://petstore.swagger.io/v2/swagger.json" --environment-id public
"""
59 changes: 40 additions & 19 deletions src/apic-extension/azext_apic_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,47 @@ def register_apic(cmd, api_location, resource_group, service_name, environment_i
# Load the JSON file
if api_location:

# TODO Future Confirm its a file and not link
with open(str(api_location), 'rb') as f:
rawdata = f.read()
result = chardet.detect(rawdata)
encoding = result['encoding']

# TODO - read other file types later
value = None
if str(api_location).endswith('.yaml') or str(api_location).endswith('.yml'):
with open(str(api_location), 'r', encoding=encoding) as f:
content = f.read()
data = yaml.safe_load(content)
if data:
value = content
if (str(api_location).endswith('.json')):
with open(str(api_location), 'r', encoding=encoding) as f:
content = f.read()
data = json.loads(content)
if data:
value = content
# Read the spec content from URL
if str(api_location).startswith('https://') or str(api_location).startswith('http://'):
try:
# Fetch the content from the URL
response = requests.get(api_location)
# Raise an error for bad status codes
response.raise_for_status()
# Try to parse the content as JSON
try:
data = json.loads(response.content)
except json.JSONDecodeError:
try:
# If JSON parsing fails, try to parse as YAML
data = yaml.safe_load(response.content)
except yaml.YAMLError as e:
logger.error("Error parsing data from %s: %s", api_location, e)
data = None
# If we could parse the content(json or yaml), convert it to a json format string
value = json.dumps(data) if data else None
except requests.exceptions.RequestException as e:
logger.error("Error fetching data from %s: %s", api_location, e)
value = None
else:
# Confirm its a file and not link
with open(str(api_location), 'rb') as f:
rawdata = f.read()
result = chardet.detect(rawdata)
encoding = result['encoding']

# TODO - read other file types later
if str(api_location).endswith('.yaml') or str(api_location).endswith('.yml'):
with open(str(api_location), 'r', encoding=encoding) as f:
content = f.read()
data = yaml.safe_load(content)
value = content if data else None
if (str(api_location).endswith('.json')):
with open(str(api_location), 'r', encoding=encoding) as f:
content = f.read()
data = json.loads(content)
value = content if data else None

# If we could not read the file, return error
if value is None:
Expand Down
Loading

0 comments on commit 41cc197

Please sign in to comment.