Browse Source

Created noun service. Moved service startup/coordination to WaaS.py

zed 4 years ago
parent
commit
147a522b37
5 changed files with 9028 additions and 10 deletions
  1. 1 1
      README.md
  2. 0 9
      src/AdjectiveService.py
  3. 47 0
      src/NounService.py
  4. 20 0
      src/WaaS.py
  5. 8960 0
      src/nouns.txt

+ 1 - 1
README.md

@@ -4,4 +4,4 @@ Words as a Service
 
 It's like MadLibs, but opposite in nearly every way.
 
-Try something like `gunicorn AdjectiveService:api' in the src directory and hope for the best!
+Try something like `gunicorn WaaS:api' in the src directory and hope for the best!

+ 0 - 9
src/AdjectiveService.py

@@ -1,7 +1,6 @@
 import falcon
 import random
 
-
 class Adjectives:
     def __init__(self):
         f = open('adjectives1.txt')
@@ -45,11 +44,3 @@ class AdjectiveGetterXML:
         chosenWord = self.words.getWord()
         resp.body = '<adjective>{0}</adjective>'.format(chosenWord)
 
-app = api = falcon.API()
-basicResponder = AdjectiveGetter()
-jsonResponder = AdjectiveGetterJSON()
-xmlResponder = AdjectiveGetterXML()
-api.add_route('/adjective/basic', basicResponder)
-api.add_route('/adjective/json', jsonResponder)
-api.add_route('/adjective/xml', xmlResponder)
-

+ 47 - 0
src/NounService.py

@@ -0,0 +1,47 @@
+import falcon
+import random
+
+
+class Nouns:
+    def __init__(self):
+        f = open('nouns.txt')
+        self.wordlist = f.read().splitlines()
+        random.shuffle(self.wordlist)
+        self.index = 0
+        self.shuffletime = (3 * len(self.wordlist)) / 5
+    def getWord(self):
+        self.index += 1
+        if self.index > self.shuffletime:
+            random.shuffle(self.wordlist)
+            self.index = 0
+        return self.wordlist[self.index]
+
+class NounGetter:
+    def __init__(self):
+        self.words = Nouns()
+
+    def on_get(self, req, resp):
+        """Handle GET requests"""
+        resp.status = falcon.HTTP_200
+        resp.body = self.words.getWord()
+
+class NounGetterJSON:
+    def __init__(self):
+        self.words = Nouns()
+
+    def on_get(self, req, resp):
+        """Handle GET requests"""
+        resp.status = falcon.HTTP_200
+        chosenWord = self.words.getWord()
+        resp.body = '{{"adjective":"{0}"}}'.format(chosenWord)
+
+class NounGetterXML:
+    def __init__(self):
+        self.words = Nouns()
+
+    def on_get(self, req, resp):
+        """Handle GET requests"""
+        resp.status = falcon.HTTP_200
+        chosenWord = self.words.getWord()
+        resp.body = '<adjective>{0}</adjective>'.format(chosenWord)
+

+ 20 - 0
src/WaaS.py

@@ -0,0 +1,20 @@
+import falcon
+import random
+
+import AdjectiveService
+import NounService
+
+app = api = falcon.API()
+aBasicResponder = AdjectiveService.AdjectiveGetter()
+aJsonResponder = AdjectiveService.AdjectiveGetterJSON()
+aXmlResponder = AdjectiveService.AdjectiveGetterXML()
+nBasicResponder = AdjectiveService.AdjectiveGetter()
+nJsonResponder = AdjectiveService.AdjectiveGetterJSON()
+nXmlResponder = AdjectiveService.AdjectiveGetterXML()
+api.add_route('/adjective/basic', aBasicResponder)
+api.add_route('/adjective/json', aJsonResponder)
+api.add_route('/adjective/xml', aXmlResponder)
+api.add_route('/noun/basic', nBasicResponder)
+api.add_route('/noun/json', nJsonResponder)
+api.add_route('/noun/xml', nXmlResponder)
+

File diff suppressed because it is too large
+ 8960 - 0
src/nouns.txt