Skip to content

Commit

Permalink
Move functions to class (#28)
Browse files Browse the repository at this point in the history
* Remove helpers module for simplicity.
Move functions to apprentice class

* Update examples in readme and init

* Linting

* update changelog.
Bump version
  • Loading branch information
andrewgy8 authored Dec 9, 2018
1 parent 9329c87 commit 93311ff
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### v0.3.0
- Move functions to apprentice encapsulation
- Remove `helpers` module.

### v0.2.0
- Update to Dialogflow API 2.0
- Fix routing issue
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Build Status](https://travis-ci.com/andrewgy8/apprentice.svg?branch=master)](https://travis-ci.com/andrewgy8/apprentice)
[![PyPI version](https://badge.fury.io/py/apprentice.svg)](https://badge.fury.io/py/apprentice)

Apprentice is a library for deploying and developing actions via Dialogflow and Google Cloud Functions.
Apprentice is a framework for developing actions via Dialogflow and Google Cloud Functions.

## Installation

Expand Down Expand Up @@ -64,7 +64,7 @@ apr = Apprentice(__name__)
@apr.route('/', methods=['POST'])
def hello_world(*args, **kwargs):
reply = 'Hello world!'
return apr.response(reply)
return apr.generate_text_response(reply)

```

Expand Down
2 changes: 1 addition & 1 deletion apprentice/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 2, 0)
VERSION = (0, 3, 0)

__version__ = '.'.join(map(str, VERSION))
2 changes: 1 addition & 1 deletion apprentice/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@apr.route('/', methods=['POST'])
def hello_world(*args, **kwargs):
reply = 'Hello world!'
return apr.response(reply)
return apr.generate_text_response(reply)
"""

Expand Down
68 changes: 33 additions & 35 deletions apprentice/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@
from flask import Flask, make_response


def response(data):
resp = json.dumps(data, indent=4)
resp = make_response(resp)
resp.headers['Content-Type'] = 'application/json'
return resp


def query_result(text, expect_user_response=True):
return {
'fulfillmentText': text,
'fulfillmentMessages': [
{
"platform": "ACTIONS_ON_GOOGLE",
"text": {
"text": [
text
]
}
}
],
'payload': {
'google': {
"expect_user_response": expect_user_response,
"is_ssml": True,
"permissions_request": None,
}
},
'outputContexts': [],
'source': 'webhook'
}


class Apprentice(Flask):

def __init__(self, name, *args, **kwargs):
self.name = name
super().__init__(__name__, *args, **kwargs)

def response(self, reply):
res = query_result(reply)
return response(res)
def generate_text_response(self, reply):
response_object = self.query_result(reply)
return self.generate_response(response_object)

def generate_response(self, data):
resp = json.dumps(data, indent=4)
resp = make_response(resp)
resp.headers['Content-Type'] = 'application/json'
return resp

def query_result(self, text, expect_user_response=True):
return {
'fulfillmentText': text,
'fulfillmentMessages': [
{
"platform": "ACTIONS_ON_GOOGLE",
"text": {
"text": [
text
]
}
}
],
'payload': {
'google': {
"expect_user_response": expect_user_response,
"is_ssml": True,
"permissions_request": None,
}
},
'outputContexts': [],
'source': 'webhook'
}
9 changes: 0 additions & 9 deletions apprentice/helpers.py

This file was deleted.

2 changes: 1 addition & 1 deletion example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@apr.route('/', methods=['POST'])
def cool_fact_generator(*args, **kwargs):
reply = _fact_response('name')
return apr.response(reply)
return apr.generate_text_response(reply)


def _fact_response(entity):
Expand Down
15 changes: 8 additions & 7 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from flask import Response

from apprentice import format
from apprentice.helpers import action_ctx_test
from apprentice import Apprentice


class TestIntentResponse:

def test_returns_object_when_given_text(self):
apr = Apprentice(__name__)
text = 'Hello world'
json_response = format.query_result(text, expect_user_response=True)
json_response = apr.query_result(text, expect_user_response=True)

assert json_response == {
'outputContexts': [],
Expand All @@ -34,9 +34,9 @@ def test_returns_object_when_given_text(self):
}

def test_returns_expect_user_response_when_set_to_false(self):
apr = Apprentice(__name__)
text = 'Hello world'
json_response = format.query_result(text,
expect_user_response=False)
json_response = apr.query_result(text, expect_user_response=False)

assert json_response == {
'outputContexts': [],
Expand Down Expand Up @@ -64,10 +64,11 @@ def test_returns_expect_user_response_when_set_to_false(self):

class TestFormatResponse:

@action_ctx_test
def test_returns_flask_response_object(self):
apr = Apprentice(__name__)
data = {'msg': 'hello world'}
res = format.response(data)
with apr.test_request_context():
res = apr.generate_response(data)

assert isinstance(res, Response)
assert res.json == data
Expand Down

0 comments on commit 93311ff

Please sign in to comment.