// 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 }
// create client resource's method func newGoClientMethod(r *raml.Resource, rd *resource.Resource, m *raml.Method, methodName string) (resource.MethodInterface, error) { method := resource.NewMethod(r, rd, m, methodName, setBodyName) method.ResourcePath = commons.ParamizingURI(method.Endpoint, "+") name := commons.NormalizeURITitle(method.Endpoint) method.ReqBody = setBodyName(m.Bodies, name+methodName, "ReqBody") gcm := clientMethod{Method: &method} err := gcm.setup(methodName) return gcm, err }
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 }
// 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.NormalizeURITitle(prefix) if len(bodies.Type) > 0 && bodies.Type != "object" { tipe = convertToGoType(bodies.Type) } else if bodies.ApplicationJSON != nil { if bodies.ApplicationJSON.Type != "" && bodies.ApplicationJSON.Type != "object" { tipe = convertToGoType(bodies.ApplicationJSON.Type) } else { tipe = prefix + suffix } } if commons.IsJSONString(tipe) { tipe = prefix + suffix } return tipe }
// 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 }
// generate all structs from resource's method's request & response body func generateStructsFromResourceBody(resourcePath, dir, packageName, lang string, r *raml.Resource) error { if r == nil { return nil } // build var methods = []struct { Name string Method *raml.Method }{ {Name: "Get", Method: r.Get}, {"Post", r.Post}, {"Head", r.Head}, {"Put", r.Put}, {"Delete", r.Delete}, {"Patch", r.Patch}, {"Options", r.Options}, } normalizedPath := commons.NormalizeURITitle(resourcePath + r.URI) for _, v := range methods { if err := buildBodyFromMethod(normalizedPath, v.Name, dir, packageName, lang, v.Method); err != nil { return err } } // build request/response body of child resources for _, v := range r.Nested { if err := generateStructsFromResourceBody(resourcePath+r.URI, dir, packageName, lang, v); err != nil { return err } } return nil }