diff --git a/example/client.py.plush b/example/client.py.plush new file mode 100644 index 0000000..f2ba54d --- /dev/null +++ b/example/client.py.plush @@ -0,0 +1,94 @@ +# Code generated by oto; DO NOT EDIT. + +import requests +import json + + +class Client: + """Client provides access to the Greeter Service.""" + + def __init__(self, endpoint="http://localhost:8080/oto"): + self.endpoint = endpoint + if self.endpoint == "": + raise FieldError(field="endpoint", message="endpoint missing") + + +<%= for (object) in def.Objects { %>class <%= object.Name %>: + """ + <%= format_comment_line(object.Comment) %> + + Attributes + ---------- + <%= for (field) in object.Fields { %><%=field.NameLowerCamel%> : <%= field.Type.PythonType %> + <%= format_comment_line(field.Comment) %> + <% } %> + """ + + def __init__(self<%= for (field) in object.Fields { %>, <%= field.NameLowerCamel %>: <%= field.Type.PythonType %><% } %>) -> None: + """ + Constructs all of the neccesary attributes for the <%= object.Name %> + + <%= for (field) in object.Fields { %><%=field.NameLowerCamel%> : <%= field.Type.PythonType %> + <%= format_comment_line(field.Comment) %> + <% } %> + """ + <%= for (field) in object.Fields { %>self.<%= field.NameLowerCamel %> = <%= field.NameLowerCamel %> + <% } %> +<% } %> +<%= for (service) in def.Services { %>class <%= service.Name %>: + """<%= format_comment_line(service.Comment) %>""" + + def __init__(self, client): + self.client = client + <%= for (method) in service.Methods { %> + def <%= method.NameLowerCamel %>(self, <%= method.InputObject.ObjectName %>: <%= method.InputObject.ObjectName %>) -> <%=method.OutputObject.ObjectName%>: + """ + <%= format_comment_line(method.Comment) %> + + :param <%= method.InputObject.ObjectName %> <%= method.InputObject.ObjectName %>: + + """ + url = "{}/<%= service.Name %>.<%= method.Name %>".format(self.client.endpoint) + headers = { + 'Accept': 'application/json; charset=utf8', + 'Content-Type': 'application/json; charset=utf8' + } + r = requests.post(url, json=<%= method.InputObject.ObjectName %>, headers=headers) + if r.status_code != 200: + raise OtoError(message="status code: {}".format(r.status_code)) + j = r.json() + if 'error' in j: + err = j.get('error') + if err != '': + raise OtoError(message=err) + return j + <% } %> +<% } %> + +class Error(Exception): + """Base class for exceptions in this module.""" + pass + + +class OtoError(Error): + """Exception raised for an error making the request. + + Attributes: + message -- explanation of the error + """ + + def __init__(self, message): + self.message = message + + +class FieldError(Error): + """Exception raised for missing fields. + + Attributes: + field -- field which the error occurred + message -- explanation of the error + """ + + def __init__(self, field, message): + self.field = field + self.message = message diff --git a/example/generate.sh b/example/generate.sh index 66f2751..d8bb0ad 100755 --- a/example/generate.sh +++ b/example/generate.sh @@ -13,6 +13,12 @@ oto -template client.js.plush \ ./def echo "generated client.gen.js" +oto -template client.py.plush \ + -out ./python/clientgen.py \ + -pkg main \ + ./def +echo "generated client.gen.py" + oto -template client.swift.plush \ -out ./swift/SwiftCLIExample/SwiftCLIExample/client.gen.swift \ -pkg main \ diff --git a/parser/parser.go b/parser/parser.go index 388c8a0..9cd23fe 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -146,6 +146,7 @@ type FieldType struct { IsObject bool `json:"isObject"` JSType string `json:"jsType"` SwiftType string `json:"swiftType"` + PythonType string `json:"pythonType"` } // Parser parses Oto Go definition packages. @@ -427,25 +428,31 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT if ftype.IsObject { ftype.JSType = "object" ftype.SwiftType = "Any" + ftype.PythonType = "Any" } else { switch typeWithoutPointer { case "interface{}": ftype.JSType = "any" ftype.SwiftType = "Any" + ftype.PythonType = "Any" case "map[string]interface{}": ftype.JSType = "object" ftype.SwiftType = "Any" + ftype.PythonType = "Any" case "string": ftype.JSType = "string" ftype.SwiftType = "String" + ftype.PythonType = "str" case "bool": ftype.JSType = "boolean" ftype.SwiftType = "Bool" + ftype.PythonType = "bool" case "int", "int16", "int32", "int64", "uint", "uint16", "uint32", "uint64", "float32", "float64": ftype.JSType = "number" ftype.SwiftType = "Double" + ftype.PythonType = "float" } } @@ -461,9 +468,10 @@ func (p *Parser) addOutputFields() error { NameLowerCamel: "error", Comment: "Error is string explaining what went wrong. Empty if everything was fine.", Type: FieldType{ - TypeName: "string", - JSType: "string", - SwiftType: "String", + TypeName: "string", + JSType: "string", + SwiftType: "String", + PythonType: "str", }, Metadata: map[string]interface{}{}, Example: "something went wrong",