Example #1
0
File: swagger.go Project: iwat/tyk
func (s *SwaggerAST) ConvertIntoApiVersion(asMock bool) (tykcommon.VersionInfo, error) {
	thisVersionInfo := tykcommon.VersionInfo{}

	if asMock {
		return thisVersionInfo, errors.New("Swagger mocks not supported")
	}

	thisVersionInfo.UseExtendedPaths = true
	thisVersionInfo.Name = s.Info.Version
	thisVersionInfo.ExtendedPaths.WhiteList = make([]tykcommon.EndPointMeta, 0)

	if len(s.Paths) == 0 {
		return thisVersionInfo, errors.New("No paths defined in swagger file!")
	}
	for pathName, methods := range s.Paths {
		newEndpointMeta := tykcommon.EndPointMeta{}
		newEndpointMeta.MethodActions = make(map[string]tykcommon.EndpointMethodMeta)
		newEndpointMeta.Path = pathName

		// We just want the paths here, no mocks
		for methodName, _ := range methods {
			thisMethodAction := tykcommon.EndpointMethodMeta{}
			thisMethodAction.Action = tykcommon.NoAction
			newEndpointMeta.MethodActions[strings.ToUpper(methodName)] = thisMethodAction
		}

		thisVersionInfo.ExtendedPaths.WhiteList = append(thisVersionInfo.ExtendedPaths.WhiteList, newEndpointMeta)
	}

	return thisVersionInfo, nil
}
Example #2
0
func (b *BluePrintAST) ConvertIntoApiVersion(asMock bool) (tykcommon.VersionInfo, error) {
	thisVersionInfo := tykcommon.VersionInfo{}
	thisVersionInfo.UseExtendedPaths = true
	thisVersionInfo.Name = b.Name

	if len(b.ResourceGroups) < 1 {
		return thisVersionInfo, errors.New("There are no resource groups defined in this blueprint, are you sure it is correctly formatted?")
	}

	for _, resourceGroup := range b.ResourceGroups {
		if len(resourceGroup.Resources) < 1 {
			return thisVersionInfo, errors.New("No resourcs defined in the resource group.")
		}

		for _, resource := range resourceGroup.Resources {
			newMetaData := tykcommon.EndPointMeta{}
			newMetaData.Path = resource.UriTemplate
			newMetaData.MethodActions = make(map[string]tykcommon.EndpointMethodMeta)

			for _, action := range resource.Actions {
				if len(action.Examples) > 0 {
					if len(action.Examples[0].Responses) > 0 {
						thisEndPointMethodMeta := tykcommon.EndpointMethodMeta{}
						code, err := strconv.Atoi(action.Examples[0].Responses[0].Name)
						if err != nil {
							log.Warning("Could not genrate response code form Name field, using 200")
							code = 200
						}
						thisEndPointMethodMeta.Code = code

						if asMock {
							thisEndPointMethodMeta.Action = tykcommon.Reply
						} else {
							thisEndPointMethodMeta.Action = tykcommon.NoAction
						}

						for _, h := range action.Examples[0].Responses[0].Headers {
							thisEndPointMethodMeta.Headers = make(map[string]string)
							thisEndPointMethodMeta.Headers[h.Name] = h.Value
						}
						thisEndPointMethodMeta.Data = action.Examples[0].Responses[0].Body
						newMetaData.MethodActions[action.Method] = thisEndPointMethodMeta
					}
				}
			}

			// Add it to the version
			thisVersionInfo.ExtendedPaths.WhiteList = make([]tykcommon.EndPointMeta, 0)
			thisVersionInfo.ExtendedPaths.WhiteList = append(thisVersionInfo.ExtendedPaths.WhiteList, newMetaData)
		}

	}

	return thisVersionInfo, nil
}