github.com/Axway/agent-sdk@v1.1.101/pkg/apic/specasyncapiprocessor.go (about)

     1  package apic
     2  
     3  import (
     4  	"net/url"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type asyncAPIProcessor struct {
    10  	asyncapiDef map[string]interface{}
    11  	spec        []byte
    12  }
    13  
    14  func newAsyncAPIProcessor(asyncapiDef map[string]interface{}, spec []byte) *asyncAPIProcessor {
    15  	return &asyncAPIProcessor{asyncapiDef: asyncapiDef, spec: spec}
    16  }
    17  
    18  func (p *asyncAPIProcessor) GetResourceType() string {
    19  	return AsyncAPI
    20  }
    21  
    22  // GetVersion -
    23  func (p *asyncAPIProcessor) GetVersion() string {
    24  	info := p.asyncapiDef["info"]
    25  	if info != nil {
    26  		if infoDetail, ok := info.(map[string]interface{}); ok {
    27  			version := infoDetail["version"]
    28  			if version != nil {
    29  				return version.(string)
    30  			}
    31  		}
    32  	}
    33  	return ""
    34  }
    35  
    36  // GetDescription -
    37  func (p *asyncAPIProcessor) GetDescription() string {
    38  	info := p.asyncapiDef["info"]
    39  	if info != nil {
    40  		if infoDetail, ok := info.(map[string]interface{}); ok {
    41  			description := infoDetail["description"]
    42  			if description != nil {
    43  				return description.(string)
    44  			}
    45  		}
    46  	}
    47  	return ""
    48  }
    49  
    50  // GetEndPoints -
    51  func (p *asyncAPIProcessor) GetEndpoints() ([]EndpointDefinition, error) {
    52  	endpoints := make([]EndpointDefinition, 0)
    53  	var err error
    54  	servers := p.asyncapiDef["servers"]
    55  	if servers != nil {
    56  		if serverList, ok := servers.(map[string]interface{}); ok {
    57  			endpoints, err = p.parseServerList(serverList)
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  		}
    62  	}
    63  
    64  	return endpoints, nil
    65  }
    66  
    67  func (p *asyncAPIProcessor) parseServerList(serverList map[string]interface{}) ([]EndpointDefinition, error) {
    68  	endpoints := make([]EndpointDefinition, 0)
    69  	for _, value := range serverList {
    70  		serverObjInterface, ok := value.(map[string]interface{})
    71  		if ok {
    72  			endpoint, err := p.parseServerObject(serverObjInterface)
    73  			if err != nil {
    74  				return nil, err
    75  			}
    76  			endpoints = append(endpoints, endpoint)
    77  		}
    78  	}
    79  	return endpoints, nil
    80  }
    81  
    82  func (p *asyncAPIProcessor) parseServerObject(serverObjInterface map[string]interface{}) (EndpointDefinition, error) {
    83  	var err error
    84  	protocol := ""
    85  	serverURL := ""
    86  	var serverVariables map[string]string
    87  	serverDetails := map[string]interface{}{}
    88  	for key, valueInterface := range serverObjInterface {
    89  		value, ok := valueInterface.(string)
    90  		if ok {
    91  			if key == "protocol" {
    92  				protocol = value
    93  			}
    94  			if key == "url" {
    95  				serverURL = value
    96  			}
    97  		}
    98  		if key == "variables" {
    99  			variablesInterface, ok := valueInterface.(map[string]interface{})
   100  			if ok {
   101  				serverVariables, _ = p.parseVariables(variablesInterface)
   102  			}
   103  		}
   104  		if key == "bindings" {
   105  			serverDetails = valueInterface.(map[string]interface{})
   106  		}
   107  	}
   108  	endpoint := EndpointDefinition{}
   109  	endpoint.Protocol = protocol
   110  	// variable substitution
   111  	for varName, varValue := range serverVariables {
   112  		serverURL = strings.ReplaceAll(serverURL, "{"+varName+"}", varValue)
   113  	}
   114  
   115  	parseURL, err := url.Parse(protocol + "://" + serverURL)
   116  	endpoint.Host = parseURL.Hostname()
   117  	port, _ := strconv.Atoi(parseURL.Port())
   118  	endpoint.Port = int32(port)
   119  	endpoint.BasePath = parseURL.Path
   120  	endpoint.Details = serverDetails
   121  	return endpoint, err
   122  }
   123  
   124  func (p *asyncAPIProcessor) parseVariables(variablesObjInterface map[string]interface{}) (map[string]string, error) {
   125  	serverVars := make(map[string]string)
   126  	for varName, varObject := range variablesObjInterface {
   127  		varObjectInterface, ok := varObject.(map[string]interface{})
   128  		if ok {
   129  			varValue := p.parseVariableObject(varObjectInterface)
   130  			serverVars[varName] = varValue
   131  		}
   132  	}
   133  	return serverVars, nil
   134  }
   135  
   136  func (p *asyncAPIProcessor) parseVariableObject(serverObjInterface map[string]interface{}) string {
   137  	varDefaultValue := ""
   138  	for key, valueInterface := range serverObjInterface {
   139  		if key == "default" {
   140  			value, ok := valueInterface.(string)
   141  			if ok {
   142  				varDefaultValue = value
   143  			}
   144  		}
   145  	}
   146  	return varDefaultValue
   147  }
   148  
   149  // GetSpecBytes -
   150  func (p *asyncAPIProcessor) GetSpecBytes() []byte {
   151  	return p.spec
   152  }