Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pacedotdev/oto
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.10.7
Choose a base ref
...
head repository: pacedotdev/oto
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.10.8
Choose a head ref
  • 8 commits
  • 4 files changed
  • 1 contributor

Commits on Nov 16, 2020

  1. added links to users of Oto

    matryer authored Nov 16, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    44d5c44 View commit details

Commits on Nov 19, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5e6cf50 View commit details
  2. Copy the full SHA
    5766639 View commit details
  3. added format_commnt helper

    matryer committed Nov 19, 2020
    Copy the full SHA
    ad2cee6 View commit details
  4. correct style

    matryer committed Nov 19, 2020
    Copy the full SHA
    2e371cf View commit details
  5. Copy the full SHA
    2773bb9 View commit details
  6. first stab at a python sdk

    matryer committed Nov 19, 2020
    Copy the full SHA
    3d1ed4f View commit details
  7. Copy the full SHA
    6231a36 View commit details
Showing with 114 additions and 0 deletions.
  1. +5 −0 README.md
  2. +85 −0 otohttp/templates/client.py.plush
  3. +9 −0 render/render.go
  4. +15 −0 render/render_test.go
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@ Go driven rpc code generation tool for right now.
- Generate server and client code
- Production ready templates (or copy and modify)

## Who's using Oto?

* [Pace.dev](https://pace.dev/docs/) - Oto came out of the Pace project
* [Firesearch.dev](https://firesearch.dev/docs/api) - Firesearch uses Oto to offer full-text services to the web, mobile, and the backend

## Templates

These templates are already being used in production.
85 changes: 85 additions & 0 deletions otohttp/templates/client.py.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Code generated by oto; DO NOT EDIT.

import requests
import json

class Client:
"""Client provides access to the Firesearch API."""

def __init__(self, endpoint="http://localhost:8888/api", apiKey=""):
self.endpoint = endpoint
self.apiKey = apiKey
if self.endpoint == "":
raise FieldError(field="endpoint", message="endpoint missing")
if self.apiKey == "":
raise FieldError(field="apiKey", message="apiKey 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 %>(<%= 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',
'X-API-Key': self.client.apiKey,
}
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 j.error != "":
raise OtoError(message=j.error)
return j
<% } %>
<% } %>

<%= for (object) in def.Objects { %>
class <%= object.Name %>:
"""<%= format_comment_line(object.Comment) %>
Attributes
----------
<%= for (field) in object.Fields { %>
<%= field.NameLowerCamel %>
<%= format_comment_line(field.Comment) %>
<% } %>
"""

def __init__(self):
<%= for (field) in object.Fields { %>self.<%= field.NameLowerCamel %> = None
<% } %>
def json():
""""get a JSON representation of this object"""
return json.dumps(self)

<% } %>

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
9 changes: 9 additions & 0 deletions render/render.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"encoding/json"
"go/doc"
"html/template"
"strings"

"github.com/fatih/structtag"
"github.com/gobuffalo/plush"
@@ -22,6 +23,7 @@ func Render(template string, def parser.Definition, params map[string]interface{
ctx.Set("def", def)
ctx.Set("params", params)
ctx.Set("json", toJSONHelper)
ctx.Set("format_comment_line", formatCommentLine)
ctx.Set("format_comment_text", formatCommentText)
ctx.Set("format_comment_html", formatCommentHTML)
ctx.Set("format_tags", formatTags)
@@ -40,6 +42,13 @@ func toJSONHelper(v interface{}) (template.HTML, error) {
return template.HTML(b), nil
}

func formatCommentLine(s string) template.HTML {
var buf bytes.Buffer
doc.ToText(&buf, s, "", "", 2000)
s = strings.TrimSpace(buf.String())
return template.HTML(s)
}

func formatCommentText(s string) template.HTML {
var buf bytes.Buffer
doc.ToText(&buf, s, "// ", "", 80)
15 changes: 15 additions & 0 deletions render/render_test.go
Original file line number Diff line number Diff line change
@@ -108,4 +108,19 @@ func TestFormatCommentText(t *testing.T) {
actual = strings.TrimSpace(string(formatCommentText(`What happens if I use "quotes"?`)))
is.Equal(actual, `// What happens if I use "quotes"?`)

actual = strings.TrimSpace(string(formatCommentText("What about\nnew lines?")))
is.Equal(actual, `// What about new lines?`)

}

func TestFormatComment(t *testing.T) {
is := is.New(t)

longComment := `This is a long comment that will end up spanning
multiple lines so we get to test the indent string option
in formatComment.`
actual := strings.TrimSpace(string(formatComment(longComment, "\t\t")))
is.Equal(actual, `This is a long comment that will end up spanning multiple lines so we get to
test the indent string option in formatComment.`)

}