New Release. This version can import JSON or YAML API format (OpenAPI or Swagger)

This commit is contained in:
2024-02-15 10:03:29 -03:00
parent e7df46adba
commit 1fc4aad29d
5 changed files with 85 additions and 33 deletions

View File

@@ -6,6 +6,7 @@ import oci
import requests import requests
import time import time
from itertools import groupby from itertools import groupby
import yaml
#### IDCS Routines #### IDCS Routines
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction #### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
@@ -475,6 +476,23 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
source="EXAMPLE-source-Value", source="EXAMPLE-source-Value",
type="EXAMPLE-type-Value")])) type="EXAMPLE-type-Value")]))
def is_json(swagger):
try:
body = json.loads(swagger)
return True
except:
try:
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
s = json.dumps(yaml_object, indent=2)
return False
except:
return False
def convert_json(swagger):
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
return json.dumps(yaml_object, indent=2)
### ###
def handler(ctx, data: io.BytesIO = None): def handler(ctx, data: io.BytesIO = None):
@@ -494,13 +512,13 @@ def handler(ctx, data: io.BytesIO = None):
body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"] body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"]
# body content # body content
swagger = str(body) swagger = str(body)
body = json.loads(body) if (is_json(swagger)):
body = json.loads(body)
else:
body = json.loads(convert_json(swagger))
swagger = convert_json(swagger)
# functions context variables environment = "DEV"
# fn config app ociapigw-app oci_region us-ashburn-1
# fn config app ociapigw-app environment QA
oci_region = app_context['oci_region']
environment = app_context['environment']
# header values # header values
access_token = header["token"] access_token = header["token"]
@@ -547,7 +565,6 @@ def handler(ctx, data: io.BytesIO = None):
"active": True, "active": True,
"context": { "context": {
"environment": environment, "environment": environment,
"oci_region": oci_region,
"api_id": api_id "api_id": api_id
}}) }})

View File

@@ -6,4 +6,4 @@ six
PyJWT PyJWT
py3_lru_cache py3_lru_cache
simplejson simplejson
PyYAML

View File

@@ -4,6 +4,7 @@ import io
from fdk import response from fdk import response
import oci import oci
import requests import requests
import yaml
#### IDCS Routines #### IDCS Routines
#### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction #### https://docs.oracle.com/en/learn/apigw-modeldeployment/index.html#introduction
@@ -123,7 +124,7 @@ def process_api_spec(displayName, compartmentId, environment, swagger):
except(Exception) as ex: except(Exception) as ex:
jsonData = 'error parsing json payload: ' + str(ex) jsonData = 'error parsing json payload: ' + str(ex)
put_logs_response = logging.put_logs( put_logs_response = logging.put_logs(
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
put_logs_details=oci.loggingingestion.models.PutLogsDetails( put_logs_details=oci.loggingingestion.models.PutLogsDetails(
specversion="EXAMPLE-specversion-Value", specversion="EXAMPLE-specversion-Value",
log_entry_batches=[ log_entry_batches=[
@@ -135,6 +136,22 @@ def process_api_spec(displayName, compartmentId, environment, swagger):
source="EXAMPLE-source-Value", source="EXAMPLE-source-Value",
type="EXAMPLE-type-Value")])) type="EXAMPLE-type-Value")]))
def is_json(swagger):
try:
body = json.loads(swagger)
return True
except:
try:
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
s = json.dumps(yaml_object, indent=2)
return False
except:
return False
def convert_json(swagger):
yaml_object = yaml.safe_load(swagger) # yaml_object will be a list or a dict
return json.dumps(yaml_object, indent=2)
### ###
def handler(ctx, data: io.BytesIO = None): def handler(ctx, data: io.BytesIO = None):
@@ -154,9 +171,13 @@ def handler(ctx, data: io.BytesIO = None):
body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"] body = dict(json.loads(data.getvalue().decode('utf-8')).get("data"))["body"]
# body content # body content
swagger = str(body) swagger = str(body)
body = json.loads(body) if (is_json(swagger)):
body = json.loads(body)
else:
body = json.loads(convert_json(swagger))
swagger = convert_json(swagger)
environment = "DEV" #for future development environment = "DEV"
# header values # header values
access_token = header["token"] access_token = header["token"]
@@ -174,17 +195,17 @@ def handler(ctx, data: io.BytesIO = None):
except(Exception) as ex1: except(Exception) as ex1:
jsonData = 'error parsing json payload: ' + str(ex1) jsonData = 'error parsing json payload: ' + str(ex1)
put_logs_response = logging.put_logs( put_logs_response = logging.put_logs(
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
put_logs_details=oci.loggingingestion.models.PutLogsDetails( put_logs_details=oci.loggingingestion.models.PutLogsDetails(
specversion="EXAMPLE-specversion-Value", specversion="EXAMPLE-specversion-Value",
log_entry_batches=[ log_entry_batches=[
oci.loggingingestion.models.LogEntryBatch( oci.loggingingestion.models.LogEntryBatch(
entries=[ entries=[
oci.loggingingestion.models.LogEntry( oci.loggingingestion.models.LogEntry(
data="error: " + jsonData, data="error: " + jsonData,
id="ocid1.test.oc1..00000001.EXAMPLE-id-Value")], id="ocid1.test.oc1..00000001.EXAMPLE-id-Value")],
source="EXAMPLE-source-Value", source="EXAMPLE-source-Value",
type="EXAMPLE-type-Value")])) type="EXAMPLE-type-Value")]))
return response.Response( return response.Response(
ctx, ctx,
@@ -196,23 +217,37 @@ def handler(ctx, data: io.BytesIO = None):
api_id = process_api_spec(displayName, compartmentId, environment, swagger) api_id = process_api_spec(displayName, compartmentId, environment, swagger)
rdata = json.dumps({ rdata = json.dumps({
"active": True, "active": True,
"context": { "context": {
"environment": environment, "environment": environment,
"display_name": displayName, "display_name": displayName,
"api_id": json.dumps(api_id) "api_id": json.dumps(api_id)
}}) }})
# put_logs_response = logging.put_logs(
# log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
# put_logs_details=oci.loggingingestion.models.PutLogsDetails(
# specversion="EXAMPLE-specversion-Value",
# log_entry_batches=[
# oci.loggingingestion.models.LogEntryBatch(
# entries=[
# oci.loggingingestion.models.LogEntry(
# data="request payload: " + json.dumps(header),
# id="ocid1.test.oc1..00000001.EXAMPLE-id-Value-1")],
# source="EXAMPLE-source-Value",
# type="EXAMPLE-type-Value")]))
return response.Response( return response.Response(
ctx, response_data=rdata, ctx, response_data=rdata,
status_code=200, status_code=200,
headers={"Content-Type": "application/json", "data": rdata} headers={"Content-Type": "application/json", "data": rdata}
) )
except(Exception) as ex: except(Exception) as ex:
jsonData = 'error parsing json payload: ' + str(ex) jsonData = 'error parsing json payload: ' + str(ex)
put_logs_response = logging.put_logs( put_logs_response = logging.put_logs(
log_id="ocid1.log.oc1.iad.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", log_id="ocid1.log.oc1.iad.amaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaanamaaaaaan",
put_logs_details=oci.loggingingestion.models.PutLogsDetails( put_logs_details=oci.loggingingestion.models.PutLogsDetails(
specversion="EXAMPLE-specversion-Value", specversion="EXAMPLE-specversion-Value",
log_entry_batches=[ log_entry_batches=[

View File

@@ -6,4 +6,4 @@ six
PyJWT PyJWT
py3_lru_cache py3_lru_cache
simplejson simplejson
PyYAML