cr-connect-workflow/crc/scripts/fact_service.py

47 lines
1.5 KiB
Python
Raw Normal View History

import requests
from crc.scripts.script import Script
class FactService(Script):
def get_description(self):
return """Just your basic class that can pull in data from a few api endpoints and
do a basic task."""
def get_cat(self):
response = requests.get('https://cat-fact.herokuapp.com/facts/random')
return response.json()['text']
def get_buzzword(self):
response = requests.get('https://corporatebs-generator.sameerkumar.website/')
return response.json()['phrase']
def get_norris(self):
response = requests.get('https://api.chucknorris.io/jokes/random')
return response.json()['value']
def do_task_validate_only(self, task, study_id, **kwargs):
self.do_task(task, study_id, **kwargs)
def do_task(self, task, study_id, **kwargs):
2020-02-12 03:13:46 +00:00
print(task.data)
if "type" not in task.data:
raise Exception("No Fact Provided.")
else:
2020-02-12 03:13:46 +00:00
fact = task.data["type"]
2020-02-12 03:13:46 +00:00
if fact == "cat":
details = "The cat in the hat" # self.get_cat()
elif fact == "norris":
details = "Chuck Norris doesnt read books. He stares them down until he gets the information he wants." # self.get_norris()
elif fact == "buzzword":
details = "Move the Needle." # self.get_buzzword()
else:
details = "unknown fact type."
2020-02-12 03:13:46 +00:00
self.add_data_to_task(task, details)
2020-02-12 03:13:46 +00:00
print(details)
return details