Exemplo n.º 1
0
func (pcm *clientMethod) setup() {
	prArgs := []string{"uri"}  // prArgs are arguments we supply to python request
	params := []string{"self"} // params are method signature params

	// for method with request body, we add `data` argument
	if pcm.Verb() == "PUT" || pcm.Verb() == "POST" || pcm.Verb() == "PATCH" {
		params = append(params, "data")
		prArgs = append(prArgs, "data")
	}

	// construct prArgs string from the array
	prArgs = append(prArgs, "headers=headers", "params=query_params")
	pcm.PRArgs = strings.Join(prArgs, ", ")

	// construct method signature
	params = append(params, resource.GetResourceParams(pcm.Resource())...)
	params = append(params, "headers=None", "query_params=None")
	pcm.Params = strings.Join(params, ", ")

	// python request call
	// we encapsulate the call to put, post, and patch.
	// To be able to accept plain string or dict.
	// if it is a dict, we encode it to json
	if pcm.Verb() == "PUT" || pcm.Verb() == "POST" || pcm.Verb() == "PATCH" {
		pcm.PRCall = fmt.Sprintf("self.client.%v", strings.ToLower(pcm.Verb()))
	} else {
		pcm.PRCall = fmt.Sprintf("self.client.session.%v", strings.ToLower(pcm.Verb()))
	}

	if len(pcm.DisplayName) > 0 {
		pcm.MethodName = commons.DisplayNameToFuncName(pcm.DisplayName)
	} else {
		pcm.MethodName = snakeCaseResourceURI(pcm.Resource()) + "_" + strings.ToLower(pcm.Verb())
	}
}
Exemplo n.º 2
0
// setup sets all needed variables
func (sm *serverMethod) setup(apiDef *raml.APIDefinition, r *raml.Resource, rd *resource.Resource) error {
	// method name
	if len(sm.DisplayName) > 0 {
		sm.MethodName = commons.DisplayNameToFuncName(sm.DisplayName)
	} else {
		sm.MethodName = snakeCaseResourceURI(r) + "_" + strings.ToLower(sm.Verb())
	}
	sm.Params = strings.Join(resource.GetResourceParams(r), ", ")
	sm.Endpoint = strings.Replace(sm.Endpoint, "{", "<", -1)
	sm.Endpoint = strings.Replace(sm.Endpoint, "}", ">", -1)

	// security middlewares
	for _, v := range sm.SecuredBy {
		if !security.ValidateScheme(v.Name, apiDef) {
			continue
		}
		// oauth2 middleware
		m, err := newPythonOauth2Middleware(v)
		if err != nil {
			log.Errorf("error creating middleware for method.err = %v", err)
			return err
		}
		sm.MiddlewaresArr = append(sm.MiddlewaresArr, m)
	}
	return nil
}
Exemplo n.º 3
0
// CallParams are params when we call jester handler
func (m method) ServerCallParams() string {
	var params []string

	for _, p := range cr.GetResourceParams(m.Resource()) {
		params = append(params, fmt.Sprintf(`@"%v"`, p))
	}

	params = append(params, "request")
	return strings.Join(params, ", ")
}
Exemplo n.º 4
0
// ProcParams are params of this jester endpoint handler
func (m method) ServerProcParams() string {
	var params []string

	for _, p := range cr.GetResourceParams(m.Resource()) {
		params = append(params, p+": string")
	}

	params = append(params, "req: Request")
	return strings.Join(params, ", ")
}
Exemplo n.º 5
0
func (m method) ClientProcParams() string {
	params := []string{}

	if m.ReqBody != "" {
		params = append(params, fmt.Sprintf("reqBody: %v", m.ReqBody))
	}

	for _, p := range cr.GetResourceParams(m.Resource()) {
		params = append(params, fmt.Sprintf("%v: string", p))
	}

	str := strings.Join(params, ", ")
	if str != "" {
		str = ", " + str
	}
	return str
}
Exemplo n.º 6
0
func (gcm *clientMethod) setup(methodName string) error {
	// build func/method params
	buildParams := func(r *raml.Resource, bodyType string) (string, error) {
		paramsStr := strings.Join(resource.GetResourceParams(r), ",")
		if len(paramsStr) > 0 {
			paramsStr += " string"
		}

		// append request body type
		if len(bodyType) > 0 {
			if len(paramsStr) > 0 {
				paramsStr += ", "
			}
			paramsStr += strings.ToLower(bodyType) + " " + bodyType
		}

		// append header
		if len(paramsStr) > 0 {
			paramsStr += ","
		}
		paramsStr += "headers,queryParams map[string]interface{}"

		return paramsStr, nil
	}

	// method name
	name := commons.NormalizeURITitle(gcm.Endpoint)

	if len(gcm.DisplayName) > 0 {
		gcm.MethodName = commons.DisplayNameToFuncName(gcm.DisplayName)
	} else {
		gcm.MethodName = strings.Title(name + methodName)
	}

	// method param
	methodParam, err := buildParams(gcm.RAMLResource, gcm.ReqBody)
	if err != nil {
		return err
	}
	gcm.Params = methodParam

	return nil
}