Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python client generation to /example #27

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Python client w/ type generation. Uploading in progress work for visi…
…bility
  • Loading branch information
sethcenterbar committed Nov 22, 2020
commit 5af41db4e036fde44c4e0bc5ee42875a2cf28d6e
63 changes: 0 additions & 63 deletions example/client.gen.py

This file was deleted.

15 changes: 13 additions & 2 deletions example/client.py.plush
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import json


class Client:
"""Client provides access to the Greeter Service."""

Expand All @@ -11,20 +12,28 @@ class Client:
if self.endpoint == "":
raise FieldError(field="endpoint", message="endpoint missing")


<%= for (object) in def.Objects { %>class <%= object.Name %>:
"""<%= format_comment_line(object.Comment) %>"""

def __init__(self<%= for (field) in object.Fields { %>, <%=field.NameLowerCamel%>:<%= field.Type.PythonType%><% } %>) -> None:
<%= 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.ObjectNameLowerCamel %>):
def <%= method.NameLowerCamel %>(self, <%= method.InputObject.ObjectName %>) -> <%=method.OutputObject.ObjectName%>:
"""<%= format_comment_line(method.Comment) %>"""
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.ObjectNameLowerCamel %>, headers=headers)
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()
Expand All @@ -40,6 +49,7 @@ class Error(Exception):
"""Base class for exceptions in this module."""
pass


class OtoError(Error):
"""Exception raised for an error making the request.

Expand All @@ -50,6 +60,7 @@ class OtoError(Error):
def __init__(self, message):
self.message = message


class FieldError(Error):
"""Exception raised for missing fields.

Expand Down
2 changes: 1 addition & 1 deletion example/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ oto -template client.js.plush \
echo "generated client.gen.js"

oto -template client.py.plush \
-out client.gen.py \
-out clientgen.py \
-pkg main \
./def
echo "generated client.gen.py"
Expand Down