Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions example/client.py.plush
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions example/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
14 changes: 11 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
}
}

Expand All @@ -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",
Expand Down