Browse Source

Support for JSON and XML requests. Guess I should add the no-evil clause to the license? Meh; fuck JSON.

zed 4 years ago
parent
commit
9b95b20e4d
3 changed files with 31 additions and 5 deletions
  1. 1 1
      LICENSE
  2. 3 1
      README.md
  3. 27 3
      src/AdjectiveService.py

+ 1 - 1
LICENSE

@@ -1,4 +1,4 @@
-Copyright (c) <year> <owner> . All rights reserved.
+Copyright (c) 2017 Zed Null. All rights reserved.
 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

+ 3 - 1
README.md

@@ -2,4 +2,6 @@
 
 Words as a Service
 
-It's like MadLibs, but opposite in nearly every way.
+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!

+ 27 - 3
src/AdjectiveService.py

@@ -1,11 +1,11 @@
+import falcon
 import random
 
-import falcon
 
 class Adjectives:
     def __init__(self):
         f = open('adjectives1.txt')
-        self.wordlist = f.readlines()
+        self.wordlist = f.read().splitlines()
         random.shuffle(self.wordlist)
         self.index = 0
         self.shuffletime = (3 * len(self.wordlist)) / 5
@@ -25,7 +25,31 @@ class AdjectiveGetter:
         resp.status = falcon.HTTP_200
         resp.body = self.words.getWord()
 
+class AdjectiveGetterJSON:
+    def __init__(self):
+        self.words = Adjectives()
+
+    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 AdjectiveGetterXML:
+    def __init__(self):
+        self.words = Adjectives()
+
+    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)
+
 app = api = falcon.API()
 basicResponder = AdjectiveGetter()
-api.add_route('/basic', basicResponder)
+jsonResponder = AdjectiveGetterJSON()
+xmlResponder = AdjectiveGetterXML()
+api.add_route('/adjective/basic', basicResponder)
+api.add_route('/adjective/json', jsonResponder)
+api.add_route('/adjective/xml', xmlResponder)