Esempio n. 1
0
// New creates a new default runtime for a swagger api client.
func New(swaggerSpec *spec.Document) *Runtime {
	var rt Runtime
	rt.DefaultMediaType = httpkit.JSONMime

	// TODO: actually infer this stuff from the spec
	rt.Consumers = map[string]httpkit.Consumer{
		httpkit.JSONMime: httpkit.JSONConsumer(),
	}
	rt.Producers = map[string]httpkit.Producer{
		httpkit.JSONMime: httpkit.JSONProducer(),
	}
	rt.Spec = swaggerSpec
	rt.Transport = http.DefaultTransport
	rt.client = http.DefaultClient
	rt.client.Transport = rt.Transport
	rt.Host = swaggerSpec.Host()
	rt.BasePath = swaggerSpec.BasePath()
	schemes := swaggerSpec.Spec().Schemes
	if len(schemes) == 0 {
		schemes = append(schemes, "http")
	}
	rt.methodsAndPaths = make(map[string]methodAndPath)
	for mth, pathItem := range rt.Spec.Operations() {
		for pth, op := range pathItem {
			if len(op.Schemes) > 0 {
				rt.methodsAndPaths[op.ID] = methodAndPath{mth, pth, op.Schemes}
			} else {
				rt.methodsAndPaths[op.ID] = methodAndPath{mth, pth, schemes}
			}
		}
	}
	return &rt
}
Esempio n. 2
0
func AddSwaggerRoutes(doc *spec.Document) {
	for path, pathItem := range doc.AllPaths() {
		path = doc.BasePath() + deCurly(path)

		// TODO may be something usable here?
		// if extension, ok := pathItem.Extensions.GetString("x-revel-controller"); ok { }

		if pathItem.Head != nil {
			if action, ok := pathItem.Head.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "HEAD", path))
			}
		}

		if pathItem.Get != nil {
			if action, ok := pathItem.Get.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "GET", path))
			}
		}

		if pathItem.Post != nil {
			if action, ok := pathItem.Post.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "POST", path))
			}
		}

		if pathItem.Put != nil {
			if action, ok := pathItem.Put.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "PUT", path))
			}
		}

		if pathItem.Delete != nil {
			if action, ok := pathItem.Delete.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "DELETE", path))
			}
		}

		if pathItem.Patch != nil {
			if action, ok := pathItem.Patch.Extensions.GetString(X_REVEL_CONTROLLER_ACTION); ok {
				_fail(addRoute(action, "PATCH", path))
			}
		}

		if pathItem.Options != nil {
			if action, ok := pathItem.Options.Extensions.GetString("x-revel-controller-action"); ok {
				_fail(addRoute(action, "OPTIONS", path))
			}
		}
	}
}
Esempio n. 3
0
// New creates a new default runtime for a swagger api client.
func New(swaggerSpec *spec.Document) *Runtime {
	var rt Runtime
	rt.DefaultMediaType = httpkit.JSONMime
	rt.Consumers = map[string]httpkit.Consumer{
		httpkit.JSONMime: httpkit.JSONConsumer(),
	}
	rt.Producers = map[string]httpkit.Producer{
		httpkit.JSONMime: httpkit.JSONProducer(),
	}
	rt.Spec = swaggerSpec
	rt.Transport = http.DefaultTransport
	rt.client = http.DefaultClient
	rt.Host = swaggerSpec.Host()
	rt.BasePath = swaggerSpec.BasePath()
	rt.methodsAndPaths = make(map[string]methodAndPath)
	for mth, pathItem := range rt.Spec.Operations() {
		for pth, op := range pathItem {
			rt.methodsAndPaths[op.ID] = methodAndPath{mth, pth}
		}
	}
	return &rt
}
Esempio n. 4
0
// New creates a new default runtime for a swagger api client.
func New(swaggerSpec *spec.Document) *Runtime {
	var rt Runtime
	rt.DefaultMediaType = httpkit.JSONMime

	// TODO: actually infer this stuff from the spec
	rt.Consumers = map[string]httpkit.Consumer{
		httpkit.JSONMime: httpkit.JSONConsumer(),
	}
	rt.Producers = map[string]httpkit.Producer{
		httpkit.JSONMime: httpkit.JSONProducer(),
	}
	rt.Spec = swaggerSpec
	rt.Transport = http.DefaultTransport
	rt.client = http.DefaultClient
	rt.client.Transport = rt.Transport
	rt.Host = swaggerSpec.Host()
	rt.BasePath = swaggerSpec.BasePath()
	if !strings.HasPrefix(rt.BasePath, "/") {
		rt.BasePath = "/" + rt.BasePath
	}
	rt.Debug = os.Getenv("DEBUG") == "1"
	schemes := swaggerSpec.Spec().Schemes
	if len(schemes) == 0 {
		schemes = append(schemes, "http")
	}
	rt.methodsAndPaths = make(map[string]methodAndPath)
	for mth, pathItem := range rt.Spec.Operations() {
		for pth, op := range pathItem {
			nm := ensureUniqueName(op.ID, mth, pth, rt.methodsAndPaths)
			op.ID = nm
			if len(op.Schemes) > 0 {
				rt.methodsAndPaths[nm] = methodAndPath{mth, pth, op.Schemes}
			} else {
				rt.methodsAndPaths[nm] = methodAndPath{mth, pth, schemes}
			}
		}
	}
	return &rt
}