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
Next Next commit
Add python client generation to /example
  • Loading branch information
sethcenterbar committed Nov 21, 2020
commit 08265912fb99ee50b1bfcd1e45ecf710eb1ed48b
63 changes: 63 additions & 0 deletions example/client.gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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")

class GreeterService:
"""GreeterService is a polite API for greeting people."""

def __init__(self, client):
self.client = client

def greet(self, greetRequest):
"""Greet prepares a lovely greeting."""
url = "{}/GreeterService.Greet".format(self.client.endpoint)
headers = {
'Accept': 'application/json; charset=utf8',
'Content-Type': 'application/json; charset=utf8'
}
r = requests.post(url, json=greetRequest, 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
63 changes: 63 additions & 0 deletions example/client.py.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 (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 %>):
"""<%= 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)
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 client.gen.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