-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·137 lines (116 loc) · 4.54 KB
/
server.py
File metadata and controls
executable file
·137 lines (116 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#
# Copyright 2014 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
## -*- coding: utf-8 -*-
import os
import cherrypy
import requests
import json
from mako.template import Template
from mako.lookup import TemplateLookup
class PersonalityInsightsService:
"""Wrapper on the Personality Insights service"""
def __init__(self, vcapServices):
"""
Construct an instance. Fetches service parameters from VCAP_SERVICES
runtime variable for Bluemix, or it defaults to local URLs.
"""
# Local variables
self.url = "<url>"
self.username = "<username>"
self.password = "<password>"
if vcapServices is not None:
print("Parsing VCAP_SERVICES")
services = json.loads(vcapServices)
svcName = "personality_insights"
if svcName in services:
print("Personality Insights service found!")
svc = services[svcName][0]["credentials"]
self.url = svc["url"]
self.username = svc["username"]
self.password = svc["password"]
else:
print("ERROR: The Personality Insights service was not found")
def getProfile(self, text):
"""Returns the profile by doing a POST to /v2/profile with text"""
if self.url is None:
raise Exception("No Personality Insights service is bound to this app")
response = requests.post(self.url + "/v2/profile",
auth=(self.username, self.password),
headers = {"content-type": "text/plain"},
data=text
)
try:
return json.loads(response.text)
except:
raise Exception("Error processing the request, HTTP: %d" % response.status_code)
class DemoService(object):
"""
REST service/app. Since we just have 1 GET and 1 POST URLs,
there is not even need to look at paths in the request.
This class implements the handler API for cherrypy library.
"""
exposed = True
def __init__(self, service):
self.service = service
self.defaultContent = None
try:
contentFile = open("mobidick.txt", "r")
self.defaultContent = contentFile.read()
except Exception as e:
print "ERROR: couldn't read mobidick.txt: %s" % e
finally:
contentFile.close()
def GET(self):
"""Shows the default page with sample text content"""
return lookup.get_template("index.html").render(content=self.defaultContent)
def POST(self, text=None):
"""
Send 'text' to the Personality Insights API
and return the response.
"""
try:
profileJson = self.service.getProfile(text)
return json.dumps(profileJson)
except Exception as e:
print "ERROR: %s" % e
return str(e)
if __name__ == '__main__':
lookup = TemplateLookup(directories=["templates"])
# Get host/port from the Bluemix environment, or default to local
HOST_NAME = os.getenv("VCAP_APP_HOST", "127.0.0.1")
PORT_NUMBER = int(os.getenv("VCAP_APP_PORT", "3000"))
cherrypy.config.update({
"server.socket_host": HOST_NAME,
"server.socket_port": PORT_NUMBER,
})
# Configure 2 paths: "public" for all JS/CSS content, and everything
# else in "/" handled by the DemoService
conf = {
"/": {
"request.dispatch": cherrypy.dispatch.MethodDispatcher(),
"tools.response_headers.on": True,
"tools.staticdir.root": os.path.abspath(os.getcwd())
},
"/public": {
"tools.staticdir.on": True,
"tools.staticdir.dir": "./public"
}
}
# Create the Personality Insights Wrapper
personalityInsights = PersonalityInsightsService(os.getenv("VCAP_SERVICES"))
# Start the server
print("Listening on %s:%d" % (HOST_NAME, PORT_NUMBER))
cherrypy.quickstart(DemoService(personalityInsights), "/", config=conf)