示例#1
0
// NewClient creates a new Golang client
func NewClient(apiDef *raml.APIDefinition, packageName, rootImportPath string) (Client, error) {
	// rootImportPath only needed if we use libraries
	if rootImportPath == "" && len(apiDef.Libraries) > 0 {
		return Client{}, fmt.Errorf("--import-path can't be empty when we use libraries")
	}

	globRootImportPath = rootImportPath
	globAPIDef = apiDef

	services := map[string]*ClientService{}
	for k, v := range apiDef.Resources {
		rd := resource.New(apiDef, commons.NormalizeURITitle(apiDef.Title), packageName)
		rd.GenerateMethods(&v, langGo, newServerMethod, newGoClientMethod)
		services[k] = &ClientService{
			rootEndpoint: k,
			PackageName:  packageName,
			Methods:      rd.Methods,
		}
	}
	client := Client{
		apiDef:         apiDef,
		Name:           commons.NormalizeURI(apiDef.Title),
		BaseURI:        apiDef.BaseURI,
		libraries:      apiDef.Libraries,
		PackageName:    packageName,
		RootImportPath: rootImportPath,
		Services:       services,
	}

	if strings.Index(client.BaseURI, "{version}") > 0 {
		client.BaseURI = strings.Replace(client.BaseURI, "{version}", apiDef.Version, -1)
	}
	return client, nil
}
示例#2
0
// setup go server method, initializes all needed variables
func (gm *serverMethod) setup(apiDef *raml.APIDefinition, r *raml.Resource, rd *resource.Resource, methodName string) error {
	// set method name
	name := commons.NormalizeURI(gm.Endpoint)
	if len(gm.DisplayName) > 0 {
		gm.MethodName = commons.DisplayNameToFuncName(gm.DisplayName)
	} else {
		gm.MethodName = name[len(rd.Name):] + methodName
	}

	// setting middlewares
	middlewares := []string{}

	// security middlewares
	for _, v := range gm.SecuredBy {
		if !security.ValidateScheme(v.Name, apiDef) {
			continue
		}
		// oauth2 middleware
		m, err := getOauth2MwrHandler(v)
		if err != nil {
			return err
		}
		middlewares = append(middlewares, m)
	}

	gm.Middlewares = strings.Join(middlewares, ", ")

	return nil
}
示例#3
0
// New creates a resource definition
func New(apiDef *raml.APIDefinition, endpoint, packageName string) Resource {
	return Resource{
		Endpoint:    endpoint,
		APIDef:      apiDef,
		Name:        strings.Title(commons.NormalizeURI(endpoint)),
		PackageName: packageName,
	}
}
示例#4
0
func newMethod(r *raml.Resource, rd *cr.Resource, m *raml.Method,
	methodName string) (cr.MethodInterface, error) {

	rm := cr.NewMethod(r, rd, m, methodName, setBodyName)

	// set method name
	if len(rm.DisplayName) > 0 {
		rm.MethodName = strings.Replace(rm.DisplayName, " ", "", -1)
	} else {
		rm.MethodName = commons.NormalizeURI(formatProcName(r.FullURI())) + methodName
	}
	rm.ResourcePath = commons.ParamizingURI(rm.Endpoint, "&")
	return method{Method: &rm}, nil
}
示例#5
0
// setBodyName set name of method's request/response body.
//
// Rules:
//  - use bodies.Type if not empty and not `object`
//  - use bodies.ApplicationJSON.Type if not empty and not `object`
//  - use prefix+suffix if:
//      - not meet previous rules
//      - previous rules produces JSON string
func setBodyName(bodies raml.Bodies, prefix, suffix string) string {
	var tipe string

	prefix = commons.NormalizeURI(prefix)

	if len(bodies.Type) > 0 && bodies.Type != "object" {
		tipe = toNimType(bodies.Type)
	} else if bodies.ApplicationJSON != nil {
		if bodies.ApplicationJSON.Type != "" && bodies.ApplicationJSON.Type != "object" {
			tipe = toNimType(bodies.ApplicationJSON.Type)
		} else {
			tipe = prefix + suffix
		}
	}

	if commons.IsJSONString(tipe) {
		tipe = prefix + suffix
	}

	return tipe
}
示例#6
0
// NewClient creates a python Client
func NewClient(apiDef *raml.APIDefinition) Client {
	services := map[string]*service{}
	for k, v := range apiDef.Resources {
		rd := resource.New(apiDef, commons.NormalizeURITitle(apiDef.Title), "")
		rd.GenerateMethods(&v, "python", newServerMethod, newClientMethod)
		services[k] = &service{
			rootEndpoint: k,
			Methods:      rd.Methods,
		}
	}
	c := Client{
		Name:     commons.NormalizeURI(apiDef.Title),
		APIDef:   apiDef,
		BaseURI:  apiDef.BaseURI,
		Services: services,
	}
	if strings.Index(c.BaseURI, "{version}") > 0 {
		c.BaseURI = strings.Replace(c.BaseURI, "{version}", apiDef.Version, -1)
	}
	return c
}
示例#7
0
func _snakeCaseResourceURI(r *raml.Resource, completeURI string) string {
	if r == nil {
		return completeURI
	}
	var snake string
	if len(r.URI) > 0 {
		uri := commons.NormalizeURI(r.URI)
		if r.Parent != nil { // not root resource, need to add "_"
			snake = "_"
		}

		if strings.HasPrefix(r.URI, "/{") {
			snake += "by" + strings.ToUpper(uri[:1])
		} else {
			snake += strings.ToLower(uri[:1])
		}

		if len(uri) > 1 { // append with the rest of uri
			snake += uri[1:]
		}
	}
	return _snakeCaseResourceURI(r.Parent, snake+completeURI)
}